Skip to content

Commit f4b5da0

Browse files
Merge pull request #354 from haskellfoundation/GHC-53633
Document GHC-53633, redundant patterns
2 parents 46e6cc1 + 50f639e commit f4b5da0

File tree

7 files changed

+69
-0
lines changed

7 files changed

+69
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
title: Redundant patterns
3+
summary: A pattern is impossible to reach due to earlier patterns
4+
introduced: 9.6.1
5+
severity: warning
6+
flag: -Woverlapping-patterns
7+
---
8+
9+
In multi-equation definitions and case expressions, patterns are matched from top to bottom.
10+
This means that earlier patterns are tested before later patterns.
11+
If every value that a pattern could match has already been ruled out by earlier patterns, then it is unreachable, which typically indicates a bug or misunderstanding.
12+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Overlapping where
2+
3+
f [] = True
4+
f (_:_:xs) = f xs
5+
f [_] = False
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Overlapping where
2+
3+
f [] = True
4+
f (_:_:xs) = f xs
5+
f [_] = False
6+
f _ = False
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: Redundant catchall pattern
3+
---
4+
5+
## Message
6+
```
7+
Overlapping.hs:6:1: warning: [GHC-53633] [-Woverlapping-patterns]
8+
Pattern match is redundant
9+
In an equation for ‘f’: f _ = ...
10+
|
11+
6 | f _ = False
12+
| ^^^^^^^^^^^
13+
```
14+
15+
## Explanation
16+
17+
In this case, all the patterns that can match a list were matched in the prior patterns, so the catch-all pattern in the last case will never be matched and is dead code.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Overlapping where
2+
3+
f :: Maybe a -> Bool
4+
f (Just _) = True
5+
f _ = False
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module Overlapping where
2+
3+
f :: Maybe a -> Bool
4+
f (Just _) = True
5+
f (Just _) = False
6+
f _ = False
7+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: Redundant constructor pattern
3+
---
4+
## Message
5+
6+
```
7+
Overlapping.hs:5:1: warning: [GHC-53633] [-Woverlapping-patterns]
8+
Pattern match is redundant
9+
In an equation for ‘f’: f (Just _) = ...
10+
|
11+
5 | f (Just _) = False
12+
| ^^^^^^^^^^^^^^^^^^
13+
```
14+
15+
## Explanation
16+
17+
The definition of `f` contains two separate patterns that both match `(Just _)`. This usually indicates a bug, because the second one would be unreachable. This can be fixed by removing the redundant case, but it usually indicates that a change to the program was made quickly and demands more thought.

0 commit comments

Comments
 (0)