Add warning about local in sh or not in bash functions.
This commit is contained in:
parent
58d45e3fa4
commit
1eece5b2ee
|
@ -224,6 +224,8 @@ isAssignment t =
|
||||||
T_Annotation _ _ w -> isAssignment w
|
T_Annotation _ _ w -> isAssignment w
|
||||||
otherwise -> False
|
otherwise -> False
|
||||||
|
|
||||||
|
isFunction t = case t of T_Function {} -> True; _ -> False
|
||||||
|
|
||||||
-- Get the list of commands from tokens that contain them, such as
|
-- Get the list of commands from tokens that contain them, such as
|
||||||
-- the body of while loops and if statements.
|
-- the body of while loops and if statements.
|
||||||
getCommandSequences t =
|
getCommandSequences t =
|
||||||
|
|
|
@ -82,6 +82,7 @@ checksFor Bash = [
|
||||||
,checkBraceExpansionVars
|
,checkBraceExpansionVars
|
||||||
,checkEchoSed
|
,checkEchoSed
|
||||||
,checkForDecimals
|
,checkForDecimals
|
||||||
|
,checkLocalScope
|
||||||
]
|
]
|
||||||
|
|
||||||
runAnalytics :: AnalysisSpec -> AnalysisResult
|
runAnalytics :: AnalysisSpec -> AnalysisResult
|
||||||
|
@ -683,7 +684,7 @@ checkBashisms _ = bashism
|
||||||
bashCommands = [
|
bashCommands = [
|
||||||
"let", "caller", "builtin", "complete", "compgen", "declare", "dirs", "disown",
|
"let", "caller", "builtin", "complete", "compgen", "declare", "dirs", "disown",
|
||||||
"enable", "mapfile", "readarray", "pushd", "popd", "shopt", "suspend", "type",
|
"enable", "mapfile", "readarray", "pushd", "popd", "shopt", "suspend", "type",
|
||||||
"typeset"
|
"typeset", "local"
|
||||||
]
|
]
|
||||||
allowedFlags = Map.fromList [
|
allowedFlags = Map.fromList [
|
||||||
("read", ["r"]),
|
("read", ["r"]),
|
||||||
|
@ -2965,11 +2966,18 @@ checkLoopKeywordScope params t |
|
||||||
subshellType t = case leadType (shellType params) (parentMap params) t of
|
subshellType t = case leadType (shellType params) (parentMap params) t of
|
||||||
NoneScope -> Nothing
|
NoneScope -> Nothing
|
||||||
SubshellScope str -> return str
|
SubshellScope str -> return str
|
||||||
isFunction t = case t of T_Function {} -> True; _ -> False
|
|
||||||
relevant t = isLoop t || isFunction t || isJust (subshellType t)
|
relevant t = isLoop t || isFunction t || isJust (subshellType t)
|
||||||
checkLoopKeywordScope _ _ = return ()
|
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_checkFunctionDeclarations1 = verify checkFunctionDeclarations "#!/bin/ksh\nfunction foo() { command foo --lol \"$@\"; }"
|
||||||
prop_checkFunctionDeclarations2 = verify checkFunctionDeclarations "#!/bin/dash\nfunction foo { lol; }"
|
prop_checkFunctionDeclarations2 = verify checkFunctionDeclarations "#!/bin/dash\nfunction foo { lol; }"
|
||||||
prop_checkFunctionDeclarations3 = verifyNot checkFunctionDeclarations "foo() { echo bar; }"
|
prop_checkFunctionDeclarations3 = verifyNot checkFunctionDeclarations "foo() { echo bar; }"
|
||||||
|
|
Loading…
Reference in New Issue