Add check for [[ "foo" ]]

This commit is contained in:
Vidar Holen 2013-02-14 19:43:43 -08:00
parent 585529a636
commit d2e2d06978
1 changed files with 26 additions and 0 deletions

View File

@ -55,6 +55,7 @@ basicChecks = [
,checkNumberComparisons ,checkNumberComparisons
,checkSingleBracketOperators ,checkSingleBracketOperators
,checkNoaryWasBinary ,checkNoaryWasBinary
,checkConstantNoary
,checkBraceExpansionVars ,checkBraceExpansionVars
,checkForDecimals ,checkForDecimals
,checkDivBeforeMult ,checkDivBeforeMult
@ -114,6 +115,22 @@ isGlob (T_Glob _ _) = True
isGlob (T_NormalWord _ l) = any isGlob l isGlob (T_NormalWord _ l) = any isGlob l
isGlob _ = False isGlob _ = False
isConstant token =
case token of
T_NormalWord _ l -> all isConstant l
T_DoubleQuoted _ l -> all isConstant l
T_SingleQuoted _ _ -> True
T_Literal _ _ -> True
_ -> False
isEmpty token =
case token of
T_NormalWord _ l -> all isEmpty l
T_DoubleQuoted _ l -> all isEmpty l
T_SingleQuoted _ "" -> True
T_Literal _ "" -> True
_ -> False
makeSimple (T_NormalWord _ [f]) = f makeSimple (T_NormalWord _ [f]) = f
makeSimple (T_Redirecting _ _ f) = f makeSimple (T_Redirecting _ _ f) = f
makeSimple t = t makeSimple t = t
@ -523,6 +540,15 @@ checkNoaryWasBinary (TC_Noary _ _ t@(T_NormalWord id l)) = do
when ('=' `elem` str) $ err id $ "Always true because you didn't put spaces around the operator." when ('=' `elem` str) $ err id $ "Always true because you didn't put spaces around the operator."
checkNoaryWasBinary _ = return () checkNoaryWasBinary _ = return ()
prop_checkConstantNoary = verify checkConstantNoary "[[ '$(foo)' ]]"
prop_checkConstantNoary2 = verify checkConstantNoary "[ \"-f lol\" ]"
prop_checkConstantNoary3 = verify checkConstantNoary "[[ cmd ]]"
prop_checkConstantNoary4 = verify checkConstantNoary "[[ ! cmd ]]"
checkConstantNoary (TC_Noary _ _ t@(T_NormalWord id _)) | isConstant t = do
err id $ if isEmpty t then "Always false, just checks the non-emptyness of the literal word."
else "Always true, just checks the non-emptyness of the literal word."
checkConstantNoary _ = return ()
prop_checkBraceExpansionVars = verify checkBraceExpansionVars "echo {1..$n}" prop_checkBraceExpansionVars = verify checkBraceExpansionVars "echo {1..$n}"
checkBraceExpansionVars (T_BraceExpansion id s) | '$' `elem` s = checkBraceExpansionVars (T_BraceExpansion id s) | '$' `elem` s =
warn id $ "Bash doesn't support variables in brace expansions." warn id $ "Bash doesn't support variables in brace expansions."