mirror of
https://github.com/koalaman/shellcheck
synced 2025-07-13 16:43:20 -07:00
Skip SC2214 if variable is modified in loop (fixes #2351)
This commit is contained in:
parent
3aedda766d
commit
c3aaa27540
3 changed files with 23 additions and 9 deletions
|
@ -961,11 +961,13 @@ prop_checkWhileGetoptsCase4 = verifyNot checkWhileGetoptsCase "while getopts 'a:
|
|||
prop_checkWhileGetoptsCase5 = verifyNot checkWhileGetoptsCase "while getopts 'a:' x; do case $x in a) foo;; \\?) bar;; *) baz;; esac; done"
|
||||
prop_checkWhileGetoptsCase6 = verifyNot checkWhileGetoptsCase "while getopts 'a:b' x; do case $y in a) foo;; esac; done"
|
||||
prop_checkWhileGetoptsCase7 = verifyNot checkWhileGetoptsCase "while getopts 'a:b' x; do case x$x in xa) foo;; xb) foo;; esac; done"
|
||||
prop_checkWhileGetoptsCase8 = verifyNot checkWhileGetoptsCase "while getopts 'a:b' x; do x=a; case $x in a) foo;; esac; done"
|
||||
checkWhileGetoptsCase = CommandCheck (Exactly "getopts") f
|
||||
where
|
||||
f :: Token -> Analysis
|
||||
f t@(T_SimpleCommand _ _ (cmd:arg1:name:_)) = do
|
||||
path <- getPathM t
|
||||
params <- ask
|
||||
sequence_ $ do
|
||||
options <- getLiteralString arg1
|
||||
getoptsVar <- getLiteralString name
|
||||
|
@ -977,6 +979,9 @@ checkWhileGetoptsCase = CommandCheck (Exactly "getopts") f
|
|||
[T_Literal _ caseVar] <- return $ getWordParts bracedWord
|
||||
guard $ caseVar == getoptsVar
|
||||
|
||||
-- Make sure the variable isn't modified
|
||||
guard . not $ modifiesVariable params (T_BraceGroup (Id 0) body) getoptsVar
|
||||
|
||||
return $ check (getId arg1) (map (:[]) $ filter (/= ':') options) caseCmd
|
||||
f _ = return ()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue