jfmengels / elm-review-simplify

Provides elm-review rules to simplify your Elm code
https://package.elm-lang.org/packages/jfmengels/elm-review-simplify/latest/
BSD 3-Clause "New" or "Revised" License
20 stars 9 forks source link

Remove unreachable case branches in case ... of #285

Closed lue-bird closed 3 months ago

lue-bird commented 8 months ago

A fresh attempt at implementing https://github.com/jfmengels/elm-review-simplify/issues/99 (slightly less ambitious), having stopped https://github.com/jfmengels/elm-review-simplify/pull/103.

As always, suggested refactors and nitpicks welcome.

Deliberately not included

Allowing all pattern (_) for the last pattern → requires exhaustiveness check! (Because otherwise the case removal could lead to a compiler error) → out of scope?

This means that for example literal char, string, int/hex patterns are not checked for whether they are unnecessary because they require a _ or variable pattern case as the last case.

If you think we should tackle allowing _ patterns some time in the future, leave #99 open, otherwise this PR closes it.

Discussion

There are situations where no cases are "unnecessary" but parts of the pattern and cased expression are because they're always matched (in all examples, the second part in the tuple is unnecessary):

case ( a, () ) of
    ( Something, () ) -> b
    _ -> c
type Blank
    = Blank
case ( a, Blank ) of
    ( Something, Blank ) -> b
    _ -> c
type Thing
    = Thing Int Int Int
case ( a, Thing i0 i1 i2 ) of
    ( Something, Thing _ _ _ ) -> b
    _ -> c
case ( a, b ) of
    ( Something, _ ) -> c
    _ -> d

Should I open an issue to handle those separately or would you like for them to be included in this PR? The last 2 could also fit into no-unused I guess?