From f4afb9a88f68880824d117b0dd5cf26cebb29237 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Mon, 26 Nov 2012 20:42:10 -0800 Subject: [PATCH] Added check for constant [[]] expression, e.g. [[ a == b ]] --- ShellCheck/Analytics.hs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/ShellCheck/Analytics.hs b/ShellCheck/Analytics.hs index e4af4a9..32f89db 100644 --- a/ShellCheck/Analytics.hs +++ b/ShellCheck/Analytics.hs @@ -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