Skip to content

PatternWildCard hint #1473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions hints.md
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,38 @@ x
</tr>
</table>

## Builtin PatternWildCard

<table>
<tr>
<th>Hint Name</th>
<th>Hint</th>
<th>Severity</th>
</tr>
<tr>
<td>Don't use wildcard in pattern match</td>
<td>
Example:
<code>
case x of { Foo _ -> spam }
</code>
<br>
Found:
<code>
_
</code>
<br>
Suggestion:
<code>

</code>
<br>
Does not support refactoring.
</td>
<td>Ignore</td>
</tr>
</table>

## Builtin Pragma

<table>
Expand Down
1 change: 1 addition & 0 deletions hlint.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ library
Hint.Negation
Hint.NewType
Hint.Pattern
Hint.PatternWildCard
Hint.Pragma
Hint.Restrict
Hint.Smell
Expand Down
4 changes: 3 additions & 1 deletion src/Hint/All.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ import Hint.Unsafe
import Hint.NewType
import Hint.Smell
import Hint.NumLiteral
import Hint.PatternWildCard

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

-- See https://github.com/ndmitchell/hlint/issues/1150 - Duplicate is too slow
Expand Down Expand Up @@ -70,6 +71,7 @@ builtin x = case x of
HintMonad -> decl monadHint
HintExtensions -> modu extensionsHint
HintNumLiteral -> decl numLiteralHint
HintPatternWildCard -> decl patternWildCardHint
where
wrap = timed "Hint" (drop 4 $ show x) . forceList
decl f = mempty{hintDecl=const $ \a b c -> wrap $ f a b c}
Expand Down
38 changes: 38 additions & 0 deletions src/Hint/PatternWildCard.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{-# LANGUAGE CPP #-}
{-
Warn against wildcards in pattern

<TEST>
foo (case x of { Foo _ -> spam }) -- @Ignore ???
case x of { Foo (Spam (Eggs _)) -> spam } -- @Ignore ???
case x of { Foo _ -> spam } -- @Ignore ???
case x of { Foo bar -> spam }
foo (case x of { Foo bar -> spam })
</TEST>
-}

module Hint.PatternWildCard (patternWildCardHint)
where

import Hint.Type (DeclHint, ignoreNoSuggestion, Idea)
import GHC.Hs
import GHC.Types.SrcLoc
import Data.Generics.Uniplate.DataOnly

patternWildCardHint :: DeclHint
patternWildCardHint _ _ code = concatMap inspectCode $ childrenBi code

inspectCode :: LHsExpr GhcPs -> [Idea]
#if __GLASGOW_HASKELL__ >= 906
inspectCode (L _ ((HsCase _ _ (MG _ (L _ cases))))) = concatMap inspectCase cases
#else
inspectCode (L _ ((HsCase _ _ (MG _ (L _ cases) _)))) = concatMap inspectCase cases
#endif
inspectCode o = concatMap inspectCode $ children o

inspectCase :: LMatch GhcPs (LHsExpr GhcPs) -> [Idea]
inspectCase c@(L _ (Match _ _ (L _ pats) _)) = concatMap inspectPat pats

inspectPat :: LPat GhcPs -> [Idea]
inspectPat c@(L _ (WildPat _)) = [ignoreNoSuggestion "Don't use wildcard in pattern match" (reLoc c)]
inspectPat o = concatMap inspectPat $ children o