mirror of
https://github.com/koalaman/shellcheck
synced 2025-07-29 19:19:10 -07:00
Warn about export/declare/local masking return values.
This commit is contained in:
parent
0a2cf208c8
commit
0e1a64b6ba
2 changed files with 24 additions and 1 deletions
|
@ -207,6 +207,7 @@ nodeChecks = [
|
||||||
,checkTildeInPath
|
,checkTildeInPath
|
||||||
,checkFindExecWithSingleArgument
|
,checkFindExecWithSingleArgument
|
||||||
,checkReturn
|
,checkReturn
|
||||||
|
,checkMaskedReturns
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -3261,5 +3262,27 @@ checkFindExecWithSingleArgument _ = checkCommand "find" (const f)
|
||||||
commandRegex = mkRegex "[ |;]"
|
commandRegex = mkRegex "[ |;]"
|
||||||
|
|
||||||
|
|
||||||
|
prop_checkMaskedReturns1 = verify checkMaskedReturns "f() { local a=$(false); }"
|
||||||
|
prop_checkMaskedReturns2 = verify checkMaskedReturns "declare a=$(false)"
|
||||||
|
prop_checkMaskedReturns3 = verify checkMaskedReturns "declare a=\"`false`\""
|
||||||
|
prop_checkMaskedReturns4 = verifyNot checkMaskedReturns "declare a; a=$(false)"
|
||||||
|
prop_checkMaskedReturns5 = verifyNot checkMaskedReturns "f() { local -r a=$(false); }"
|
||||||
|
checkMaskedReturns _ t@(T_SimpleCommand id _ (cmd:rest)) = potentially $ do
|
||||||
|
name <- getCommandName t
|
||||||
|
guard $ name `elem` ["declare", "export"]
|
||||||
|
|| name == "local" && "r" `notElem` getFlags t
|
||||||
|
return $ mapM_ checkArgs rest
|
||||||
|
where
|
||||||
|
checkArgs (T_Assignment id _ _ _ word) | any hasReturn $ getWordParts word =
|
||||||
|
warn id 2155 "Declare and assign separately to avoid masking return values."
|
||||||
|
checkArgs _ = return ()
|
||||||
|
|
||||||
|
hasReturn t = case t of
|
||||||
|
T_Backticked {} -> True
|
||||||
|
T_DollarExpansion {} -> True
|
||||||
|
_ -> False
|
||||||
|
checkMaskedReturns _ _ = return ()
|
||||||
|
|
||||||
|
|
||||||
return []
|
return []
|
||||||
runTests = $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])
|
runTests = $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])
|
||||||
|
|
|
@ -950,7 +950,7 @@ readNormalEscaped = called "escaped char" $ do
|
||||||
pos <- getPosition
|
pos <- getPosition
|
||||||
backslash
|
backslash
|
||||||
do
|
do
|
||||||
next <- quotable <|> oneOf "?*@!+[]{}.,~"
|
next <- quotable <|> oneOf "?*@!+[]{}.,~#"
|
||||||
return $ if next == '\n' then "" else [next]
|
return $ if next == '\n' then "" else [next]
|
||||||
<|>
|
<|>
|
||||||
do
|
do
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue