Suggest quoting case patterns, as for SC2053 (fixes #1682)

This commit is contained in:
Vidar Holen 2019-09-08 20:06:06 -07:00
parent 71a4053e8c
commit e01c470598
2 changed files with 19 additions and 0 deletions

View file

@ -125,6 +125,7 @@ nodeChecks = [
,checkArithmeticDeref
,checkArithmeticBadOctal
,checkComparisonAgainstGlob
,checkCaseAgainstGlob
,checkCommarrays
,checkOrNeq
,checkEchoWc
@ -1338,6 +1339,21 @@ checkComparisonAgainstGlob params (TC_Binary _ SingleBracket op _ word)
checkComparisonAgainstGlob _ _ = return ()
prop_checkCaseAgainstGlob1 = verify checkCaseAgainstGlob "case foo in lol$n) foo;; esac"
prop_checkCaseAgainstGlob2 = verify checkCaseAgainstGlob "case foo in $(foo)) foo;; esac"
prop_checkCaseAgainstGlob3 = verifyNot checkCaseAgainstGlob "case foo in *$bar*) foo;; esac"
checkCaseAgainstGlob _ t =
case t of
(T_CaseExpression _ _ cases) -> mapM_ check cases
_ -> return ()
where
check (_, list, _) = mapM_ check' list
check' expr@(T_NormalWord _ list)
-- If it's already a glob, assume that's what the user wanted
| not (isGlob expr) && any isQuoteableExpansion list =
warn (getId expr) 2254 "Quote expansions in case patterns to match literally rather than as a glob."
check' _ = return ()
prop_checkCommarrays1 = verify checkCommarrays "a=(1, 2)"
prop_checkCommarrays2 = verify checkCommarrays "a+=(1,2,3)"
prop_checkCommarrays3 = verifyNot checkCommarrays "cow=(1 \"foo,bar\" 3)"