{-# LANGUAGE ViewPatterns, PatternGuards, TypeFamilies #-}
module Hint.Pattern(patternHint) where
import Hint.Type(DeclHint,Idea,ghcAnnotations,ideaTo,toSS,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 qualified Refact.Types as R (RType(Pattern, Match), SrcSpan)
import GHC.Hs
import SrcLoc
import RdrName
import OccName
import Bag
import BasicTypes
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
_scope modu :: ModuleEx
modu x :: LHsDecl GhcPs
x =
((Pattern, String -> Pattern -> [Refactoring SrcSpan] -> Idea)
-> [Idea])
-> [(Pattern, String -> Pattern -> [Refactoring SrcSpan] -> Idea)]
-> [Idea]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (((String -> Pattern -> [Refactoring SrcSpan] -> Idea)
-> Pattern -> [Idea])
-> (String -> Pattern -> [Refactoring SrcSpan] -> Idea, Pattern)
-> [Idea]
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (String -> Pattern -> [Refactoring SrcSpan] -> Idea)
-> Pattern -> [Idea]
hints ((String -> Pattern -> [Refactoring SrcSpan] -> Idea, Pattern)
-> [Idea])
-> ((Pattern, String -> Pattern -> [Refactoring SrcSpan] -> Idea)
-> (String -> Pattern -> [Refactoring SrcSpan] -> Idea, Pattern))
-> (Pattern, String -> Pattern -> [Refactoring SrcSpan] -> Idea)
-> [Idea]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Pattern, String -> Pattern -> [Refactoring SrcSpan] -> Idea)
-> (String -> Pattern -> [Refactoring SrcSpan] -> Idea, Pattern)
forall a b. (a, b) -> (b, a)
swap) (LHsDecl GhcPs
-> [(Pattern, String -> Pattern -> [Refactoring SrcSpan] -> Idea)]
asPattern LHsDecl GhcPs
x) [Idea] -> [Idea] -> [Idea]
forall a. [a] -> [a] -> [a]
++
(Located (Pat GhcPs) -> [Idea]) -> [Located (Pat GhcPs)] -> [Idea]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Bool -> Bool -> LPat GhcPs -> [Idea]
patHint Bool
strict Bool
False) [LPat GhcPs
Located (Pat GhcPs)
p | PatBind _ p :: LPat GhcPs
p _ _ <- LHsDecl GhcPs -> [HsBindLR GhcPs GhcPs]
forall from to. Biplate from to => from -> [to]
universeBi LHsDecl GhcPs
x :: [HsBind GhcPs]] [Idea] -> [Idea] -> [Idea]
forall a. [a] -> [a] -> [a]
++
(Located (Pat GhcPs) -> [Idea]) -> [Located (Pat GhcPs)] -> [Idea]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Bool -> Bool -> LPat GhcPs -> [Idea]
patHint Bool
strict Bool
True) (LHsDecl GhcPs -> [Located (Pat GhcPs)]
forall from to. Biplate from to => from -> [to]
universeBi (LHsDecl GhcPs -> [Located (Pat GhcPs)])
-> LHsDecl GhcPs -> [Located (Pat GhcPs)]
forall a b. (a -> b) -> a -> b
$ (LHsBind GhcPs -> LHsBind GhcPs) -> LHsDecl GhcPs -> LHsDecl GhcPs
forall from to. Biplate from to => (to -> to) -> from -> from
transformBi LHsBind GhcPs -> LHsBind GhcPs
noPatBind LHsDecl GhcPs
x) [Idea] -> [Idea] -> [Idea]
forall a. [a] -> [a] -> [a]
++
(LHsExpr GhcPs -> [Idea]) -> [LHsExpr GhcPs] -> [Idea]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap LHsExpr GhcPs -> [Idea]
expHint (LHsDecl GhcPs -> [LHsExpr GhcPs]
forall from to. Biplate from to => from -> [to]
universeBi LHsDecl GhcPs
x)
where
exts :: [String]
exts = [String] -> [String]
forall a. Ord a => [a] -> [a]
nubOrd ([String] -> [String]) -> [String] -> [String]
forall a b. (a -> b) -> a -> b
$ ((Located AnnotationComment, [String]) -> [String])
-> [(Located AnnotationComment, [String])] -> [String]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Located AnnotationComment, [String]) -> [String]
forall a b. (a, b) -> b
snd ([(Located AnnotationComment, String)]
-> [(Located AnnotationComment, [String])]
languagePragmas (ApiAnns -> [(Located AnnotationComment, String)]
pragmas (ModuleEx -> ApiAnns
ghcAnnotations ModuleEx
modu)))
strict :: Bool
strict = "Strict" String -> [String] -> Bool
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 loc :: SrcSpan
loc a :: HsBindLR GhcPs GhcPs
a@PatBind{}) = SrcSpan -> HsBindLR GhcPs GhcPs -> LHsBind GhcPs
forall l e. l -> e -> GenLocated l e
L SrcSpan
loc HsBindLR GhcPs GhcPs
a{pat_lhs :: LPat GhcPs
pat_lhs=SrcSpanLess (Located (Pat GhcPs)) -> Located (Pat GhcPs)
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (XWildPat GhcPs -> Pat GhcPs
forall p. XWildPat p -> Pat p
WildPat NoExtField
XWildPat GhcPs
noExtField)}
noPatBind x :: LHsBind GhcPs
x = LHsBind GhcPs
x
hints :: (String -> Pattern -> [Refactoring R.SrcSpan] -> Idea) -> Pattern -> [Idea]
hints :: (String -> Pattern -> [Refactoring SrcSpan] -> Idea)
-> Pattern -> [Idea]
hints gen :: String -> Pattern -> [Refactoring SrcSpan] -> Idea
gen (Pattern l :: SrcSpan
l rtype :: RType
rtype pat :: [LPat GhcPs]
pat (GRHSs _ [L _ (GRHS _ [] bod :: LHsExpr GhcPs
bod)] bind :: LHsLocalBinds GhcPs
bind))
| [LGRHS GhcPs (LHsExpr GhcPs)] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [LGRHS GhcPs (LHsExpr GhcPs)]
guards Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 2 = [String -> Pattern -> [Refactoring SrcSpan] -> Idea
gen "Use guards" (SrcSpan
-> RType -> [LPat GhcPs] -> GRHSs GhcPs (LHsExpr GhcPs) -> Pattern
Pattern SrcSpan
l RType
rtype [LPat GhcPs]
pat (XCGRHSs GhcPs (LHsExpr GhcPs)
-> [LGRHS GhcPs (LHsExpr GhcPs)]
-> LHsLocalBinds GhcPs
-> GRHSs GhcPs (LHsExpr GhcPs)
forall p body.
XCGRHSs p body -> [LGRHS p body] -> LHsLocalBinds p -> GRHSs p body
GRHSs NoExtField
XCGRHSs GhcPs (LHsExpr GhcPs)
noExtField [LGRHS GhcPs (LHsExpr GhcPs)]
guards LHsLocalBinds GhcPs
bind)) [Refactoring SrcSpan
refactoring]]
where
rawGuards :: [(LHsExpr GhcPs, LHsExpr GhcPs)]
rawGuards :: [(LHsExpr GhcPs, LHsExpr GhcPs)]
rawGuards = LHsExpr GhcPs -> [(LHsExpr GhcPs, LHsExpr GhcPs)]
asGuards LHsExpr GhcPs
bod
mkGuard :: LHsExpr GhcPs -> (LHsExpr GhcPs -> GRHS GhcPs (LHsExpr GhcPs))
mkGuard :: LHsExpr GhcPs -> LHsExpr GhcPs -> GRHS GhcPs (LHsExpr GhcPs)
mkGuard a :: LHsExpr GhcPs
a = XCGRHS GhcPs (LHsExpr GhcPs)
-> [GuardLStmt GhcPs]
-> LHsExpr GhcPs
-> GRHS GhcPs (LHsExpr GhcPs)
forall p body.
XCGRHS p body -> [GuardLStmt p] -> body -> GRHS p body
GRHS NoExtField
XCGRHS GhcPs (LHsExpr GhcPs)
noExtField [SrcSpanLess (GuardLStmt GhcPs) -> GuardLStmt GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (GuardLStmt GhcPs) -> GuardLStmt GhcPs)
-> SrcSpanLess (GuardLStmt GhcPs) -> GuardLStmt GhcPs
forall a b. (a -> b) -> a -> b
$ XBodyStmt GhcPs GhcPs (LHsExpr GhcPs)
-> LHsExpr GhcPs
-> SyntaxExpr GhcPs
-> SyntaxExpr GhcPs
-> StmtLR GhcPs GhcPs (LHsExpr GhcPs)
forall idL idR body.
XBodyStmt idL idR body
-> body -> SyntaxExpr idR -> SyntaxExpr idR -> StmtLR idL idR body
BodyStmt NoExtField
XBodyStmt GhcPs GhcPs (LHsExpr GhcPs)
noExtField LHsExpr GhcPs
a SyntaxExpr GhcPs
forall (p :: Pass). SyntaxExpr (GhcPass p)
noSyntaxExpr SyntaxExpr GhcPs
forall (p :: Pass). SyntaxExpr (GhcPass p)
noSyntaxExpr]
guards :: [LGRHS GhcPs (LHsExpr GhcPs)]
guards :: [LGRHS GhcPs (LHsExpr GhcPs)]
guards = ((LHsExpr GhcPs, LHsExpr GhcPs) -> LGRHS GhcPs (LHsExpr GhcPs))
-> [(LHsExpr GhcPs, LHsExpr GhcPs)]
-> [LGRHS GhcPs (LHsExpr GhcPs)]
forall a b. (a -> b) -> [a] -> [b]
map (GRHS GhcPs (LHsExpr GhcPs) -> LGRHS GhcPs (LHsExpr GhcPs)
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (GRHS GhcPs (LHsExpr GhcPs) -> LGRHS GhcPs (LHsExpr GhcPs))
-> ((LHsExpr GhcPs, LHsExpr GhcPs) -> GRHS GhcPs (LHsExpr GhcPs))
-> (LHsExpr GhcPs, LHsExpr GhcPs)
-> LGRHS GhcPs (LHsExpr GhcPs)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LHsExpr GhcPs -> LHsExpr GhcPs -> GRHS GhcPs (LHsExpr GhcPs))
-> (LHsExpr GhcPs, LHsExpr GhcPs) -> GRHS GhcPs (LHsExpr GhcPs)
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
(lhs :: [LHsExpr GhcPs]
lhs, rhs :: [LHsExpr GhcPs]
rhs) = [(LHsExpr GhcPs, LHsExpr GhcPs)]
-> ([LHsExpr GhcPs], [LHsExpr GhcPs])
forall a b. [(a, b)] -> ([a], [b])
unzip [(LHsExpr GhcPs, LHsExpr GhcPs)]
rawGuards
mkTemplate :: String
-> [GenLocated SrcSpan e]
-> [Either (GenLocated SrcSpan e) (String, SrcSpan)]
mkTemplate c :: String
c ps :: [GenLocated SrcSpan e]
ps =
(GenLocated SrcSpan e
-> Char -> Either (GenLocated SrcSpan e) (String, SrcSpan))
-> [GenLocated SrcSpan e]
-> String
-> [Either (GenLocated SrcSpan e) (String, SrcSpan)]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith GenLocated SrcSpan e
-> Char -> Either (GenLocated SrcSpan e) (String, SrcSpan)
checkLoc [GenLocated SrcSpan e]
ps ['1' .. '9']
where
checkLoc :: GenLocated SrcSpan e
-> Char -> Either (GenLocated SrcSpan e) (String, SrcSpan)
checkLoc p :: GenLocated SrcSpan e
p@(L l :: SrcSpan
l _) v :: Char
v = if SrcSpan
l SrcSpan -> SrcSpan -> Bool
forall a. Eq a => a -> a -> Bool
== SrcSpan
noSrcSpan then GenLocated SrcSpan e
-> Either (GenLocated SrcSpan e) (String, SrcSpan)
forall a b. a -> Either a b
Left GenLocated SrcSpan e
p else (String, SrcSpan)
-> Either (GenLocated SrcSpan e) (String, SrcSpan)
forall a b. b -> Either a b
Right (String
c String -> String -> String
forall a. [a] -> [a] -> [a]
++ [Char
v], GenLocated SrcSpan e -> SrcSpan
forall a. HasSrcSpan a => a -> SrcSpan
toSS GenLocated SrcSpan e
p)
patSubts :: [Either (Located (Pat GhcPs)) (String, SrcSpan)]
patSubts =
case [LPat GhcPs]
pat of
[p :: LPat GhcPs
p] -> [Located (Pat GhcPs)
-> Either (Located (Pat GhcPs)) (String, SrcSpan)
forall a b. a -> Either a b
Left LPat GhcPs
Located (Pat GhcPs)
p]
ps :: [LPat GhcPs]
ps -> String
-> [Located (Pat GhcPs)]
-> [Either (Located (Pat GhcPs)) (String, SrcSpan)]
forall e.
String
-> [GenLocated SrcSpan e]
-> [Either (GenLocated SrcSpan e) (String, SrcSpan)]
mkTemplate "p100" [LPat GhcPs]
[Located (Pat GhcPs)]
ps
guardSubts :: [Either (LHsExpr GhcPs) (String, SrcSpan)]
guardSubts = String
-> [LHsExpr GhcPs] -> [Either (LHsExpr GhcPs) (String, SrcSpan)]
forall e.
String
-> [GenLocated SrcSpan e]
-> [Either (GenLocated SrcSpan e) (String, SrcSpan)]
mkTemplate "g100" [LHsExpr GhcPs]
lhs
exprSubts :: [Either (LHsExpr GhcPs) (String, SrcSpan)]
exprSubts = String
-> [LHsExpr GhcPs] -> [Either (LHsExpr GhcPs) (String, SrcSpan)]
forall e.
String
-> [GenLocated SrcSpan e]
-> [Either (GenLocated SrcSpan e) (String, SrcSpan)]
mkTemplate "e100" [LHsExpr GhcPs]
rhs
templateGuards :: [LGRHS GhcPs (LHsExpr GhcPs)]
templateGuards = (GRHS GhcPs (LHsExpr GhcPs) -> LGRHS GhcPs (LHsExpr GhcPs))
-> [GRHS GhcPs (LHsExpr GhcPs)] -> [LGRHS GhcPs (LHsExpr GhcPs)]
forall a b. (a -> b) -> [a] -> [b]
map GRHS GhcPs (LHsExpr GhcPs) -> LGRHS GhcPs (LHsExpr GhcPs)
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc ((Either (LHsExpr GhcPs) (String, SrcSpan)
-> Either (LHsExpr GhcPs) (String, SrcSpan)
-> GRHS GhcPs (LHsExpr GhcPs))
-> [Either (LHsExpr GhcPs) (String, SrcSpan)]
-> [Either (LHsExpr GhcPs) (String, SrcSpan)]
-> [GRHS GhcPs (LHsExpr GhcPs)]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (LHsExpr GhcPs -> LHsExpr GhcPs -> GRHS GhcPs (LHsExpr GhcPs)
mkGuard (LHsExpr GhcPs -> LHsExpr GhcPs -> GRHS GhcPs (LHsExpr GhcPs))
-> (Either (LHsExpr GhcPs) (String, SrcSpan) -> LHsExpr GhcPs)
-> Either (LHsExpr GhcPs) (String, SrcSpan)
-> Either (LHsExpr GhcPs) (String, SrcSpan)
-> GRHS GhcPs (LHsExpr GhcPs)
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` Either (LHsExpr GhcPs) (String, SrcSpan) -> LHsExpr GhcPs
forall b. Either (LHsExpr GhcPs) (String, b) -> LHsExpr GhcPs
toString) [Either (LHsExpr GhcPs) (String, SrcSpan)]
guardSubts [Either (LHsExpr GhcPs) (String, SrcSpan)]
exprSubts)
toString :: Either (LHsExpr GhcPs) (String, b) -> LHsExpr GhcPs
toString (Left e :: LHsExpr GhcPs
e) = LHsExpr GhcPs
e
toString (Right (v :: String
v, _)) = String -> LHsExpr GhcPs
strToVar String
v
toString' :: Either (Located (Pat GhcPs)) (String, b) -> Located (Pat GhcPs)
toString' (Left e :: Located (Pat GhcPs)
e) = Located (Pat GhcPs)
e
toString' (Right (v :: String
v, _)) = String -> LPat GhcPs
strToPat String
v
template :: String
template = String -> Maybe String -> String
forall a. a -> Maybe a -> a
fromMaybe "" (Maybe String -> String) -> Maybe String -> String
forall a b. (a -> b) -> a -> b
$ Idea -> Maybe String
ideaTo (String -> Pattern -> [Refactoring SrcSpan] -> Idea
gen "" (SrcSpan
-> RType -> [LPat GhcPs] -> GRHSs GhcPs (LHsExpr GhcPs) -> Pattern
Pattern SrcSpan
l RType
rtype ((Either (Located (Pat GhcPs)) (String, SrcSpan)
-> Located (Pat GhcPs))
-> [Either (Located (Pat GhcPs)) (String, SrcSpan)]
-> [Located (Pat GhcPs)]
forall a b. (a -> b) -> [a] -> [b]
map Either (Located (Pat GhcPs)) (String, SrcSpan)
-> Located (Pat GhcPs)
forall b.
Either (Located (Pat GhcPs)) (String, b) -> Located (Pat GhcPs)
toString' [Either (Located (Pat GhcPs)) (String, SrcSpan)]
patSubts) (XCGRHSs GhcPs (LHsExpr GhcPs)
-> [LGRHS GhcPs (LHsExpr GhcPs)]
-> LHsLocalBinds GhcPs
-> GRHSs GhcPs (LHsExpr GhcPs)
forall p body.
XCGRHSs p body -> [LGRHS p body] -> LHsLocalBinds p -> GRHSs p body
GRHSs NoExtField
XCGRHSs GhcPs (LHsExpr GhcPs)
noExtField [LGRHS GhcPs (LHsExpr GhcPs)]
templateGuards LHsLocalBinds GhcPs
bind)) [])
f :: [Either a (String, R.SrcSpan)] -> [(String, R.SrcSpan)]
f :: [Either a (String, SrcSpan)] -> [(String, SrcSpan)]
f = [Either a (String, SrcSpan)] -> [(String, SrcSpan)]
forall a b. [Either a b] -> [b]
rights
refactoring :: Refactoring SrcSpan
refactoring = RType
-> SrcSpan -> [(String, SrcSpan)] -> String -> Refactoring SrcSpan
forall a. RType -> a -> [(String, a)] -> String -> Refactoring a
Replace RType
rtype (SrcSpan -> SrcSpan
toRefactSrcSpan SrcSpan
l) ([Either (Located (Pat GhcPs)) (String, SrcSpan)]
-> [(String, SrcSpan)]
forall a. [Either a (String, SrcSpan)] -> [(String, SrcSpan)]
f [Either (Located (Pat GhcPs)) (String, SrcSpan)]
patSubts [(String, SrcSpan)] -> [(String, SrcSpan)] -> [(String, SrcSpan)]
forall a. [a] -> [a] -> [a]
++ [Either (LHsExpr GhcPs) (String, SrcSpan)] -> [(String, SrcSpan)]
forall a. [Either a (String, SrcSpan)] -> [(String, SrcSpan)]
f [Either (LHsExpr GhcPs) (String, SrcSpan)]
guardSubts [(String, SrcSpan)] -> [(String, SrcSpan)] -> [(String, SrcSpan)]
forall a. [a] -> [a] -> [a]
++ [Either (LHsExpr GhcPs) (String, SrcSpan)] -> [(String, SrcSpan)]
forall a. [Either a (String, SrcSpan)] -> [(String, SrcSpan)]
f [Either (LHsExpr GhcPs) (String, SrcSpan)]
exprSubts) String
template
hints gen :: String -> Pattern -> [Refactoring SrcSpan] -> Idea
gen (Pattern l :: SrcSpan
l t :: RType
t pats :: [LPat GhcPs]
pats o :: GRHSs GhcPs (LHsExpr GhcPs)
o@(GRHSs _ [L _ (GRHS _ [test :: GuardLStmt GhcPs
test] bod :: LHsExpr GhcPs
bod)] bind :: LHsLocalBinds GhcPs
bind))
| GuardLStmt GhcPs -> String
forall a. Outputable a => a -> String
unsafePrettyPrint GuardLStmt GhcPs
test String -> [String] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` ["otherwise", "True"]
= [String -> Pattern -> [Refactoring SrcSpan] -> Idea
gen "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 (LHsExpr GhcPs)]
grhssGRHSs=[SrcSpanLess (LGRHS GhcPs (LHsExpr GhcPs))
-> LGRHS GhcPs (LHsExpr GhcPs)
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (XCGRHS GhcPs (LHsExpr GhcPs)
-> [GuardLStmt GhcPs]
-> LHsExpr GhcPs
-> GRHS GhcPs (LHsExpr GhcPs)
forall p body.
XCGRHS p body -> [GuardLStmt p] -> body -> GRHS p body
GRHS NoExtField
XCGRHS GhcPs (LHsExpr GhcPs)
noExtField [] LHsExpr GhcPs
bod)]}) [RType -> SrcSpan -> Refactoring SrcSpan
forall a. RType -> a -> Refactoring a
Delete RType
Stmt (GuardLStmt GhcPs -> SrcSpan
forall a. HasSrcSpan a => a -> SrcSpan
toSS GuardLStmt GhcPs
test)]]
hints _ (Pattern l :: SrcSpan
l t :: RType
t pats :: [LPat GhcPs]
pats bod :: GRHSs GhcPs (LHsExpr GhcPs)
bod@(GRHSs _ _ binds :: LHsLocalBinds GhcPs
binds)) | LHsLocalBinds GhcPs -> Bool
f LHsLocalBinds GhcPs
binds
= [String -> SrcSpan -> String -> [Refactoring SrcSpan] -> Idea
suggestRemove "Redundant where" SrcSpan
whereSpan "where" [ ]]
where
f :: LHsLocalBinds GhcPs -> Bool
f :: LHsLocalBinds GhcPs -> Bool
f (L _ (HsValBinds _ (ValBinds _ bag :: LHsBindsLR GhcPs GhcPs
bag _))) = LHsBindsLR GhcPs GhcPs -> Bool
forall a. Bag a -> Bool
isEmptyBag LHsBindsLR GhcPs GhcPs
bag
f (L _ (HsIPBinds _ (IPBinds _ l :: [LIPBind GhcPs]
l))) = [LIPBind GhcPs] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [LIPBind GhcPs]
l
f _ = Bool
False
whereSpan :: SrcSpan
whereSpan = case SrcSpan
l of
UnhelpfulSpan s :: FastString
s -> FastString -> SrcSpan
UnhelpfulSpan FastString
s
RealSrcSpan s :: RealSrcSpan
s ->
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 Int -> Int -> Int
forall a. Num a => a -> a -> a
- 5)
in RealSrcSpan -> SrcSpan
RealSrcSpan (RealSrcLoc -> RealSrcLoc -> RealSrcSpan
mkRealSrcSpan RealSrcLoc
start RealSrcLoc
end)
hints gen :: String -> Pattern -> [Refactoring SrcSpan] -> Idea
gen (Pattern l :: SrcSpan
l t :: RType
t pats :: [LPat GhcPs]
pats o :: GRHSs GhcPs (LHsExpr GhcPs)
o@(GRHSs _ ([LGRHS GhcPs (LHsExpr GhcPs)]
-> Maybe
([LGRHS GhcPs (LHsExpr GhcPs)], LGRHS GhcPs (LHsExpr GhcPs))
forall a. [a] -> Maybe ([a], a)
unsnoc -> Just (gs :: [LGRHS GhcPs (LHsExpr GhcPs)]
gs, L _ (GRHS _ [test :: GuardLStmt GhcPs
test] bod :: LHsExpr GhcPs
bod))) binds :: LHsLocalBinds GhcPs
binds))
| GuardLStmt GhcPs -> String
forall a. Outputable a => a -> String
unsafePrettyPrint GuardLStmt GhcPs
test String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== "True"
= let otherwise_ :: GuardLStmt GhcPs
otherwise_ = SrcSpanLess (GuardLStmt GhcPs) -> GuardLStmt GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (GuardLStmt GhcPs) -> GuardLStmt GhcPs)
-> SrcSpanLess (GuardLStmt GhcPs) -> GuardLStmt GhcPs
forall a b. (a -> b) -> a -> b
$ XBodyStmt GhcPs GhcPs (LHsExpr GhcPs)
-> LHsExpr GhcPs
-> SyntaxExpr GhcPs
-> SyntaxExpr GhcPs
-> StmtLR GhcPs GhcPs (LHsExpr GhcPs)
forall idL idR body.
XBodyStmt idL idR body
-> body -> SyntaxExpr idR -> SyntaxExpr idR -> StmtLR idL idR body
BodyStmt NoExtField
XBodyStmt GhcPs GhcPs (LHsExpr GhcPs)
noExtField (String -> LHsExpr GhcPs
strToVar "otherwise") SyntaxExpr GhcPs
forall (p :: Pass). SyntaxExpr (GhcPass p)
noSyntaxExpr SyntaxExpr GhcPs
forall (p :: Pass). SyntaxExpr (GhcPass p)
noSyntaxExpr in
[String -> Pattern -> [Refactoring SrcSpan] -> Idea
gen "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 (LHsExpr GhcPs)]
grhssGRHSs = [LGRHS GhcPs (LHsExpr GhcPs)]
gs [LGRHS GhcPs (LHsExpr GhcPs)]
-> [LGRHS GhcPs (LHsExpr GhcPs)] -> [LGRHS GhcPs (LHsExpr GhcPs)]
forall a. [a] -> [a] -> [a]
++ [SrcSpanLess (LGRHS GhcPs (LHsExpr GhcPs))
-> LGRHS GhcPs (LHsExpr GhcPs)
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (XCGRHS GhcPs (LHsExpr GhcPs)
-> [GuardLStmt GhcPs]
-> LHsExpr GhcPs
-> GRHS GhcPs (LHsExpr GhcPs)
forall p body.
XCGRHS p body -> [GuardLStmt p] -> body -> GRHS p body
GRHS NoExtField
XCGRHS GhcPs (LHsExpr GhcPs)
noExtField [GuardLStmt GhcPs
otherwise_] LHsExpr GhcPs
bod)]}) [RType
-> SrcSpan -> [(String, SrcSpan)] -> String -> Refactoring SrcSpan
forall a. RType -> a -> [(String, a)] -> String -> Refactoring a
Replace RType
Expr (GuardLStmt GhcPs -> SrcSpan
forall a. HasSrcSpan a => a -> SrcSpan
toSS GuardLStmt GhcPs
test) [] "otherwise"]]
hints _ _ = []
asGuards :: LHsExpr GhcPs -> [(LHsExpr GhcPs, LHsExpr GhcPs)]
asGuards :: LHsExpr GhcPs -> [(LHsExpr GhcPs, LHsExpr GhcPs)]
asGuards (L _ (HsPar _ x :: LHsExpr GhcPs
x)) = LHsExpr GhcPs -> [(LHsExpr GhcPs, LHsExpr GhcPs)]
asGuards LHsExpr GhcPs
x
asGuards (L _ (HsIf _ _ a :: LHsExpr GhcPs
a b :: LHsExpr GhcPs
b c :: LHsExpr GhcPs
c)) = (LHsExpr GhcPs
a, LHsExpr GhcPs
b) (LHsExpr GhcPs, LHsExpr GhcPs)
-> [(LHsExpr GhcPs, LHsExpr GhcPs)]
-> [(LHsExpr GhcPs, LHsExpr GhcPs)]
forall a. a -> [a] -> [a]
: LHsExpr GhcPs -> [(LHsExpr GhcPs, LHsExpr GhcPs)]
asGuards LHsExpr GhcPs
c
asGuards x :: LHsExpr GhcPs
x = [(String -> LHsExpr GhcPs
strToVar "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 loc :: SrcSpan
loc x :: HsDecl GhcPs
x) = (HsBindLR GhcPs GhcPs
-> [(Pattern, String -> Pattern -> [Refactoring SrcSpan] -> Idea)])
-> [HsBindLR GhcPs GhcPs]
-> [(Pattern, String -> Pattern -> [Refactoring SrcSpan] -> Idea)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap HsBindLR GhcPs GhcPs
-> [(Pattern, String -> Pattern -> [Refactoring SrcSpan] -> Idea)]
decl (HsDecl GhcPs -> [HsBindLR GhcPs GhcPs]
forall from to. Biplate from to => from -> [to]
universeBi HsDecl GhcPs
x)
where
decl :: HsBind GhcPs -> [(Pattern, String -> Pattern -> [Refactoring R.SrcSpan] -> Idea)]
decl :: HsBindLR GhcPs GhcPs
-> [(Pattern, String -> Pattern -> [Refactoring SrcSpan] -> Idea)]
decl o :: HsBindLR GhcPs GhcPs
o@(PatBind _ pat :: LPat GhcPs
pat rhs :: GRHSs GhcPs (LHsExpr GhcPs)
rhs _) = [(SrcSpan
-> RType -> [LPat GhcPs] -> GRHSs GhcPs (LHsExpr GhcPs) -> Pattern
Pattern SrcSpan
loc RType
Bind [LPat GhcPs
pat] GRHSs GhcPs (LHsExpr GhcPs)
rhs, \msg :: String
msg (Pattern _ _ [pat :: LPat GhcPs
pat] rhs :: GRHSs GhcPs (LHsExpr GhcPs)
rhs) rs :: [Refactoring SrcSpan]
rs -> String
-> LHsBind GhcPs -> LHsBind GhcPs -> [Refactoring SrcSpan] -> Idea
forall a b.
(HasSrcSpan a, Outputable a, HasSrcSpan b, Outputable b) =>
String -> a -> b -> [Refactoring SrcSpan] -> Idea
suggest String
msg (SrcSpan -> HsBindLR GhcPs GhcPs -> LHsBind GhcPs
forall l e. l -> e -> GenLocated l e
L SrcSpan
loc HsBindLR GhcPs GhcPs
o :: LHsBind GhcPs) (SrcSpanLess (LHsBind GhcPs) -> LHsBind GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (XPatBind GhcPs GhcPs
-> LPat GhcPs
-> GRHSs GhcPs (LHsExpr GhcPs)
-> ([Tickish Id], [[Tickish Id]])
-> HsBindLR GhcPs GhcPs
forall idL idR.
XPatBind idL idR
-> LPat idL
-> GRHSs idR (LHsExpr idR)
-> ([Tickish Id], [[Tickish Id]])
-> HsBindLR idL idR
PatBind NoExtField
XPatBind GhcPs GhcPs
noExtField LPat GhcPs
pat GRHSs GhcPs (LHsExpr GhcPs)
rhs ([], [])) :: LHsBind GhcPs) [Refactoring SrcSpan]
rs)]
decl (FunBind _ _ (MG _ (L _ xs :: [LMatch GhcPs (LHsExpr GhcPs)]
xs) _) _ _) = (LMatch GhcPs (LHsExpr GhcPs)
-> (Pattern, String -> Pattern -> [Refactoring SrcSpan] -> Idea))
-> [LMatch GhcPs (LHsExpr GhcPs)]
-> [(Pattern, String -> Pattern -> [Refactoring SrcSpan] -> Idea)]
forall a b. (a -> b) -> [a] -> [b]
map LMatch GhcPs (LHsExpr GhcPs)
-> (Pattern, String -> Pattern -> [Refactoring SrcSpan] -> Idea)
match [LMatch GhcPs (LHsExpr GhcPs)]
xs
decl _ = []
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 loc :: SrcSpan
loc (Match _ ctx :: HsMatchContext (NameOrRdrName (IdP GhcPs))
ctx pats :: [LPat GhcPs]
pats grhss :: GRHSs GhcPs (LHsExpr GhcPs)
grhss)) = (SrcSpan
-> RType -> [LPat GhcPs] -> GRHSs GhcPs (LHsExpr GhcPs) -> Pattern
Pattern SrcSpan
loc RType
R.Match [LPat GhcPs]
pats GRHSs GhcPs (LHsExpr GhcPs)
grhss, \msg :: String
msg (Pattern _ _ pats :: [LPat GhcPs]
pats grhss :: GRHSs GhcPs (LHsExpr GhcPs)
grhss) rs :: [Refactoring SrcSpan]
rs -> String
-> LMatch GhcPs (LHsExpr GhcPs)
-> LMatch GhcPs (LHsExpr GhcPs)
-> [Refactoring SrcSpan]
-> Idea
forall a b.
(HasSrcSpan a, Outputable a, HasSrcSpan b, Outputable b) =>
String -> a -> b -> [Refactoring SrcSpan] -> Idea
suggest String
msg LMatch GhcPs (LHsExpr GhcPs)
o (SrcSpanLess (LMatch GhcPs (LHsExpr GhcPs))
-> LMatch GhcPs (LHsExpr GhcPs)
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (XCMatch GhcPs (LHsExpr GhcPs)
-> HsMatchContext (NameOrRdrName (IdP GhcPs))
-> [LPat GhcPs]
-> GRHSs GhcPs (LHsExpr GhcPs)
-> Match GhcPs (LHsExpr GhcPs)
forall p body.
XCMatch p body
-> HsMatchContext (NameOrRdrName (IdP p))
-> [LPat p]
-> GRHSs p body
-> Match p body
Match NoExtField
XCMatch GhcPs (LHsExpr GhcPs)
noExtField HsMatchContext (NameOrRdrName (IdP GhcPs))
ctx [LPat GhcPs]
pats GRHSs GhcPs (LHsExpr GhcPs)
grhss) :: LMatch GhcPs (LHsExpr GhcPs)) [Refactoring SrcSpan]
rs)
match _ = (Pattern, String -> Pattern -> [Refactoring SrcSpan] -> Idea)
forall a. HasCallStack => a
undefined
patHint :: Bool -> Bool -> LPat GhcPs -> [Idea]
patHint :: Bool -> Bool -> LPat GhcPs -> [Idea]
patHint _ _ o :: LPat GhcPs
o@(L _ (ConPatIn name (PrefixCon args)))
| [Located (Pat GhcPs)] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [LPat GhcPs]
[Located (Pat GhcPs)]
args Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= 3 Bool -> Bool -> Bool
&& (Located (Pat GhcPs) -> Bool) -> [Located (Pat GhcPs)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all LPat GhcPs -> Bool
Located (Pat GhcPs) -> Bool
isPWildcard [LPat GhcPs]
[Located (Pat GhcPs)]
args =
let rec_fields :: HsRecFields GhcPs (LPat GhcPs)
rec_fields = [LHsRecField GhcPs (Located (Pat GhcPs))]
-> Maybe (Located Int) -> HsRecFields GhcPs (Located (Pat GhcPs))
forall p arg.
[LHsRecField p arg] -> Maybe (Located Int) -> HsRecFields p arg
HsRecFields [] Maybe (Located Int)
forall a. Maybe a
Nothing :: HsRecFields GhcPs (LPat GhcPs)
new :: LPat GhcPs
new = SrcSpanLess (Located (Pat GhcPs)) -> LPat GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (Located (Pat GhcPs)) -> LPat GhcPs)
-> SrcSpanLess (Located (Pat GhcPs)) -> LPat GhcPs
forall a b. (a -> b) -> a -> b
$ Located (IdP GhcPs) -> HsConPatDetails GhcPs -> Pat GhcPs
forall p. Located (IdP p) -> HsConPatDetails p -> Pat p
ConPatIn Located (IdP GhcPs)
name (HsRecFields GhcPs (Located (Pat GhcPs))
-> HsConDetails
(Located (Pat GhcPs)) (HsRecFields GhcPs (Located (Pat GhcPs)))
forall arg rec. rec -> HsConDetails arg rec
RecCon HsRecFields GhcPs (Located (Pat GhcPs))
rec_fields) :: LPat GhcPs
in
[String
-> Located (Pat GhcPs)
-> Located (Pat GhcPs)
-> [Refactoring SrcSpan]
-> Idea
forall a b.
(HasSrcSpan a, Outputable a, HasSrcSpan b, Outputable b) =>
String -> a -> b -> [Refactoring SrcSpan] -> Idea
suggest "Use record patterns" LPat GhcPs
Located (Pat GhcPs)
o LPat GhcPs
Located (Pat GhcPs)
new [RType
-> SrcSpan -> [(String, SrcSpan)] -> String -> Refactoring SrcSpan
forall a. RType -> a -> [(String, a)] -> String -> Refactoring a
Replace RType
R.Pattern (Located (Pat GhcPs) -> SrcSpan
forall a. HasSrcSpan a => a -> SrcSpan
toSS LPat GhcPs
Located (Pat GhcPs)
o) [] (Located (Pat GhcPs) -> String
forall a. Outputable a => a -> String
unsafePrettyPrint LPat GhcPs
Located (Pat GhcPs)
new)]]
patHint _ _ o :: LPat GhcPs
o@(L _ (VarPat _ (L _ name)))
| OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc IdP GhcPs
RdrName
name) String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== "otherwise" =
[String
-> Located (Pat GhcPs)
-> Located (Pat GhcPs)
-> [Refactoring SrcSpan]
-> Idea
forall a b.
(HasSrcSpan a, Outputable a, HasSrcSpan b, Outputable b) =>
String -> a -> b -> [Refactoring SrcSpan] -> Idea
warn "Used otherwise as a pattern" LPat GhcPs
Located (Pat GhcPs)
o (SrcSpanLess (Located (Pat GhcPs)) -> Located (Pat GhcPs)
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (XWildPat GhcPs -> Pat GhcPs
forall p. XWildPat p -> Pat p
WildPat NoExtField
XWildPat GhcPs
noExtField) :: LPat GhcPs) []]
patHint lang :: Bool
lang strict :: Bool
strict o :: LPat GhcPs
o@(L _ (BangPat _ pat@(L _ x)))
| Bool
strict, Pat GhcPs -> Bool
f Pat GhcPs
x = [String
-> Located (Pat GhcPs)
-> Located (Pat GhcPs)
-> [Refactoring SrcSpan]
-> Idea
forall a b.
(HasSrcSpan a, Outputable a, HasSrcSpan b, Outputable b) =>
String -> a -> b -> [Refactoring SrcSpan] -> Idea
warn "Redundant bang pattern" LPat GhcPs
Located (Pat GhcPs)
o (SrcSpanLess (Located (Pat GhcPs)) -> Located (Pat GhcPs)
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc Pat GhcPs
SrcSpanLess (Located (Pat GhcPs))
x :: LPat GhcPs) [Refactoring SrcSpan
r]]
where
f :: Pat GhcPs -> Bool
f :: Pat GhcPs -> Bool
f (ParPat _ (L _ x)) = Pat GhcPs -> Bool
f Pat GhcPs
x
f (AsPat _ _ (L _ x)) = Pat GhcPs -> Bool
f Pat GhcPs
x
f LitPat {} = Bool
True
f NPat {} = Bool
True
f ConPatIn {} = Bool
True
f TuplePat {} = Bool
True
f ListPat {} = Bool
True
f (SigPat _ (L _ p) _) = Pat GhcPs -> Bool
f Pat GhcPs
p
f _ = Bool
False
r :: Refactoring SrcSpan
r = RType
-> SrcSpan -> [(String, SrcSpan)] -> String -> Refactoring SrcSpan
forall a. RType -> a -> [(String, a)] -> String -> Refactoring a
Replace RType
R.Pattern (Located (Pat GhcPs) -> SrcSpan
forall a. HasSrcSpan a => a -> SrcSpan
toSS LPat GhcPs
Located (Pat GhcPs)
o) [("x", Located (Pat GhcPs) -> SrcSpan
forall a. HasSrcSpan a => a -> SrcSpan
toSS LPat GhcPs
Located (Pat GhcPs)
pat)] "x"
patHint False _ o :: LPat GhcPs
o@(L _ (LazyPat _ pat@(L _ x)))
| Pat GhcPs -> Bool
f Pat GhcPs
x = [String
-> Located (Pat GhcPs)
-> Located (Pat GhcPs)
-> [Refactoring SrcSpan]
-> Idea
forall a b.
(HasSrcSpan a, Outputable a, HasSrcSpan b, Outputable b) =>
String -> a -> b -> [Refactoring SrcSpan] -> Idea
warn "Redundant irrefutable pattern" LPat GhcPs
Located (Pat GhcPs)
o (SrcSpanLess (Located (Pat GhcPs)) -> Located (Pat GhcPs)
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc Pat GhcPs
SrcSpanLess (Located (Pat GhcPs))
x :: LPat GhcPs) [Refactoring SrcSpan
r]]
where
f :: Pat GhcPs -> Bool
f :: Pat GhcPs -> Bool
f (ParPat _ (L _ x)) = Pat GhcPs -> Bool
f Pat GhcPs
x
f (AsPat _ _ (L _ x)) = Pat GhcPs -> Bool
f Pat GhcPs
x
f WildPat{} = Bool
True
f VarPat{} = Bool
True
f _ = Bool
False
r :: Refactoring SrcSpan
r = RType
-> SrcSpan -> [(String, SrcSpan)] -> String -> Refactoring SrcSpan
forall a. RType -> a -> [(String, a)] -> String -> Refactoring a
Replace RType
R.Pattern (Located (Pat GhcPs) -> SrcSpan
forall a. HasSrcSpan a => a -> SrcSpan
toSS LPat GhcPs
Located (Pat GhcPs)
o) [("x", Located (Pat GhcPs) -> SrcSpan
forall a. HasSrcSpan a => a -> SrcSpan
toSS LPat GhcPs
Located (Pat GhcPs)
pat)] "x"
patHint _ _ o :: LPat GhcPs
o@(L _ (AsPat _ v (L _ (WildPat _)))) =
[String
-> Located (Pat GhcPs)
-> Located RdrName
-> [Refactoring SrcSpan]
-> Idea
forall a b.
(HasSrcSpan a, Outputable a, HasSrcSpan b, Outputable b) =>
String -> a -> b -> [Refactoring SrcSpan] -> Idea
warn "Redundant as-pattern" LPat GhcPs
Located (Pat GhcPs)
o Located (IdP GhcPs)
Located RdrName
v []]
patHint _ _ _ = []
expHint :: LHsExpr GhcPs -> [Idea]
expHint :: LHsExpr GhcPs -> [Idea]
expHint o :: LHsExpr GhcPs
o@(L _ (HsCase _ _ (MG _ (L _ [L _ (Match _ CaseAlt [L _ (WildPat _)] (GRHSs _ [L _ (GRHS _ [] e :: LHsExpr GhcPs
e)] (L _ (EmptyLocalBinds _)))) ]) FromSource ))) =
[String
-> LHsExpr GhcPs -> LHsExpr GhcPs -> [Refactoring SrcSpan] -> Idea
forall a b.
(HasSrcSpan a, Outputable a, HasSrcSpan b, Outputable b) =>
String -> a -> b -> [Refactoring SrcSpan] -> Idea
suggest "Redundant case" LHsExpr GhcPs
o LHsExpr GhcPs
e [Refactoring SrcSpan
r]]
where
r :: Refactoring SrcSpan
r = RType
-> SrcSpan -> [(String, SrcSpan)] -> String -> Refactoring SrcSpan
forall a. RType -> a -> [(String, a)] -> String -> Refactoring a
Replace RType
Expr (LHsExpr GhcPs -> SrcSpan
forall a. HasSrcSpan a => a -> SrcSpan
toSS LHsExpr GhcPs
o) [("x", LHsExpr GhcPs -> SrcSpan
forall a. HasSrcSpan a => a -> SrcSpan
toSS LHsExpr GhcPs
e)] "x"
expHint o :: LHsExpr GhcPs
o@(L _ (HsCase _ (L _ (HsVar _ (L _ x :: IdP GhcPs
x))) (MG _ (L _ [L _ (Match _ CaseAlt [L _ (VarPat _ (L _ y))] (GRHSs _ [L _ (GRHS _ [] e :: LHsExpr GhcPs
e)] (L _ (EmptyLocalBinds _)))) ]) FromSource )))
| RdrName -> String
occNameStr IdP GhcPs
RdrName
x String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== RdrName -> String
occNameStr IdP GhcPs
RdrName
y =
[String
-> LHsExpr GhcPs -> LHsExpr GhcPs -> [Refactoring SrcSpan] -> Idea
forall a b.
(HasSrcSpan a, Outputable a, HasSrcSpan b, Outputable b) =>
String -> a -> b -> [Refactoring SrcSpan] -> Idea
suggest "Redundant case" LHsExpr GhcPs
o LHsExpr GhcPs
e [Refactoring SrcSpan
r]]
where
r :: Refactoring SrcSpan
r = RType
-> SrcSpan -> [(String, SrcSpan)] -> String -> Refactoring SrcSpan
forall a. RType -> a -> [(String, a)] -> String -> Refactoring a
Replace RType
Expr (LHsExpr GhcPs -> SrcSpan
forall a. HasSrcSpan a => a -> SrcSpan
toSS LHsExpr GhcPs
o) [("x", LHsExpr GhcPs -> SrcSpan
forall a. HasSrcSpan a => a -> SrcSpan
toSS LHsExpr GhcPs
e)] "x"
expHint _ = []