{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE ViewPatterns, PatternGuards, TypeFamilies #-}
module Hint.Pattern(patternHint) where
import Hint.Type(DeclHint,Idea,modComments,firstDeclComments,ideaTo,toSSA,toRefactSrcSpan,suggest,suggestRemove,warn)
import Data.Generics.Uniplate.DataOnly
import Data.Function
import Data.List.Extra
import Data.Tuple
import Data.Maybe
import Data.Either
import Refact.Types hiding (RType(Pattern, Match), SrcSpan)
import Refact.Types qualified as R (RType(Pattern, Match), SrcSpan)
import GHC.Hs
import GHC.Types.SrcLoc
import GHC.Types.Name.Reader
import GHC.Types.Name.Occurrence
import GHC.Data.Bag
import GHC.Types.Basic hiding (Pattern)
import GHC.Data.Strict qualified
import GHC.Util
import Language.Haskell.GhclibParserEx.GHC.Hs.Pat
import Language.Haskell.GhclibParserEx.GHC.Hs.Expr
import Language.Haskell.GhclibParserEx.GHC.Utils.Outputable
import Language.Haskell.GhclibParserEx.GHC.Types.Name.Reader
patternHint :: DeclHint
patternHint :: DeclHint
patternHint Scope
_scope ModuleEx
modu LHsDecl GhcPs
x =
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (String -> Pattern -> [Refactoring SrcSpan] -> Idea)
-> Pattern -> [Idea]
hints forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a, b) -> (b, a)
swap) (LHsDecl GhcPs
-> [(Pattern, String -> Pattern -> [Refactoring SrcSpan] -> Idea)]
asPattern LHsDecl GhcPs
x) forall a. [a] -> [a] -> [a]
++
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Bool -> Bool -> LPat GhcPs -> [Idea]
patHint Bool
strict Bool
False) [LPat GhcPs
p | PatBind XPatBind GhcPs GhcPs
_ LPat GhcPs
p GRHSs GhcPs (LHsExpr GhcPs)
_ <- forall from to. Biplate from to => from -> [to]
universeBi LHsDecl GhcPs
x :: [HsBind GhcPs]] forall a. [a] -> [a] -> [a]
++
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Bool -> Bool -> LPat GhcPs -> [Idea]
patHint Bool
strict Bool
True) (forall from to. Biplate from to => from -> [to]
universeBi forall a b. (a -> b) -> a -> b
$ forall from to. Biplate from to => (to -> to) -> from -> from
transformBi LHsBind GhcPs -> LHsBind GhcPs
noPatBind LHsDecl GhcPs
x) forall a. [a] -> [a] -> [a]
++
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap LHsExpr GhcPs -> [Idea]
expHint (forall from to. Biplate from to => from -> [to]
universeBi LHsDecl GhcPs
x)
where
exts :: [String]
exts = forall a. Ord a => [a] -> [a]
nubOrd forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap forall a b. (a, b) -> b
snd ([(LEpaComment, String)] -> [(LEpaComment, [String])]
languagePragmas (EpAnnComments -> [(LEpaComment, String)]
pragmas (ModuleEx -> EpAnnComments
modComments ModuleEx
modu) forall a. [a] -> [a] -> [a]
++ EpAnnComments -> [(LEpaComment, String)]
pragmas (ModuleEx -> EpAnnComments
firstDeclComments ModuleEx
modu)))
strict :: Bool
strict = String
"Strict" forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String]
exts
noPatBind :: LHsBind GhcPs -> LHsBind GhcPs
noPatBind :: LHsBind GhcPs -> LHsBind GhcPs
noPatBind (L SrcSpanAnnA
loc a :: HsBind GhcPs
a@PatBind{}) = forall l e. l -> e -> GenLocated l e
L SrcSpanAnnA
loc HsBind GhcPs
a{pat_lhs :: LPat GhcPs
pat_lhs=forall a an. a -> LocatedAn an a
noLocA (forall p. XWildPat p -> Pat p
WildPat NoExtField
noExtField)}
noPatBind LHsBind GhcPs
x = LHsBind GhcPs
x
hints :: (String -> Pattern -> [Refactoring R.SrcSpan] -> Idea) -> Pattern -> [Idea]
hints :: (String -> Pattern -> [Refactoring SrcSpan] -> Idea)
-> Pattern -> [Idea]
hints String -> Pattern -> [Refactoring SrcSpan] -> Idea
gen (Pattern SrcSpan
l RType
rtype [LPat GhcPs]
pat (GRHSs XCGRHSs GhcPs (LHsExpr GhcPs)
_ [L SrcAnn NoEpAnns
_ (GRHS XCGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
_ [] GenLocated SrcSpanAnnA (HsExpr GhcPs)
bod)] HsLocalBinds GhcPs
bind))
| forall (t :: * -> *) a. Foldable t => t a -> Int
length [LGRHS GhcPs (LHsExpr GhcPs)]
guards forall a. Ord a => a -> a -> Bool
> Int
2 = [String -> Pattern -> [Refactoring SrcSpan] -> Idea
gen String
"Use guards" (SrcSpan
-> RType -> [LPat GhcPs] -> GRHSs GhcPs (LHsExpr GhcPs) -> Pattern
Pattern SrcSpan
l RType
rtype [LPat GhcPs]
pat (forall p body.
XCGRHSs p body -> [LGRHS p body] -> HsLocalBinds p -> GRHSs p body
GRHSs EpAnnComments
emptyComments [LGRHS GhcPs (LHsExpr GhcPs)]
guards HsLocalBinds GhcPs
bind)) [Refactoring SrcSpan
refactoring]]
where
rawGuards :: [(LHsExpr GhcPs, LHsExpr GhcPs)]
rawGuards :: [(LHsExpr GhcPs, LHsExpr GhcPs)]
rawGuards = LHsExpr GhcPs -> [(LHsExpr GhcPs, LHsExpr GhcPs)]
asGuards GenLocated SrcSpanAnnA (HsExpr GhcPs)
bod
mkGuard :: LHsExpr GhcPs -> (LHsExpr GhcPs -> GRHS GhcPs (LHsExpr GhcPs))
mkGuard :: LHsExpr GhcPs -> LHsExpr GhcPs -> GRHS GhcPs (LHsExpr GhcPs)
mkGuard LHsExpr GhcPs
a = forall p body.
XCGRHS p body -> [GuardLStmt p] -> body -> GRHS p body
GRHS forall ann. EpAnn ann
EpAnnNotUsed [forall a an. a -> LocatedAn an a
noLocA forall a b. (a -> b) -> a -> b
$ forall idL idR body.
XBodyStmt idL idR body
-> body -> SyntaxExpr idR -> SyntaxExpr idR -> StmtLR idL idR body
BodyStmt NoExtField
noExtField LHsExpr GhcPs
a forall (p :: Pass). IsPass p => SyntaxExpr (GhcPass p)
noSyntaxExpr forall (p :: Pass). IsPass p => SyntaxExpr (GhcPass p)
noSyntaxExpr]
guards :: [LGRHS GhcPs (LHsExpr GhcPs)]
guards :: [LGRHS GhcPs (LHsExpr GhcPs)]
guards = forall a b. (a -> b) -> [a] -> [b]
map (forall a an. a -> LocatedAn an a
noLocA forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry LHsExpr GhcPs -> LHsExpr GhcPs -> GRHS GhcPs (LHsExpr GhcPs)
mkGuard) [(LHsExpr GhcPs, LHsExpr GhcPs)]
rawGuards
([GenLocated SrcSpanAnnA (HsExpr GhcPs)]
lhs, [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
rhs) = forall a b. [(a, b)] -> ([a], [b])
unzip [(LHsExpr GhcPs, LHsExpr GhcPs)]
rawGuards
mkTemplate :: String
-> [GenLocated (SrcSpanAnn' a) e]
-> [Either (GenLocated (SrcSpanAnn' a) e) (String, SrcSpan)]
mkTemplate String
c [GenLocated (SrcSpanAnn' a) e]
ps =
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith GenLocated (SrcSpanAnn' a) e
-> Char -> Either (GenLocated (SrcSpanAnn' a) e) (String, SrcSpan)
checkLoc [GenLocated (SrcSpanAnn' a) e]
ps [Char
'1' .. Char
'9']
where
checkLoc :: GenLocated (SrcSpanAnn' a) e
-> Char -> Either (GenLocated (SrcSpanAnn' a) e) (String, SrcSpan)
checkLoc p :: GenLocated (SrcSpanAnn' a) e
p@(L SrcSpanAnn' a
l e
_) Char
v = if forall a. SrcSpanAnn' a -> SrcSpan
locA SrcSpanAnn' a
l forall a. Eq a => a -> a -> Bool
== SrcSpan
noSrcSpan then forall a b. a -> Either a b
Left GenLocated (SrcSpanAnn' a) e
p else forall a b. b -> Either a b
Right (String
c forall a. [a] -> [a] -> [a]
++ [Char
v], forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
toSSA GenLocated (SrcSpanAnn' a) e
p)
patSubts :: [Either (GenLocated SrcSpanAnnA (Pat GhcPs)) (String, SrcSpan)]
patSubts =
case [LPat GhcPs]
pat of
[LPat GhcPs
p] -> [forall a b. a -> Either a b
Left LPat GhcPs
p]
[LPat GhcPs]
ps -> forall {a} {e}.
String
-> [GenLocated (SrcSpanAnn' a) e]
-> [Either (GenLocated (SrcSpanAnn' a) e) (String, SrcSpan)]
mkTemplate String
"p100" [LPat GhcPs]
ps
guardSubts :: [Either (GenLocated SrcSpanAnnA (HsExpr GhcPs)) (String, SrcSpan)]
guardSubts = forall {a} {e}.
String
-> [GenLocated (SrcSpanAnn' a) e]
-> [Either (GenLocated (SrcSpanAnn' a) e) (String, SrcSpan)]
mkTemplate String
"g100" [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
lhs
exprSubts :: [Either (GenLocated SrcSpanAnnA (HsExpr GhcPs)) (String, SrcSpan)]
exprSubts = forall {a} {e}.
String
-> [GenLocated (SrcSpanAnn' a) e]
-> [Either (GenLocated (SrcSpanAnn' a) e) (String, SrcSpan)]
mkTemplate String
"e100" [GenLocated SrcSpanAnnA (HsExpr GhcPs)]
rhs
templateGuards :: [LocatedAn an (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
templateGuards = forall a b. (a -> b) -> [a] -> [b]
map forall a an. a -> LocatedAn an a
noLocA (forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (LHsExpr GhcPs -> LHsExpr GhcPs -> GRHS GhcPs (LHsExpr GhcPs)
mkGuard forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` forall {b}.
Either (GenLocated SrcSpanAnnA (HsExpr GhcPs)) (String, b)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
toString) [Either (GenLocated SrcSpanAnnA (HsExpr GhcPs)) (String, SrcSpan)]
guardSubts [Either (GenLocated SrcSpanAnnA (HsExpr GhcPs)) (String, SrcSpan)]
exprSubts)
toString :: Either (GenLocated SrcSpanAnnA (HsExpr GhcPs)) (String, b)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
toString (Left GenLocated SrcSpanAnnA (HsExpr GhcPs)
e) = GenLocated SrcSpanAnnA (HsExpr GhcPs)
e
toString (Right (String
v, b
_)) = String -> LHsExpr GhcPs
strToVar String
v
toString' :: Either (GenLocated SrcSpanAnnA (Pat GhcPs)) (String, b)
-> GenLocated SrcSpanAnnA (Pat GhcPs)
toString' (Left GenLocated SrcSpanAnnA (Pat GhcPs)
e) = GenLocated SrcSpanAnnA (Pat GhcPs)
e
toString' (Right (String
v, b
_)) = String -> LPat GhcPs
strToPat String
v
template :: String
template = forall a. a -> Maybe a -> a
fromMaybe String
"" forall a b. (a -> b) -> a -> b
$ Idea -> Maybe String
ideaTo (String -> Pattern -> [Refactoring SrcSpan] -> Idea
gen String
"" (SrcSpan
-> RType -> [LPat GhcPs] -> GRHSs GhcPs (LHsExpr GhcPs) -> Pattern
Pattern SrcSpan
l RType
rtype (forall a b. (a -> b) -> [a] -> [b]
map forall {b}.
Either (GenLocated SrcSpanAnnA (Pat GhcPs)) (String, b)
-> GenLocated SrcSpanAnnA (Pat GhcPs)
toString' [Either (GenLocated SrcSpanAnnA (Pat GhcPs)) (String, SrcSpan)]
patSubts) (forall p body.
XCGRHSs p body -> [LGRHS p body] -> HsLocalBinds p -> GRHSs p body
GRHSs EpAnnComments
emptyComments forall {an}.
[LocatedAn an (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
templateGuards HsLocalBinds GhcPs
bind)) [])
f :: [Either a (String, R.SrcSpan)] -> [(String, R.SrcSpan)]
f :: forall a. [Either a (String, SrcSpan)] -> [(String, SrcSpan)]
f = forall a b. [Either a b] -> [b]
rights
refactoring :: Refactoring SrcSpan
refactoring = forall a. RType -> a -> [(String, a)] -> String -> Refactoring a
Replace RType
rtype (SrcSpan -> SrcSpan
toRefactSrcSpan SrcSpan
l) (forall a. [Either a (String, SrcSpan)] -> [(String, SrcSpan)]
f [Either (GenLocated SrcSpanAnnA (Pat GhcPs)) (String, SrcSpan)]
patSubts forall a. [a] -> [a] -> [a]
++ forall a. [Either a (String, SrcSpan)] -> [(String, SrcSpan)]
f [Either (GenLocated SrcSpanAnnA (HsExpr GhcPs)) (String, SrcSpan)]
guardSubts forall a. [a] -> [a] -> [a]
++ forall a. [Either a (String, SrcSpan)] -> [(String, SrcSpan)]
f [Either (GenLocated SrcSpanAnnA (HsExpr GhcPs)) (String, SrcSpan)]
exprSubts) String
template
hints String -> Pattern -> [Refactoring SrcSpan] -> Idea
gen (Pattern SrcSpan
l RType
t [LPat GhcPs]
pats o :: GRHSs GhcPs (LHsExpr GhcPs)
o@(GRHSs XCGRHSs GhcPs (LHsExpr GhcPs)
_ [L SrcAnn NoEpAnns
_ (GRHS XCGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
_ [GuardLStmt GhcPs
test] GenLocated SrcSpanAnnA (HsExpr GhcPs)
bod)] HsLocalBinds GhcPs
bind))
| forall a. Outputable a => a -> String
unsafePrettyPrint GuardLStmt GhcPs
test forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String
"otherwise", String
"True"]
= [String -> Pattern -> [Refactoring SrcSpan] -> Idea
gen String
"Redundant guard" (SrcSpan
-> RType -> [LPat GhcPs] -> GRHSs GhcPs (LHsExpr GhcPs) -> Pattern
Pattern SrcSpan
l RType
t [LPat GhcPs]
pats GRHSs GhcPs (LHsExpr GhcPs)
o{grhssGRHSs :: [LGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))]
grhssGRHSs=[forall a an. a -> LocatedAn an a
noLocA (forall p body.
XCGRHS p body -> [GuardLStmt p] -> body -> GRHS p body
GRHS forall ann. EpAnn ann
EpAnnNotUsed [] GenLocated SrcSpanAnnA (HsExpr GhcPs)
bod)]}) [forall a. RType -> a -> Refactoring a
Delete RType
Stmt (forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
toSSA GuardLStmt GhcPs
test)]]
hints String -> Pattern -> [Refactoring SrcSpan] -> Idea
_ (Pattern SrcSpan
l RType
t [LPat GhcPs]
pats bod :: GRHSs GhcPs (LHsExpr GhcPs)
bod@(GRHSs XCGRHSs GhcPs (LHsExpr GhcPs)
_ [LGRHS GhcPs (LHsExpr GhcPs)]
_ HsLocalBinds GhcPs
binds)) | HsLocalBinds GhcPs -> Bool
f HsLocalBinds GhcPs
binds
= [String -> SrcSpan -> String -> [Refactoring SrcSpan] -> Idea
suggestRemove String
"Redundant where" SrcSpan
whereSpan String
"where" [ ]]
where
f :: HsLocalBinds GhcPs -> Bool
f :: HsLocalBinds GhcPs -> Bool
f (HsValBinds XHsValBinds GhcPs GhcPs
_ (ValBinds XValBinds GhcPs GhcPs
_ LHsBindsLR GhcPs GhcPs
bag [LSig GhcPs]
_)) = forall a. Bag a -> Bool
isEmptyBag LHsBindsLR GhcPs GhcPs
bag
f (HsIPBinds XHsIPBinds GhcPs GhcPs
_ (IPBinds XIPBinds GhcPs
_ [LIPBind GhcPs]
l)) = forall (t :: * -> *) a. Foldable t => t a -> Bool
null [LIPBind GhcPs]
l
f HsLocalBinds GhcPs
_ = Bool
False
whereSpan :: SrcSpan
whereSpan = case SrcSpan
l of
UnhelpfulSpan UnhelpfulSpanReason
s -> UnhelpfulSpanReason -> SrcSpan
UnhelpfulSpan UnhelpfulSpanReason
s
RealSrcSpan RealSrcSpan
s Maybe BufSpan
_ ->
let end :: RealSrcLoc
end = RealSrcSpan -> RealSrcLoc
realSrcSpanEnd RealSrcSpan
s
start :: RealSrcLoc
start = FastString -> Int -> Int -> RealSrcLoc
mkRealSrcLoc (RealSrcSpan -> FastString
srcSpanFile RealSrcSpan
s) (RealSrcLoc -> Int
srcLocLine RealSrcLoc
end) (RealSrcLoc -> Int
srcLocCol RealSrcLoc
end forall a. Num a => a -> a -> a
- Int
5)
in RealSrcSpan -> Maybe BufSpan -> SrcSpan
RealSrcSpan (RealSrcLoc -> RealSrcLoc -> RealSrcSpan
mkRealSrcSpan RealSrcLoc
start RealSrcLoc
end) forall a. Maybe a
GHC.Data.Strict.Nothing
hints String -> Pattern -> [Refactoring SrcSpan] -> Idea
gen (Pattern SrcSpan
l RType
t [LPat GhcPs]
pats o :: GRHSs GhcPs (LHsExpr GhcPs)
o@(GRHSs XCGRHSs GhcPs (LHsExpr GhcPs)
_ (forall a. [a] -> Maybe ([a], a)
unsnoc -> Just ([GenLocated
(SrcAnn NoEpAnns)
(GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
gs, L SrcAnn NoEpAnns
_ (GRHS XCGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
_ [GuardLStmt GhcPs
test] GenLocated SrcSpanAnnA (HsExpr GhcPs)
bod))) HsLocalBinds GhcPs
binds))
| forall a. Outputable a => a -> String
unsafePrettyPrint GuardLStmt GhcPs
test forall a. Eq a => a -> a -> Bool
== String
"True"
= let otherwise_ :: LocatedAn
an (StmtLR GhcPs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
otherwise_ = forall a an. a -> LocatedAn an a
noLocA forall a b. (a -> b) -> a -> b
$ forall idL idR body.
XBodyStmt idL idR body
-> body -> SyntaxExpr idR -> SyntaxExpr idR -> StmtLR idL idR body
BodyStmt NoExtField
noExtField (String -> LHsExpr GhcPs
strToVar String
"otherwise") forall (p :: Pass). IsPass p => SyntaxExpr (GhcPass p)
noSyntaxExpr forall (p :: Pass). IsPass p => SyntaxExpr (GhcPass p)
noSyntaxExpr in
[String -> Pattern -> [Refactoring SrcSpan] -> Idea
gen String
"Use otherwise" (SrcSpan
-> RType -> [LPat GhcPs] -> GRHSs GhcPs (LHsExpr GhcPs) -> Pattern
Pattern SrcSpan
l RType
t [LPat GhcPs]
pats GRHSs GhcPs (LHsExpr GhcPs)
o{grhssGRHSs :: [LGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))]
grhssGRHSs = [GenLocated
(SrcAnn NoEpAnns)
(GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
gs forall a. [a] -> [a] -> [a]
++ [forall a an. a -> LocatedAn an a
noLocA (forall p body.
XCGRHS p body -> [GuardLStmt p] -> body -> GRHS p body
GRHS forall ann. EpAnn ann
EpAnnNotUsed [forall {an}.
LocatedAn
an (StmtLR GhcPs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))
otherwise_] GenLocated SrcSpanAnnA (HsExpr GhcPs)
bod)]}) [forall a. RType -> a -> [(String, a)] -> String -> Refactoring a
Replace RType
Expr (forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
toSSA GuardLStmt GhcPs
test) [] String
"otherwise"]]
hints String -> Pattern -> [Refactoring SrcSpan] -> Idea
_ Pattern
_ = []
asGuards :: LHsExpr GhcPs -> [(LHsExpr GhcPs, LHsExpr GhcPs)]
asGuards :: LHsExpr GhcPs -> [(LHsExpr GhcPs, LHsExpr GhcPs)]
asGuards (L SrcSpanAnnA
_ (HsPar XPar GhcPs
_ LHsToken "(" GhcPs
_ LHsExpr GhcPs
x LHsToken ")" GhcPs
_)) = LHsExpr GhcPs -> [(LHsExpr GhcPs, LHsExpr GhcPs)]
asGuards LHsExpr GhcPs
x
asGuards (L SrcSpanAnnA
_ (HsIf XIf GhcPs
_ LHsExpr GhcPs
a LHsExpr GhcPs
b LHsExpr GhcPs
c)) = (LHsExpr GhcPs
a, LHsExpr GhcPs
b) forall a. a -> [a] -> [a]
: LHsExpr GhcPs -> [(LHsExpr GhcPs, LHsExpr GhcPs)]
asGuards LHsExpr GhcPs
c
asGuards LHsExpr GhcPs
x = [(String -> LHsExpr GhcPs
strToVar String
"otherwise", LHsExpr GhcPs
x)]
data Pattern = Pattern SrcSpan R.RType [LPat GhcPs] (GRHSs GhcPs (LHsExpr GhcPs))
asPattern :: LHsDecl GhcPs -> [(Pattern, String -> Pattern -> [Refactoring R.SrcSpan] -> Idea)]
asPattern :: LHsDecl GhcPs
-> [(Pattern, String -> Pattern -> [Refactoring SrcSpan] -> Idea)]
asPattern (L SrcSpanAnnA
loc HsDecl GhcPs
x) = forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap HsBind GhcPs
-> [(Pattern, String -> Pattern -> [Refactoring SrcSpan] -> Idea)]
decl (forall from to. Biplate from to => from -> [to]
universeBi HsDecl GhcPs
x)
where
decl :: HsBind GhcPs -> [(Pattern, String -> Pattern -> [Refactoring R.SrcSpan] -> Idea)]
decl :: HsBind GhcPs
-> [(Pattern, String -> Pattern -> [Refactoring SrcSpan] -> Idea)]
decl o :: HsBind GhcPs
o@(PatBind XPatBind GhcPs GhcPs
_ LPat GhcPs
pat GRHSs GhcPs (LHsExpr GhcPs)
rhs) = [(SrcSpan
-> RType -> [LPat GhcPs] -> GRHSs GhcPs (LHsExpr GhcPs) -> Pattern
Pattern (forall a. SrcSpanAnn' a -> SrcSpan
locA SrcSpanAnnA
loc) RType
Bind [LPat GhcPs
pat] GRHSs GhcPs (LHsExpr GhcPs)
rhs, \String
msg (Pattern SrcSpan
_ RType
_ [LPat GhcPs
pat] GRHSs GhcPs (LHsExpr GhcPs)
rhs) [Refactoring SrcSpan]
rs -> forall a b.
(Outputable a, Outputable b) =>
String -> Located a -> Located b -> [Refactoring SrcSpan] -> Idea
suggest String
msg (forall e. e -> Located e
noLoc HsBind GhcPs
o :: Located (HsBind GhcPs)) (forall e. e -> Located e
noLoc (forall idL idR.
XPatBind idL idR
-> LPat idL -> GRHSs idR (LHsExpr idR) -> HsBindLR idL idR
PatBind forall ann. EpAnn ann
EpAnnNotUsed LPat GhcPs
pat GRHSs GhcPs (LHsExpr GhcPs)
rhs) :: Located (HsBind GhcPs)) [Refactoring SrcSpan]
rs)]
decl (FunBind XFunBind GhcPs GhcPs
_ LIdP GhcPs
_ (MG XMG GhcPs (LHsExpr GhcPs)
_ (L SrcSpanAnnL
_ [GenLocated
SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
xs))) = forall a b. (a -> b) -> [a] -> [b]
map LMatch GhcPs (LHsExpr GhcPs)
-> (Pattern, String -> Pattern -> [Refactoring SrcSpan] -> Idea)
match [GenLocated
SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
xs
decl HsBind GhcPs
_ = []
match :: LMatch GhcPs (LHsExpr GhcPs) -> (Pattern, String -> Pattern -> [Refactoring R.SrcSpan] -> Idea)
match :: LMatch GhcPs (LHsExpr GhcPs)
-> (Pattern, String -> Pattern -> [Refactoring SrcSpan] -> Idea)
match o :: LMatch GhcPs (LHsExpr GhcPs)
o@(L SrcSpanAnnA
loc (Match XCMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
_ HsMatchContext GhcPs
ctx [LPat GhcPs]
pats GRHSs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
grhss)) = (SrcSpan
-> RType -> [LPat GhcPs] -> GRHSs GhcPs (LHsExpr GhcPs) -> Pattern
Pattern (forall a. SrcSpanAnn' a -> SrcSpan
locA SrcSpanAnnA
loc) RType
R.Match [LPat GhcPs]
pats GRHSs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
grhss, \String
msg (Pattern SrcSpan
_ RType
_ [LPat GhcPs]
pats GRHSs GhcPs (LHsExpr GhcPs)
grhss) [Refactoring SrcSpan]
rs -> forall a b.
(Outputable a, Outputable b) =>
String -> Located a -> Located b -> [Refactoring SrcSpan] -> Idea
suggest String
msg (forall a e. LocatedAn a e -> Located e
reLoc LMatch GhcPs (LHsExpr GhcPs)
o) (forall e. e -> Located e
noLoc (forall p body.
XCMatch p body
-> HsMatchContext p -> [LPat p] -> GRHSs p body -> Match p body
Match forall ann. EpAnn ann
EpAnnNotUsed HsMatchContext GhcPs
ctx [LPat GhcPs]
pats GRHSs GhcPs (LHsExpr GhcPs)
grhss) :: Located (Match GhcPs (LHsExpr GhcPs))) [Refactoring SrcSpan]
rs)
patHint :: Bool -> Bool -> LPat GhcPs -> [Idea]
patHint :: Bool -> Bool -> LPat GhcPs -> [Idea]
patHint Bool
_ Bool
_ o :: LPat GhcPs
o@(L SrcSpanAnnA
_ (ConPat XConPat GhcPs
_ XRec GhcPs (ConLikeP GhcPs)
name (PrefixCon [HsConPatTyArg (NoGhcTc GhcPs)]
_ [LPat GhcPs]
args)))
| forall (t :: * -> *) a. Foldable t => t a -> Int
length [LPat GhcPs]
args forall a. Ord a => a -> a -> Bool
>= Int
3 Bool -> Bool -> Bool
&& forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all LPat GhcPs -> Bool
isPWildcard [LPat GhcPs]
args =
let rec_fields :: HsRecFields GhcPs (LPat GhcPs)
rec_fields = forall p arg.
[LHsRecField p arg]
-> Maybe (XRec p RecFieldsDotDot) -> HsRecFields p arg
HsRecFields [] forall a. Maybe a
Nothing :: HsRecFields GhcPs (LPat GhcPs)
new :: LPat GhcPs
new = forall a an. a -> LocatedAn an a
noLocA forall a b. (a -> b) -> a -> b
$ forall p.
XConPat p -> XRec p (ConLikeP p) -> HsConPatDetails p -> Pat p
ConPat forall ann. EpAnn ann
EpAnnNotUsed XRec GhcPs (ConLikeP GhcPs)
name (forall tyarg arg rec. rec -> HsConDetails tyarg arg rec
RecCon HsRecFields GhcPs (GenLocated SrcSpanAnnA (Pat GhcPs))
rec_fields) :: LPat GhcPs
in
[forall a b.
(Outputable a, Outputable b) =>
String -> Located a -> Located b -> [Refactoring SrcSpan] -> Idea
suggest String
"Use record patterns" (forall a e. LocatedAn a e -> Located e
reLoc LPat GhcPs
o) (forall a e. LocatedAn a e -> Located e
reLoc LPat GhcPs
new) [forall a. RType -> a -> [(String, a)] -> String -> Refactoring a
Replace RType
R.Pattern (forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
toSSA LPat GhcPs
o) [] (forall a. Outputable a => a -> String
unsafePrettyPrint LPat GhcPs
new)]]
patHint Bool
_ Bool
_ o :: LPat GhcPs
o@(L SrcSpanAnnA
_ (VarPat XVarPat GhcPs
_ (L SrcSpanAnnN
_ RdrName
name)))
| OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc RdrName
name) forall a. Eq a => a -> a -> Bool
== String
"otherwise" =
[forall a b.
(Outputable a, Outputable b) =>
String -> Located a -> Located b -> [Refactoring SrcSpan] -> Idea
warn String
"Used otherwise as a pattern" (forall a e. LocatedAn a e -> Located e
reLoc LPat GhcPs
o) (forall e. e -> Located e
noLoc (forall p. XWildPat p -> Pat p
WildPat NoExtField
noExtField) :: Located (Pat GhcPs)) []]
patHint Bool
lang Bool
strict o :: LPat GhcPs
o@(L SrcSpanAnnA
_ (BangPat XBangPat GhcPs
_ pat :: LPat GhcPs
pat@(L SrcSpanAnnA
_ Pat GhcPs
x)))
| Bool
strict, Pat GhcPs -> Bool
f Pat GhcPs
x = [forall a b.
(Outputable a, Outputable b) =>
String -> Located a -> Located b -> [Refactoring SrcSpan] -> Idea
warn String
"Redundant bang pattern" (forall a e. LocatedAn a e -> Located e
reLoc LPat GhcPs
o) (forall e. e -> Located e
noLoc Pat GhcPs
x :: Located (Pat GhcPs)) [Refactoring SrcSpan
r]]
where
f :: Pat GhcPs -> Bool
f :: Pat GhcPs -> Bool
f (ParPat XParPat GhcPs
_ LHsToken "(" GhcPs
_ (L SrcSpanAnnA
_ Pat GhcPs
x) LHsToken ")" GhcPs
_) = Pat GhcPs -> Bool
f Pat GhcPs
x
f (AsPat XAsPat GhcPs
_ LIdP GhcPs
_ LHsToken "@" GhcPs
_ (L SrcSpanAnnA
_ Pat GhcPs
x)) = Pat GhcPs -> Bool
f Pat GhcPs
x
f LitPat {} = Bool
True
f NPat {} = Bool
True
f ConPat {} = Bool
True
f TuplePat {} = Bool
True
f ListPat {} = Bool
True
f (SigPat XSigPat GhcPs
_ (L SrcSpanAnnA
_ Pat GhcPs
p) HsPatSigType (NoGhcTc GhcPs)
_) = Pat GhcPs -> Bool
f Pat GhcPs
p
f Pat GhcPs
_ = Bool
False
r :: Refactoring SrcSpan
r = forall a. RType -> a -> [(String, a)] -> String -> Refactoring a
Replace RType
R.Pattern (forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
toSSA LPat GhcPs
o) [(String
"x", forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
toSSA LPat GhcPs
pat)] String
"x"
patHint Bool
False Bool
_ o :: LPat GhcPs
o@(L SrcSpanAnnA
_ (LazyPat XLazyPat GhcPs
_ pat :: LPat GhcPs
pat@(L SrcSpanAnnA
_ Pat GhcPs
x)))
| Pat GhcPs -> Bool
f Pat GhcPs
x = [forall a b.
(Outputable a, Outputable b) =>
String -> Located a -> Located b -> [Refactoring SrcSpan] -> Idea
warn String
"Redundant irrefutable pattern" (forall a e. LocatedAn a e -> Located e
reLoc LPat GhcPs
o) (forall e. e -> Located e
noLoc Pat GhcPs
x :: Located (Pat GhcPs)) [Refactoring SrcSpan
r]]
where
f :: Pat GhcPs -> Bool
f :: Pat GhcPs -> Bool
f (ParPat XParPat GhcPs
_ LHsToken "(" GhcPs
_ (L SrcSpanAnnA
_ Pat GhcPs
x) LHsToken ")" GhcPs
_) = Pat GhcPs -> Bool
f Pat GhcPs
x
f (AsPat XAsPat GhcPs
_ LIdP GhcPs
_ LHsToken "@" GhcPs
_ (L SrcSpanAnnA
_ Pat GhcPs
x)) = Pat GhcPs -> Bool
f Pat GhcPs
x
f WildPat{} = Bool
True
f VarPat{} = Bool
True
f Pat GhcPs
_ = Bool
False
r :: Refactoring SrcSpan
r = forall a. RType -> a -> [(String, a)] -> String -> Refactoring a
Replace RType
R.Pattern (forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
toSSA LPat GhcPs
o) [(String
"x", forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
toSSA LPat GhcPs
pat)] String
"x"
patHint Bool
_ Bool
_ o :: LPat GhcPs
o@(L SrcSpanAnnA
_ (AsPat XAsPat GhcPs
_ LIdP GhcPs
v LHsToken "@" GhcPs
_ (L SrcSpanAnnA
_ (WildPat XWildPat GhcPs
_)))) =
[forall a b.
(Outputable a, Outputable b) =>
String -> Located a -> Located b -> [Refactoring SrcSpan] -> Idea
warn String
"Redundant as-pattern" (forall a e. LocatedAn a e -> Located e
reLoc LPat GhcPs
o) (forall a e. LocatedAn a e -> Located e
reLoc LIdP GhcPs
v) [forall a. RType -> a -> [(String, a)] -> String -> Refactoring a
Replace RType
R.Pattern (forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
toSSA LPat GhcPs
o) [] (GenLocated SrcSpanAnnN RdrName -> String
rdrNameStr LIdP GhcPs
v)]]
patHint Bool
_ Bool
_ LPat GhcPs
_ = []
expHint :: LHsExpr GhcPs -> [Idea]
expHint :: LHsExpr GhcPs -> [Idea]
expHint o :: LHsExpr GhcPs
o@(L SrcSpanAnnA
_ (HsCase XCase GhcPs
_ LHsExpr GhcPs
_ (MG Origin
XMG GhcPs (LHsExpr GhcPs)
FromSource (L SrcSpanAnnL
_ [L SrcSpanAnnA
_ (Match XCMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
_ HsMatchContext GhcPs
CaseAlt [L SrcSpanAnnA
_ (WildPat XWildPat GhcPs
_)] (GRHSs XCGRHSs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
_ [L SrcAnn NoEpAnns
_ (GRHS XCGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
_ [] GenLocated SrcSpanAnnA (HsExpr GhcPs)
e)] (EmptyLocalBinds XEmptyLocalBinds GhcPs GhcPs
_))) ])))) =
[forall a b.
(Outputable a, Outputable b) =>
String -> Located a -> Located b -> [Refactoring SrcSpan] -> Idea
suggest String
"Redundant case" (forall a e. LocatedAn a e -> Located e
reLoc LHsExpr GhcPs
o) (forall a e. LocatedAn a e -> Located e
reLoc GenLocated SrcSpanAnnA (HsExpr GhcPs)
e) [Refactoring SrcSpan
r]]
where
r :: Refactoring SrcSpan
r = forall a. RType -> a -> [(String, a)] -> String -> Refactoring a
Replace RType
Expr (forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
toSSA LHsExpr GhcPs
o) [(String
"x", forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
toSSA GenLocated SrcSpanAnnA (HsExpr GhcPs)
e)] String
"x"
expHint o :: LHsExpr GhcPs
o@(L SrcSpanAnnA
_ (HsCase XCase GhcPs
_ (L SrcSpanAnnA
_ (HsVar XVar GhcPs
_ (L SrcSpanAnnN
_ RdrName
x))) (MG Origin
XMG GhcPs (LHsExpr GhcPs)
FromSource (L SrcSpanAnnL
_ [L SrcSpanAnnA
_ (Match XCMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
_ HsMatchContext GhcPs
CaseAlt [L SrcSpanAnnA
_ (VarPat XVarPat GhcPs
_ (L SrcSpanAnnN
_ RdrName
y))] (GRHSs XCGRHSs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
_ [L SrcAnn NoEpAnns
_ (GRHS XCGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
_ [] GenLocated SrcSpanAnnA (HsExpr GhcPs)
e)] (EmptyLocalBinds XEmptyLocalBinds GhcPs GhcPs
_))) ]))))
| RdrName -> String
occNameStr RdrName
x forall a. Eq a => a -> a -> Bool
== RdrName -> String
occNameStr RdrName
y =
[forall a b.
(Outputable a, Outputable b) =>
String -> Located a -> Located b -> [Refactoring SrcSpan] -> Idea
suggest String
"Redundant case" (forall a e. LocatedAn a e -> Located e
reLoc LHsExpr GhcPs
o) (forall a e. LocatedAn a e -> Located e
reLoc GenLocated SrcSpanAnnA (HsExpr GhcPs)
e) [Refactoring SrcSpan
r]]
where
r :: Refactoring SrcSpan
r = forall a. RType -> a -> [(String, a)] -> String -> Refactoring a
Replace RType
Expr (forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
toSSA LHsExpr GhcPs
o) [(String
"x", forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
toSSA GenLocated SrcSpanAnnA (HsExpr GhcPs)
e)] String
"x"
expHint LHsExpr GhcPs
_ = []