Redundant patterns [GHC-53633]
Flag: -Woverlapping-patterns
Enabled by default
In multi-equation definitions and case expressions, patterns are matched from top to bottom. This means that earlier patterns are tested before later patterns. 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.
Examples
Redundant catchall pattern
Message
Overlapping.hs:6:1: warning: [GHC-53633] [-Woverlapping-patterns]
Pattern match is redundant
In an equation for ‘f’: f _ = ...
|
6 | f _ = False
| ^^^^^^^^^^^
Explanation
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.
Overlapping.hs
module Overlapping where
f [] = True
f (_:_:xs) = f xs
f [_] = False
f _ = False
module Overlapping where
f [] = True
f (_:_:xs) = f xs
f [_] = False
Redundant constructor pattern
Message
Overlapping.hs:5:1: warning: [GHC-53633] [-Woverlapping-patterns]
Pattern match is redundant
In an equation for ‘f’: f (Just _) = ...
|
5 | f (Just _) = False
| ^^^^^^^^^^^^^^^^^^
Explanation
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.
Overlapping.hs
module Overlapping where
f :: Maybe a -> Bool
f (Just _) = True
f (Just _) = False
f _ = False
module Overlapping where
f :: Maybe a -> Bool
f (Just _) = True
f _ = False