gleam icon indicating copy to clipboard operation
gleam copied to clipboard

exhaustiveness: Warn about guards not being considered

Open inoas-nbw opened this issue 1 year ago • 2 comments

For case expressions if there is at least one guard but the expression is inexhaustive, show a warning about guards not contributing/being considered when checking for exhaustiveness.

Example

warning: Inexhaustive patterns
  ┌─ .../code/gleam/exhaust/src/exhaust.gleam:4:3
  │  
4 │ ╭   case 1 {
5 │ │     x if x < 0 -> panic
6 │ │     x if x > 0 -> panic
7 │ │     x if x == 0 -> panic
8 │ │   }
  │ ╰───^

This case expression does not have a pattern for all possible values.
If is run on one of the values without a pattern then it will crash.

The missing patterns are:

    _

In a future version of Gleam this will become a compile error.

Source: https://discord.com/channels/768594524158427167/768594524158427170/1178878959530811502

inoas-nbw avatar Nov 28 '23 09:11 inoas-nbw

We'd need to be careful here. If we introduced a message about guards and the problem is unrelated to guards then it would be very confusing to the user.

lpil avatar Nov 28 '23 10:11 lpil

I don't think it should be a standalone warning. Given that this only makes sense when there is an exhaustiveness warning(error), I think it makes more sense to just explain it in the same exhaustiveness warning(error), of course only when applicable.

massivefermion avatar Jan 08 '24 17:01 massivefermion