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

@ -2,6 +2,9 @@
### Fixed ### Fixed
- `-f diff` no longer claims that it found more issues when it didn't - `-f diff` no longer claims that it found more issues when it didn't
### Added
- SC2254: Suggest quoting expansions in case statements
## v0.7.0 - 2019-07-28 ## v0.7.0 - 2019-07-28
### Added ### Added
- Precompiled binaries for macOS and Linux aarch64 - Precompiled binaries for macOS and Linux aarch64

View File

@ -125,6 +125,7 @@ nodeChecks = [
,checkArithmeticDeref ,checkArithmeticDeref
,checkArithmeticBadOctal ,checkArithmeticBadOctal
,checkComparisonAgainstGlob ,checkComparisonAgainstGlob
,checkCaseAgainstGlob
,checkCommarrays ,checkCommarrays
,checkOrNeq ,checkOrNeq
,checkEchoWc ,checkEchoWc
@ -1338,6 +1339,21 @@ checkComparisonAgainstGlob params (TC_Binary _ SingleBracket op _ word)
checkComparisonAgainstGlob _ _ = return () 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_checkCommarrays1 = verify checkCommarrays "a=(1, 2)"
prop_checkCommarrays2 = verify checkCommarrays "a+=(1,2,3)" prop_checkCommarrays2 = verify checkCommarrays "a+=(1,2,3)"
prop_checkCommarrays3 = verifyNot checkCommarrays "cow=(1 \"foo,bar\" 3)" prop_checkCommarrays3 = verifyNot checkCommarrays "cow=(1 \"foo,bar\" 3)"