Merge branch 'issue_1039_case_pattern_context' of https://github.com/Gandalf-/shellcheck

This commit is contained in:
Vidar Holen 2019-01-01 16:50:59 -08:00
commit 98266a1878
1 changed files with 10 additions and 3 deletions

View File

@ -2750,7 +2750,7 @@ prop_checkUnmatchableCases6 = verifyNot checkUnmatchableCases "case $f in ?*) tr
prop_checkUnmatchableCases7 = verifyNot checkUnmatchableCases "case $f in $(x)) true;; asdf) false;; esac" prop_checkUnmatchableCases7 = verifyNot checkUnmatchableCases "case $f in $(x)) true;; asdf) false;; esac"
prop_checkUnmatchableCases8 = verify checkUnmatchableCases "case $f in cow) true;; bar|cow) false;; esac" prop_checkUnmatchableCases8 = verify checkUnmatchableCases "case $f in cow) true;; bar|cow) false;; esac"
prop_checkUnmatchableCases9 = verifyNot checkUnmatchableCases "case $f in x) true;;& x) false;; esac" prop_checkUnmatchableCases9 = verifyNot checkUnmatchableCases "case $f in x) true;;& x) false;; esac"
checkUnmatchableCases _ t = checkUnmatchableCases params t =
case t of case t of
T_CaseExpression _ word list -> do T_CaseExpression _ word list -> do
-- Check all patterns for whether they can ever match -- Check all patterns for whether they can ever match
@ -2775,6 +2775,7 @@ checkUnmatchableCases _ t =
where where
fst3 (x,_,_) = x fst3 (x,_,_) = x
snd3 (_,x,_) = x snd3 (_,x,_) = x
tp = tokenPositions params
check target candidate = potentially $ do check target candidate = potentially $ do
candidateGlob <- wordToPseudoGlob candidate candidateGlob <- wordToPseudoGlob candidate
guard . not $ pseudoGlobsCanOverlap target candidateGlob guard . not $ pseudoGlobsCanOverlap target candidateGlob
@ -2785,10 +2786,16 @@ checkUnmatchableCases _ t =
checkDoms ((glob, Just x), rest) = checkDoms ((glob, Just x), rest) =
case filter (\(_, p) -> x `pseudoGlobIsSuperSetof` p) valids of case filter (\(_, p) -> x `pseudoGlobIsSuperSetof` p) valids of
((first,_):_) -> do ((first,_):_) -> do
warn (getId glob) 2221 "This pattern always overrides a later one." warn (getId glob) 2221 $ "This pattern always overrides a later one" <> patternContext (getId first)
warn (getId first) 2222 "This pattern never matches because of a previous pattern." warn (getId first) 2222 $ "This pattern never matches because of a previous pattern" <> patternContext (getId glob)
_ -> return () _ -> return ()
where where
patternContext :: Id -> String
patternContext id =
case posLine . fst <$> Map.lookup id tp of
Just l -> " on line " <> show l <> "."
_ -> "."
valids = concatMap f rest valids = concatMap f rest
f (x, Just y) = [(x,y)] f (x, Just y) = [(x,y)]
f _ = [] f _ = []