Skip to content

Commit

Permalink
Warn about foo=(bar baz); echo $foo
Browse files Browse the repository at this point in the history
  • Loading branch information
koalaman committed Apr 19, 2014
1 parent 5f568dd commit 58c362f
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions ShellCheck/Analytics.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ treeChecks = [
,checkFunctionsUsedExternally
,checkUnusedAssignments
,checkUnpassedInFunctions
,checkArrayWithoutIndex
]

checksFor Sh = [
Expand Down Expand Up @@ -854,6 +855,27 @@ checkArrayAsString _ (T_Assignment id _ _ _ word) =
"Brace expansions and globs are literal in assignments. Quote it or use an array."
checkArrayAsString _ _ = return ()

prop_checkArrayWithoutIndex1 = verifyTree checkArrayWithoutIndex "foo=(a b); echo $foo"
prop_checkArrayWithoutIndex2 = verifyNotTree checkArrayWithoutIndex "foo='bar baz'; foo=($foo); echo ${foo[0]}"
checkArrayWithoutIndex params _ =
concat $ doVariableFlowAnalysis readF writeF Map.empty (variableFlow params)
where
readF _ (T_DollarBraced id token) _ = do
map <- get
return . maybeToList $ do
name <- getLiteralString token
assignment <- Map.lookup name map
return [(Note id WarningC 2128
"Expanding an array without an index only gives the first element.")]
readF _ _ _ = return []

writeF _ t name (DataFrom [T_Array {}]) = do
modify (Map.insert name t)
return []
writeF _ _ name _ = do
modify (Map.delete name)
return []

prop_checkStderrRedirect = verify checkStderrRedirect "test 2>&1 > cow"
prop_checkStderrRedirect2 = verifyNot checkStderrRedirect "test > cow 2>&1"
checkStderrRedirect _ (T_Redirecting _ [
Expand Down

0 comments on commit 58c362f

Please sign in to comment.