Add warning about local in sh or not in bash functions.

This commit is contained in:
Vidar Holen 2015-10-10 20:48:52 -07:00
parent 58d45e3fa4
commit 1eece5b2ee
2 changed files with 12 additions and 2 deletions

View File

@ -224,6 +224,8 @@ isAssignment t =
T_Annotation _ _ w -> isAssignment w
otherwise -> False
isFunction t = case t of T_Function {} -> True; _ -> False
-- Get the list of commands from tokens that contain them, such as
-- the body of while loops and if statements.
getCommandSequences t =

View File

@ -82,6 +82,7 @@ checksFor Bash = [
,checkBraceExpansionVars
,checkEchoSed
,checkForDecimals
,checkLocalScope
]
runAnalytics :: AnalysisSpec -> AnalysisResult
@ -683,7 +684,7 @@ checkBashisms _ = bashism
bashCommands = [
"let", "caller", "builtin", "complete", "compgen", "declare", "dirs", "disown",
"enable", "mapfile", "readarray", "pushd", "popd", "shopt", "suspend", "type",
"typeset"
"typeset", "local"
]
allowedFlags = Map.fromList [
("read", ["r"]),
@ -2965,11 +2966,18 @@ checkLoopKeywordScope params t |
subshellType t = case leadType (shellType params) (parentMap params) t of
NoneScope -> Nothing
SubshellScope str -> return str
isFunction t = case t of T_Function {} -> True; _ -> False
relevant t = isLoop t || isFunction t || isJust (subshellType t)
checkLoopKeywordScope _ _ = return ()
prop_checkLocalScope1 = verify checkLocalScope "local foo=3"
prop_checkLocalScope2 = verifyNot checkLocalScope "f() { local foo=3; }"
checkLocalScope params t | t `isCommand` "local" && not (isInFunction t) =
err (getId t) 2168 "'local' is only valid in functions."
where
isInFunction t = any isFunction $ getPath (parentMap params) t
checkLocalScope _ _ = return ()
prop_checkFunctionDeclarations1 = verify checkFunctionDeclarations "#!/bin/ksh\nfunction foo() { command foo --lol \"$@\"; }"
prop_checkFunctionDeclarations2 = verify checkFunctionDeclarations "#!/bin/dash\nfunction foo { lol; }"
prop_checkFunctionDeclarations3 = verifyNot checkFunctionDeclarations "foo() { echo bar; }"