From 13ff0a74328d7be12345fc44b79a1a0f2832be8e Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sat, 14 May 2016 16:24:18 -0700 Subject: [PATCH] Warn when arrays are appended/assigned scalars. --- ShellCheck/Analytics.hs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/ShellCheck/Analytics.hs b/ShellCheck/Analytics.hs index a74878b..3b62142 100644 --- a/ShellCheck/Analytics.hs +++ b/ShellCheck/Analytics.hs @@ -914,8 +914,10 @@ prop_checkArrayWithoutIndex3 = verifyTree checkArrayWithoutIndex "coproc foo whi prop_checkArrayWithoutIndex4 = verifyTree checkArrayWithoutIndex "coproc tail -f log; echo $COPROC" prop_checkArrayWithoutIndex5 = verifyTree checkArrayWithoutIndex "a[0]=foo; echo $a" prop_checkArrayWithoutIndex6 = verifyTree checkArrayWithoutIndex "echo $PIPESTATUS" +prop_checkArrayWithoutIndex7 = verifyTree checkArrayWithoutIndex "a=(a b); a+=c" +prop_checkArrayWithoutIndex8 = verifyTree checkArrayWithoutIndex "declare -a foo; foo=bar;" checkArrayWithoutIndex params _ = - concat $ doVariableFlowAnalysis readF writeF defaultMap (variableFlow params) + doVariableFlowAnalysis readF writeF defaultMap (variableFlow params) where defaultMap = Map.fromList $ map (\x -> (x,())) arrayVariables readF _ (T_DollarBraced id token) _ = do @@ -923,10 +925,17 @@ checkArrayWithoutIndex params _ = return . maybeToList $ do name <- getLiteralString token assigned <- Map.lookup name map - return [makeComment WarningC id 2128 - "Expanding an array without an index only gives the first element."] + return $ makeComment WarningC id 2128 + "Expanding an array without an index only gives the first element." readF _ _ _ = return [] + writeF _ (T_Assignment id mode name Nothing _) _ (DataString _) = do + isArray <- gets (isJust . Map.lookup name) + return $ if not isArray then [] else + case mode of + Assign -> [makeComment WarningC id 2178 "Variable was used as an array but is now assigned a string."] + Append -> [makeComment WarningC id 2179 "Use array+=(\"item\") to append items to an array."] + writeF _ t name (DataArray _) = do modify (Map.insert name ()) return []