Suggest quoting expansions in for loop globs

This commit is contained in:
Vidar Holen 2018-03-24 17:43:20 -07:00
parent c3b606c68a
commit 764b242f1b
3 changed files with 25 additions and 0 deletions

View File

@ -1,5 +1,6 @@
## Latest - ??? ## Latest - ???
### Added ### Added
- SC2231: Suggest quoting expansions in for loop globs
- SC2229: Warn about 'read $var' - SC2229: Warn about 'read $var'
- SC2227: Warn about redirections in the middle of 'find' commands - SC2227: Warn about redirections in the middle of 'find' commands
- SC2224,SC2225,SC2226: Warn when using mv/cp/ln without a destination - SC2224,SC2225,SC2226: Warn when using mv/cp/ln without a destination

View File

@ -435,3 +435,12 @@ pseudoGlobIsSuperSetof = matchable
wordsCanBeEqual x y = fromMaybe True $ wordsCanBeEqual x y = fromMaybe True $
liftM2 pseudoGlobsCanOverlap (wordToPseudoGlob x) (wordToPseudoGlob y) liftM2 pseudoGlobsCanOverlap (wordToPseudoGlob x) (wordToPseudoGlob y)
-- Is this an expansion that can be quoted,
-- e.g. $(foo) `foo` $foo (but not {foo,})?
isQuoteableExpansion t = case t of
T_DollarExpansion {} -> True
T_DollarBraceCommandExpansion {} -> True
T_Backticked {} -> True
T_DollarBraced {} -> True
_ -> False

View File

@ -166,6 +166,7 @@ nodeChecks = [
,checkFlagAsCommand ,checkFlagAsCommand
,checkEmptyCondition ,checkEmptyCondition
,checkPipeToNowhere ,checkPipeToNowhere
,checkForLoopGlobVariables
] ]
@ -2908,5 +2909,19 @@ checkUseBeforeDefinition _ t =
then [x] then [x]
else concatMap recursiveSequences list else concatMap recursiveSequences list
prop_checkForLoopGlobVariables1 = verify checkForLoopGlobVariables "for i in $var/*.txt; do true; done"
prop_checkForLoopGlobVariables2 = verifyNot checkForLoopGlobVariables "for i in \"$var\"/*.txt; do true; done"
prop_checkForLoopGlobVariables3 = verifyNot checkForLoopGlobVariables "for i in $var; do true; done"
checkForLoopGlobVariables _ t =
case t of
T_ForIn _ _ words _ -> mapM_ check words
_ -> return ()
where
check (T_NormalWord _ parts) =
when (any isGlob parts) $
mapM_ suggest $ filter isQuoteableExpansion parts
suggest t = info (getId t) 2231
"Quote expansions in this for loop glob to prevent wordsplitting, e.g. \"$dir\"/*.txt ."
return [] return []
runTests = $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |]) runTests = $( [| $(forAllProperties) (quickCheckWithResult (stdArgs { maxSuccess = 1 }) ) |])