Detect used types in pattern matching patterns

This commit is contained in:
Jeroen Engels 2019-07-25 00:19:43 +02:00
parent f3fb8c59b9
commit 78562d1de5
2 changed files with 112 additions and 0 deletions

View File

@ -16,6 +16,7 @@ import Elm.Syntax.Expression exposing (Expression(..), Function, FunctionImpleme
import Elm.Syntax.Import exposing (Import)
import Elm.Syntax.Module as Module exposing (Module(..))
import Elm.Syntax.Node as Node exposing (Node)
import Elm.Syntax.Pattern as Pattern exposing (Pattern)
import Elm.Syntax.Range exposing (Range)
import Elm.Syntax.TypeAnnotation exposing (TypeAnnotation(..))
import Lint.Rule as Rule exposing (Direction, Error, Rule)
@ -263,6 +264,20 @@ expressionVisitor node direction context =
( Rule.OnExit, RecordUpdateExpression expr _ ) ->
( [], markAsUsed (Node.value expr) context )
( Rule.OnExit, CaseExpression { cases } ) ->
let
usedVariables : List String
usedVariables =
List.concatMap
(\( patternNode, expressionNode ) ->
getUsedVariablesFromPattern patternNode
)
cases
in
( []
, markAllAsUsed usedVariables context
)
( Rule.OnExit, LetExpression _ ) ->
let
( errors, remainingUsed ) =
@ -279,6 +294,65 @@ expressionVisitor node direction context =
( [], context )
getUsedVariablesFromPattern : Node Pattern -> List String
getUsedVariablesFromPattern patternNode =
case Node.value patternNode of
Pattern.AllPattern ->
[]
Pattern.UnitPattern ->
[]
Pattern.CharPattern _ ->
[]
Pattern.StringPattern _ ->
[]
Pattern.IntPattern _ ->
[]
Pattern.HexPattern _ ->
[]
Pattern.FloatPattern _ ->
[]
Pattern.TuplePattern patterns ->
List.concatMap getUsedVariablesFromPattern patterns
Pattern.RecordPattern _ ->
[]
Pattern.UnConsPattern pattern1 pattern2 ->
List.concatMap getUsedVariablesFromPattern [ pattern1, pattern2 ]
Pattern.ListPattern patterns ->
List.concatMap getUsedVariablesFromPattern patterns
Pattern.VarPattern _ ->
[]
Pattern.NamedPattern qualifiedNameRef patterns ->
let
usedVariable : String
usedVariable =
case qualifiedNameRef.moduleName of
[] ->
qualifiedNameRef.name
moduleName ->
getModuleName moduleName
in
usedVariable :: List.concatMap getUsedVariablesFromPattern patterns
Pattern.AsPattern pattern alias_ ->
getUsedVariablesFromPattern pattern
Pattern.ParenthesizedPattern pattern ->
getUsedVariablesFromPattern pattern
declarationVisitor : Node Declaration -> Direction -> Context -> ( List Error, Context )
declarationVisitor node direction context =
case ( direction, Node.value node ) of

View File

@ -369,6 +369,44 @@ patternMatchingVariablesTests =
a = case thing of
Foo b c -> []"""
|> Lint.Test.expectNoErrors
, test "should not report unused variable when used as the expression in a case expression" <|
\() ->
testRule """module A exposing (a)
b = 1
a =
case b of
_ -> 2"""
|> Lint.Test.expectNoErrors
, test "should not report unused type when it is used in a pattern matching pattern" <|
\() ->
testRule """module A exposing (a)
type Bar = Baz
a =
case () of
Baz ->
[]"""
|> Lint.Test.expectNoErrors
, test "should not report unused type when it is used in a pattern matching pattern (sub-pattern)" <|
\() ->
testRule """module A exposing (a)
type Bar = Baz
a =
case () of
Just (Baz range) ->
[]"""
|> Lint.Test.expectNoErrors
, test "should not report unused import when a type from it is used in a pattern matching pattern" <|
\() ->
testRule """module A exposing (a)
import Bar
a =
case () of
Just (Bar.Baz range) ->
[]"""
|> Lint.Test.expectNoErrors
]