mirror of
https://github.com/koalaman/shellcheck
synced 2025-07-13 00:23:20 -07:00
Warn about read foo[i]
expanding as glob (fixes #2345)
This commit is contained in:
parent
05bdeae3ab
commit
205ba429b3
3 changed files with 24 additions and 4 deletions
|
@ -790,6 +790,7 @@ prop_checkReadExpansions5 = verify checkReadExpansions "read \"$var\""
|
|||
prop_checkReadExpansions6 = verify checkReadExpansions "read -a $var"
|
||||
prop_checkReadExpansions7 = verifyNot checkReadExpansions "read $1"
|
||||
prop_checkReadExpansions8 = verifyNot checkReadExpansions "read ${var?}"
|
||||
prop_checkReadExpansions9 = verify checkReadExpansions "read arr[val]"
|
||||
checkReadExpansions = CommandCheck (Exactly "read") check
|
||||
where
|
||||
options = getGnuOpts flagsForRead
|
||||
|
@ -797,13 +798,26 @@ checkReadExpansions = CommandCheck (Exactly "read") check
|
|||
opts <- options $ arguments cmd
|
||||
return [y | (x,(_, y)) <- opts, null x || x == "a"]
|
||||
|
||||
check cmd = mapM_ warning $ getVars cmd
|
||||
warning t = sequence_ $ do
|
||||
check cmd = do
|
||||
mapM_ dollarWarning $ getVars cmd
|
||||
mapM_ arrayWarning $ arguments cmd
|
||||
|
||||
dollarWarning t = sequence_ $ do
|
||||
name <- getSingleUnmodifiedBracedString t
|
||||
guard $ isVariableName name -- e.g. not $1
|
||||
return . warn (getId t) 2229 $
|
||||
"This does not read '" ++ name ++ "'. Remove $/${} for that, or use ${var?} to quiet."
|
||||
|
||||
arrayWarning word =
|
||||
when (any isUnquotedBracket $ getWordParts word) $
|
||||
warn (getId word) 2313 $
|
||||
"Quote array indices to avoid them expanding as globs."
|
||||
|
||||
isUnquotedBracket t =
|
||||
case t of
|
||||
T_Glob _ ('[':_) -> True
|
||||
_ -> False
|
||||
|
||||
-- Return the single variable expansion that makes up this word, if any.
|
||||
-- e.g. $foo -> $foo, "$foo"'' -> $foo , "hello $name" -> Nothing
|
||||
getSingleUnmodifiedBracedString :: Token -> Maybe String
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue