Forbid using wildcard in pattern matching when it only replaces a single case #91
jfmengels
started this conversation in
Rule ideas
Replies: 2 comments
-
hayleigh on Slack suggested |
Beta Was this translation helpful? Give feedback.
0 replies
-
I'd configure this rule to optionally be more strict about wildcards: -when it only replaces a single case
+when it replaces a finite number of cases So case remoteData of
Success data -> foo
_ -> bar would have to be rewritten to case remoteData of
Success data -> foo
NotAsked -> bar
Loading -> bar
Failure _ -> bar I'm not sure if that's possible though. Do we have access to type information in elm-review? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
What the rule should do:
The rule should report an error when a pattern match contains a wildcard
_
pattern match, when there is only a single case left to handle.What problems does it solve:
Having a wildcard makes it tricky to add new cases to custom type declarations. In this case, having a wildcard adds unnecessary complexity
Example of things the rule would report:
Example of things the rule would not report:
** "Edge-case" **
So one thing where it can be a bit harder to detect, is for cases like the following
We should technically have all the necessary knowledge to know how many cases are covered by
_
, but it will be hard work, especially when there is a lot of nested patterns.Either we do the hard work of computing all of these, or the rule requires that you have a case for every custom type constructor, unless the
_
covers two or more of them.When (not) to enable this rule:
Except for the edge-case I mention above, I think it would always be useful, so always enable? 🤷
I am looking for:
elm-review-common
.( Msg, Model )
?Beta Was this translation helpful? Give feedback.
All reactions