haskell / haskell/error-messages
Potentially confusing "Pattern match is redundant" with single alternative case/pattern binding
- Dominant language
- No language data
- Stars
- 76
- Forks
- 19
- PR merge metrics
- No merged PRs in 30d
Description
Compiling the following program with GHC Version 9.2.1 gives a redundant match warning.
```haskell
module Test (patBind, caseMatch) where
patBind :: a
Just patBind = Nothing
caseMatch :: a
caseMatch = case Nothing of
Just x -> x
```
This produces:
```
Test.hs:5:1: warning: [-Woverlapping-patterns]
Pattern match is redundant
In a pattern binding: Just patBind = ...
|
5 | Just patBind = Nothing
| ^^^^^^^^^^^^^^^^^^^^^^
Test.hs:9:3: warning: [-Woverlapping-patterns]
Pattern match is redundant
In a case alternative: Just x -> ...
|
9 | Just x -> x
```
While it is arguably correct to emit a warning for both definitions as they can only produce a pattern match error, I think it is misleading to call the pattern match redundant.
For me, redundant means I can just delete the pattern match entirely. I clearly cannot do that for the `patBind` example, as this is the only definition for it. In `caseMatch`, removing the match yields an empty case, which is not allowed without `EmptyCase`.
In my opinion, the definitions should produce something like the following.
```
Test.hs:5:1: warning: [-Woverlapping-patterns]
Inaccessible pattern binding (?)
The pattern can never match the left hand side expression.
In a pattern binding: Just patBind = ...
|
5 | Just patBind = Nothing
| ^^^^^^^^^^^^^^^^^^^^^^
Test.hs:9:3: warning: [-Woverlapping-patterns]
Inaccessible right hand side.
The pattern can never match the scrutinized expression.
In a case expression: case Nothing of ...
In a case alternative: Just x -> ...
|
9 | Just x -> x
```
On a side note, I did not realize that top-level pattern bindings are actually part of the Haskell standard. I've never encountered them anywhere and never had the urge to use them.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.