From a2cc44a04d45d6091cf9b9c3e4dea27ed8a8c177 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Thu, 15 Nov 2012 23:32:45 -0800 Subject: [PATCH] Added check for $(($n)) --- ShellCheck/Analytics.hs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ShellCheck/Analytics.hs b/ShellCheck/Analytics.hs index b53e96f..8b0a1ca 100644 --- a/ShellCheck/Analytics.hs +++ b/ShellCheck/Analytics.hs @@ -37,6 +37,7 @@ basicChecks = [ ,checkBraceExpansionVars ,checkForDecimals ,checkDivBeforeMult + ,checkArithmeticDeref ] modifyMap = modify @@ -252,6 +253,13 @@ checkDivBeforeMult (TA_Binary _ "*" (TA_Binary id "/" _ _) _) = do addNoteFor id $ Note InfoC $ "Increase precision by replacing a/b*c with a*c/b" checkDivBeforeMult _ = return () +prop_checkArithmeticDeref = verify checkArithmeticDeref "echo $((3+$foo))" +prop_checkArithmeticDeref2 = verify checkArithmeticDeref "cow=14; (( s+= $cow ))" +prop_checkArithmeticDeref3 = verifyNot checkArithmeticDeref "cow=1/40; (( s+= ${cow%%/*} ))" +checkArithmeticDeref (TA_Expansion _ (T_DollarBraced id str)) | not $ any (`elem` "/.:#%") $ str = + addNoteFor id $ Note WarningC $ "Don't use $ on variables in (( )) unless you want to dereference twice" +checkArithmeticDeref _ = return () + allModifiedVariables t = snd $ runState (doAnalysis (\x -> modify $ (++) (getModifiedVariables x)) t) [] --- Subshell detection