Added check for constant [[]] expression, e.g. [[ a == b ]]

This commit is contained in:
Vidar Holen 2012-11-26 20:42:10 -08:00
parent 648090af31
commit f4afb9a88f
1 changed files with 14 additions and 0 deletions

View File

@ -62,6 +62,7 @@ basicChecks = [
,checkCommarrays
,checkOrNeq
,checkEcho
,checkConstantIfs
]
modifyMap = modify
@ -287,6 +288,19 @@ checkNumberComparisons (TC_Binary id typ op lhs rhs)
eqv _ = "the numerical equivalent"
checkNumberComparisons _ = return ()
prop_checkConstantIfs1 = verify checkConstantIfs "[[ foo != bar ]]"
prop_checkConstantIfs2 = verify checkConstantIfs "[[ n -le 4 ]]"
prop_checkConstantIfs3 = verify checkConstantIfs "[[ $n -le 4 && n -ge 2 ]]"
prop_checkConstantIfs4 = verifyNot checkConstantIfs "[[ $n -le 3 ]]"
prop_checkConstantIfs5 = verifyNot checkConstantIfs "[[ $n -le $n ]]"
checkConstantIfs (TC_Binary id typ op lhs rhs)
| op `elem` [ "==", "!=", "<=", ">=", "-eq", "-ne", "-lt", "-le", "-gt", "-ge", "=~", ">", "<", "="] = do
when (isJust lLit && isJust rLit) $ warn id $ "This expression is constant. Did you forget the $ on a variable?"
where
lLit = getLiteralString lhs
rLit = getLiteralString rhs
checkConstantIfs _ = return ()
prop_checkNoaryWasBinary = verify checkNoaryWasBinary "[[ a==$foo ]]"
prop_checkNoaryWasBinary2 = verify checkNoaryWasBinary "[ $foo=3 ]"
checkNoaryWasBinary (TC_Noary _ _ t@(T_NormalWord id l)) = do