Skip to content

Commit 57df440

Browse files
PatternWildCard hint
1 parent 91dbb3e commit 57df440

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

hints.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,38 @@ x
14021402
</tr>
14031403
</table>
14041404

1405+
## Builtin PatternWildCard
1406+
1407+
<table>
1408+
<tr>
1409+
<th>Hint Name</th>
1410+
<th>Hint</th>
1411+
<th>Severity</th>
1412+
</tr>
1413+
<tr>
1414+
<td>Don't use wildcard in pattern match</td>
1415+
<td>
1416+
Example:
1417+
<code>
1418+
case x of { Foo _ -> spam }
1419+
</code>
1420+
<br>
1421+
Found:
1422+
<code>
1423+
_
1424+
</code>
1425+
<br>
1426+
Suggestion:
1427+
<code>
1428+
1429+
</code>
1430+
<br>
1431+
Does not support refactoring.
1432+
</td>
1433+
<td>Ignore</td>
1434+
</tr>
1435+
</table>
1436+
14051437
## Builtin Pragma
14061438

14071439
<table>

hlint.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ library
160160
Hint.Naming
161161
Hint.NewType
162162
Hint.Pattern
163+
Hint.PatternWildCard
163164
Hint.Pragma
164165
Hint.Restrict
165166
Hint.Smell

src/Hint/All.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@ import Hint.Unsafe
3333
import Hint.NewType
3434
import Hint.Smell
3535
import Hint.NumLiteral
36+
import Hint.PatternWildCard
3637

3738
-- | A list of the builtin hints wired into HLint.
3839
-- This list is likely to grow over time.
3940
data HintBuiltin =
4041
HintList | HintListRec | HintMonad | HintLambda | HintFixities |
4142
HintBracket | HintNaming | HintPattern | HintImport | HintExport |
4243
HintPragma | HintExtensions | HintUnsafe | HintDuplicate | HintRestrict |
43-
HintComment | HintNewType | HintSmell | HintNumLiteral
44+
HintComment | HintNewType | HintSmell | HintNumLiteral | HintPatternWildCard
4445
deriving (Show,Eq,Ord,Bounded,Enum)
4546

4647
-- See https://github.com/ndmitchell/hlint/issues/1150 - Duplicate is too slow
@@ -68,6 +69,7 @@ builtin x = case x of
6869
HintMonad -> decl monadHint
6970
HintExtensions -> modu extensionsHint
7071
HintNumLiteral -> decl numLiteralHint
72+
HintPatternWildCard -> decl patternWildCardHint
7173
where
7274
wrap = timed "Hint" (drop 4 $ show x) . forceList
7375
decl f = mempty{hintDecl=const $ \a b c -> wrap $ f a b c}

src/Hint/PatternWildCard.hs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{-
2+
Warn against wildcards in pattern
3+
4+
<TEST>
5+
foo (case x of { Foo _ -> spam }) -- @Ignore ???
6+
case x of { Foo (Spam (Eggs _)) -> spam } -- @Ignore ???
7+
case x of { Foo _ -> spam } -- @Ignore ???
8+
case x of { Foo bar -> spam }
9+
foo (case x of { Foo bar -> spam })
10+
</TEST>
11+
-}
12+
13+
module Hint.PatternWildCard (patternWildCardHint)
14+
where
15+
16+
import Hint.Type (DeclHint, ignoreNoSuggestion, Idea)
17+
import GHC.Hs
18+
import GHC.Types.SrcLoc
19+
import Data.Generics.Uniplate.DataOnly
20+
21+
patternWildCardHint :: DeclHint
22+
patternWildCardHint _ _ code = concatMap inspectCode $ childrenBi code
23+
24+
inspectCode :: LHsExpr GhcPs -> [Idea]
25+
inspectCode (L _ ((HsCase _ _ (MG _ (L _ cases) _)))) = concatMap inspectCase cases
26+
inspectCode o = concatMap inspectCode $ children o
27+
28+
inspectCase :: LMatch GhcPs (LHsExpr GhcPs) -> [Idea]
29+
inspectCase c@(L _ (Match _ _ pats _)) = concatMap inspectPat pats
30+
31+
inspectPat :: LPat GhcPs -> [Idea]
32+
inspectPat c@(L _ (WildPat _)) = [ignoreNoSuggestion "Don't use wildcard in pattern match" (reLoc c)]
33+
inspectPat o = concatMap inspectPat $ children o

0 commit comments

Comments
 (0)