Warn about [ foo ] when "foo" is non-constant with constant parts.

This commit is contained in:
Vidar Holen 2015-07-18 09:56:58 -07:00
parent 7eef12102b
commit 7e84ad031f
1 changed files with 19 additions and 11 deletions

View File

@ -134,7 +134,7 @@ nodeChecks = [
,checkNumberComparisons ,checkNumberComparisons
,checkSingleBracketOperators ,checkSingleBracketOperators
,checkDoubleBracketOperators ,checkDoubleBracketOperators
,checkNoaryWasBinary ,checkNoaryTestAlwaysTrue
,checkConstantNoary ,checkConstantNoary
,checkDivBeforeMult ,checkDivBeforeMult
,checkArithmeticDeref ,checkArithmeticDeref
@ -1282,19 +1282,27 @@ checkConstantIfs _ (TC_Binary id typ op lhs rhs)
rLit = getLiteralString rhs rLit = getLiteralString rhs
checkConstantIfs _ _ = return () checkConstantIfs _ _ = return ()
prop_checkNoaryWasBinary = verify checkNoaryWasBinary "[[ a==$foo ]]" prop_checkNoaryTestAlwaysTrue = verify checkNoaryTestAlwaysTrue "[[ a==$foo ]]"
prop_checkNoaryWasBinary2 = verify checkNoaryWasBinary "[ $foo=3 ]" prop_checkNoaryTestAlwaysTrue2 = verify checkNoaryTestAlwaysTrue "[ $foo=3 ]"
prop_checkNoaryWasBinary3 = verify checkNoaryWasBinary "[ $foo!=3 ]" prop_checkNoaryTestAlwaysTrue3 = verify checkNoaryTestAlwaysTrue "[ $foo!=3 ]"
checkNoaryWasBinary _ (TC_Noary _ _ t@(T_NormalWord _ l)) | not $ isConstant t = potentially $ do prop_checkNoaryTestAlwaysTrue4 = verify checkNoaryTestAlwaysTrue "[ \"$(ls) \" ]"
token <- getEqualsPart checkNoaryTestAlwaysTrue _ (TC_Noary _ _ t@(T_NormalWord _ l)) | not $ isConstant t =
return $ err (getId token) 2077 "You need spaces around the comparison operator." potentially $ comparisonWarning `mplus` tautologyWarning
where where
hasEquals t = isJust $ do hasEquals = matchToken ('=' `elem`)
isNonEmpty = matchToken (not . null)
matchToken m t = isJust $ do
str <- getLiteralString t str <- getLiteralString t
guard $ '=' `elem` str guard $ m str
return () return ()
getEqualsPart = listToMaybe $ filter hasEquals l
checkNoaryWasBinary _ _ = return () comparisonWarning = do
token <- listToMaybe $ filter hasEquals l
return $ err (getId token) 2077 "You need spaces around the comparison operator."
tautologyWarning = do
token <- listToMaybe $ filter isNonEmpty $ getWordParts t
return $ err (getId token) 2157 "Argument to implicit -n is always true due to literal strings."
checkNoaryTestAlwaysTrue _ _ = return ()
prop_checkConstantNoary = verify checkConstantNoary "[[ '$(foo)' ]]" prop_checkConstantNoary = verify checkConstantNoary "[[ '$(foo)' ]]"
prop_checkConstantNoary2 = verify checkConstantNoary "[ \"-f lol\" ]" prop_checkConstantNoary2 = verify checkConstantNoary "[ \"-f lol\" ]"