{-# LANGUAGE NamedFieldPuns #-}
module Hint.NewType (newtypeHint) where
import Hint.Type (Idea, DeclHint', Note(DecreasesLaziness), ideaNote, ignoreNoSuggestion', suggestN')
import Data.List (isSuffixOf)
import HsDecls
import HsSyn
import Outputable
import SrcLoc
newtypeHint :: DeclHint'
newtypeHint :: DeclHint'
newtypeHint _ _ x :: LHsDecl GhcPs
x = LHsDecl GhcPs -> [Idea]
newtypeHintDecl LHsDecl GhcPs
x [Idea] -> [Idea] -> [Idea]
forall a. [a] -> [a] -> [a]
++ LHsDecl GhcPs -> [Idea]
newTypeDerivingStrategiesHintDecl LHsDecl GhcPs
x
newtypeHintDecl :: LHsDecl GhcPs -> [Idea]
newtypeHintDecl :: LHsDecl GhcPs -> [Idea]
newtypeHintDecl old :: LHsDecl GhcPs
old
| Just WarnNewtype{LHsDecl GhcPs
newDecl :: WarnNewtype -> LHsDecl GhcPs
newDecl :: LHsDecl GhcPs
newDecl, HsType GhcPs
insideType :: WarnNewtype -> HsType GhcPs
insideType :: HsType GhcPs
insideType} <- LHsDecl GhcPs -> Maybe WarnNewtype
singleSimpleField LHsDecl GhcPs
old
= [(String -> LHsDecl GhcPs -> LHsDecl GhcPs -> Idea
forall a. (HasSrcSpan a, Outputable a) => String -> a -> a -> Idea
suggestN' "Use newtype instead of data" LHsDecl GhcPs
old LHsDecl GhcPs
newDecl)
{ideaNote :: [Note]
ideaNote = [Note
DecreasesLaziness | HsType GhcPs -> Bool
warnBang HsType GhcPs
insideType]}]
newtypeHintDecl _ = []
newTypeDerivingStrategiesHintDecl :: LHsDecl GhcPs -> [Idea]
newTypeDerivingStrategiesHintDecl :: LHsDecl GhcPs -> [Idea]
newTypeDerivingStrategiesHintDecl decl :: LHsDecl GhcPs
decl@(LL _ (TyClD _ (DataDecl _ _ _ _ dataDef))) =
[String -> LHsDecl GhcPs -> Idea
forall a. (HasSrcSpan a, Outputable a) => String -> a -> Idea
ignoreNoSuggestion' "Use DerivingStrategies" LHsDecl GhcPs
decl | Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ HsDataDefn GhcPs -> Bool
isData HsDataDefn GhcPs
dataDef, Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ HsDataDefn GhcPs -> Bool
hasAllStrategies HsDataDefn GhcPs
dataDef]
newTypeDerivingStrategiesHintDecl _ = []
hasAllStrategies :: HsDataDefn GhcPs -> Bool
hasAllStrategies :: HsDataDefn GhcPs -> Bool
hasAllStrategies (HsDataDefn _ NewType _ _ _ _ (LL _ xs :: SrcSpanLess (HsDeriving GhcPs)
xs)) = (LHsDerivingClause GhcPs -> Bool)
-> [LHsDerivingClause GhcPs] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all LHsDerivingClause GhcPs -> Bool
hasStrategyClause [LHsDerivingClause GhcPs]
SrcSpanLess (HsDeriving GhcPs)
xs
hasAllStrategies _ = Bool
False
isData :: HsDataDefn GhcPs -> Bool
isData :: HsDataDefn GhcPs -> Bool
isData (HsDataDefn _ NewType _ _ _ _ _) = Bool
False
isData (HsDataDefn _ DataType _ _ _ _ _) = Bool
True
isData _ = Bool
False
hasStrategyClause :: LHsDerivingClause GhcPs -> Bool
hasStrategyClause :: LHsDerivingClause GhcPs -> Bool
hasStrategyClause (LL _ (HsDerivingClause _ (Just _) _)) = Bool
True
hasStrategyClause _ = Bool
False
data WarnNewtype = WarnNewtype
{ WarnNewtype -> LHsDecl GhcPs
newDecl :: LHsDecl GhcPs
, WarnNewtype -> HsType GhcPs
insideType :: HsType GhcPs
}
singleSimpleField :: LHsDecl GhcPs -> Maybe WarnNewtype
singleSimpleField :: LHsDecl GhcPs -> Maybe WarnNewtype
singleSimpleField (LL loc :: SrcSpan
loc (TyClD ext decl@(DataDecl _ _ _ _ dataDef@(HsDataDefn _ DataType _ _ _ [LL _ constructor] _))))
| Just inType :: HsType GhcPs
inType <- ConDecl GhcPs -> Maybe (HsType GhcPs)
simpleCons SrcSpanLess (LConDecl GhcPs)
ConDecl GhcPs
constructor =
WarnNewtype -> Maybe WarnNewtype
forall a. a -> Maybe a
Just WarnNewtype :: LHsDecl GhcPs -> HsType GhcPs -> WarnNewtype
WarnNewtype
{ newDecl :: LHsDecl GhcPs
newDecl = SrcSpan -> SrcSpanLess (LHsDecl GhcPs) -> LHsDecl GhcPs
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
LL SrcSpan
loc (SrcSpanLess (LHsDecl GhcPs) -> LHsDecl GhcPs)
-> SrcSpanLess (LHsDecl GhcPs) -> LHsDecl GhcPs
forall a b. (a -> b) -> a -> b
$ XTyClD GhcPs -> TyClDecl GhcPs -> HsDecl GhcPs
forall p. XTyClD p -> TyClDecl p -> HsDecl p
TyClD XTyClD GhcPs
ext TyClDecl GhcPs
decl {tcdDataDefn :: HsDataDefn GhcPs
tcdDataDefn = HsDataDefn GhcPs
dataDef
{ dd_ND :: NewOrData
dd_ND = NewOrData
NewType
, dd_cons :: [LConDecl GhcPs]
dd_cons = (LConDecl GhcPs -> LConDecl GhcPs)
-> [LConDecl GhcPs] -> [LConDecl GhcPs]
forall a b. (a -> b) -> [a] -> [b]
map (\(LL consloc :: SrcSpan
consloc x :: SrcSpanLess (LConDecl GhcPs)
x) -> SrcSpan -> SrcSpanLess (LConDecl GhcPs) -> LConDecl GhcPs
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
LL SrcSpan
consloc (SrcSpanLess (LConDecl GhcPs) -> LConDecl GhcPs)
-> SrcSpanLess (LConDecl GhcPs) -> LConDecl GhcPs
forall a b. (a -> b) -> a -> b
$ ConDecl GhcPs -> ConDecl GhcPs
dropConsBang SrcSpanLess (LConDecl GhcPs)
ConDecl GhcPs
x) ([LConDecl GhcPs] -> [LConDecl GhcPs])
-> [LConDecl GhcPs] -> [LConDecl GhcPs]
forall a b. (a -> b) -> a -> b
$ HsDataDefn GhcPs -> [LConDecl GhcPs]
forall pass. HsDataDefn pass -> [LConDecl pass]
dd_cons HsDataDefn GhcPs
dataDef
}}
, insideType :: HsType GhcPs
insideType = HsType GhcPs
inType
}
singleSimpleField _ = Maybe WarnNewtype
forall a. Maybe a
Nothing
simpleCons :: ConDecl GhcPs -> Maybe (HsType GhcPs)
simpleCons :: ConDecl GhcPs -> Maybe (HsType GhcPs)
simpleCons (ConDeclH98 _ _ _ [] context :: Maybe (LHsContext GhcPs)
context (PrefixCon [LL _ inType :: SrcSpanLess (LBangType GhcPs)
inType]) _)
| Maybe (LHsContext GhcPs) -> Bool
emptyOrNoContext Maybe (LHsContext GhcPs)
context
, Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ HsType GhcPs -> Bool
isUnboxedTuple SrcSpanLess (LBangType GhcPs)
HsType GhcPs
inType
, Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ HsType GhcPs -> Bool
isHashy SrcSpanLess (LBangType GhcPs)
HsType GhcPs
inType
= HsType GhcPs -> Maybe (HsType GhcPs)
forall a. a -> Maybe a
Just SrcSpanLess (LBangType GhcPs)
HsType GhcPs
inType
simpleCons (ConDeclH98 _ _ _ [] context :: Maybe (LHsContext GhcPs)
context (RecCon (LL _ [LL _ (ConDeclField _ [_] (LL _ inType) _)])) _)
| Maybe (LHsContext GhcPs) -> Bool
emptyOrNoContext Maybe (LHsContext GhcPs)
context
, Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ HsType GhcPs -> Bool
isUnboxedTuple SrcSpanLess (LBangType GhcPs)
HsType GhcPs
inType
, Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ HsType GhcPs -> Bool
isHashy SrcSpanLess (LBangType GhcPs)
HsType GhcPs
inType
= HsType GhcPs -> Maybe (HsType GhcPs)
forall a. a -> Maybe a
Just SrcSpanLess (LBangType GhcPs)
HsType GhcPs
inType
simpleCons _ = Maybe (HsType GhcPs)
forall a. Maybe a
Nothing
isHashy :: HsType GhcPs -> Bool
isHashy :: HsType GhcPs -> Bool
isHashy (HsTyVar _ _ identifier :: Located (IdP GhcPs)
identifier) = "#" String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isSuffixOf` SDoc -> String
showSDocUnsafe (Located RdrName -> SDoc
forall a. Outputable a => a -> SDoc
ppr Located RdrName
Located (IdP GhcPs)
identifier)
isHashy _ = Bool
False
warnBang :: HsType GhcPs -> Bool
warnBang :: HsType GhcPs -> Bool
warnBang (HsBangTy _ (HsSrcBang _ _ SrcStrict) _) = Bool
False
warnBang _ = Bool
True
emptyOrNoContext :: Maybe (LHsContext GhcPs) -> Bool
emptyOrNoContext :: Maybe (LHsContext GhcPs) -> Bool
emptyOrNoContext Nothing = Bool
True
emptyOrNoContext (Just (LL _ [])) = Bool
True
emptyOrNoContext _ = Bool
False
dropConsBang :: ConDecl GhcPs -> ConDecl GhcPs
dropConsBang :: ConDecl GhcPs -> ConDecl GhcPs
dropConsBang decl :: ConDecl GhcPs
decl@(ConDeclH98 _ _ _ _ _ (PrefixCon fields :: [LBangType GhcPs]
fields) _) =
ConDecl GhcPs
decl {con_args :: HsConDetails (LBangType GhcPs) (Located [LConDeclField GhcPs])
con_args = [LBangType GhcPs]
-> HsConDetails (LBangType GhcPs) (Located [LConDeclField GhcPs])
forall arg rec. [arg] -> HsConDetails arg rec
PrefixCon ([LBangType GhcPs]
-> HsConDetails (LBangType GhcPs) (Located [LConDeclField GhcPs]))
-> [LBangType GhcPs]
-> HsConDetails (LBangType GhcPs) (Located [LConDeclField GhcPs])
forall a b. (a -> b) -> a -> b
$ (LBangType GhcPs -> LBangType GhcPs)
-> [LBangType GhcPs] -> [LBangType GhcPs]
forall a b. (a -> b) -> [a] -> [b]
map LBangType GhcPs -> LBangType GhcPs
forall a. LHsType a -> LHsType a
getBangType [LBangType GhcPs]
fields}
dropConsBang decl :: ConDecl GhcPs
decl@(ConDeclH98 _ _ _ _ _ (RecCon (LL recloc :: SrcSpan
recloc conDeclFields :: SrcSpanLess (Located [LConDeclField GhcPs])
conDeclFields)) _) =
ConDecl GhcPs
decl {con_args :: HsConDetails (LBangType GhcPs) (Located [LConDeclField GhcPs])
con_args = Located [LConDeclField GhcPs]
-> HsConDetails (LBangType GhcPs) (Located [LConDeclField GhcPs])
forall arg rec. rec -> HsConDetails arg rec
RecCon (Located [LConDeclField GhcPs]
-> HsConDetails (LBangType GhcPs) (Located [LConDeclField GhcPs]))
-> Located [LConDeclField GhcPs]
-> HsConDetails (LBangType GhcPs) (Located [LConDeclField GhcPs])
forall a b. (a -> b) -> a -> b
$ SrcSpan
-> SrcSpanLess (Located [LConDeclField GhcPs])
-> Located [LConDeclField GhcPs]
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
cL SrcSpan
recloc (SrcSpanLess (Located [LConDeclField GhcPs])
-> Located [LConDeclField GhcPs])
-> SrcSpanLess (Located [LConDeclField GhcPs])
-> Located [LConDeclField GhcPs]
forall a b. (a -> b) -> a -> b
$ [LConDeclField GhcPs] -> [LConDeclField GhcPs]
removeUnpacksRecords [LConDeclField GhcPs]
SrcSpanLess (Located [LConDeclField GhcPs])
conDeclFields}
where
removeUnpacksRecords :: [LConDeclField GhcPs] -> [LConDeclField GhcPs]
removeUnpacksRecords :: [LConDeclField GhcPs] -> [LConDeclField GhcPs]
removeUnpacksRecords = (LConDeclField GhcPs -> LConDeclField GhcPs)
-> [LConDeclField GhcPs] -> [LConDeclField GhcPs]
forall a b. (a -> b) -> [a] -> [b]
map (\(LL conDeclFieldLoc :: SrcSpan
conDeclFieldLoc x :: SrcSpanLess (LConDeclField GhcPs)
x) -> SrcSpan -> SrcSpanLess (LConDeclField GhcPs) -> LConDeclField GhcPs
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
LL SrcSpan
conDeclFieldLoc (SrcSpanLess (LConDeclField GhcPs) -> LConDeclField GhcPs)
-> SrcSpanLess (LConDeclField GhcPs) -> LConDeclField GhcPs
forall a b. (a -> b) -> a -> b
$ ConDeclField GhcPs -> ConDeclField GhcPs
removeConDeclFieldUnpacks SrcSpanLess (LConDeclField GhcPs)
ConDeclField GhcPs
x)
removeConDeclFieldUnpacks :: ConDeclField GhcPs -> ConDeclField GhcPs
removeConDeclFieldUnpacks :: ConDeclField GhcPs -> ConDeclField GhcPs
removeConDeclFieldUnpacks conDeclField :: ConDeclField GhcPs
conDeclField@(ConDeclField _ _ fieldType :: LBangType GhcPs
fieldType _) =
ConDeclField GhcPs
conDeclField {cd_fld_type :: LBangType GhcPs
cd_fld_type = LBangType GhcPs -> LBangType GhcPs
forall a. LHsType a -> LHsType a
getBangType LBangType GhcPs
fieldType}
removeConDeclFieldUnpacks x :: ConDeclField GhcPs
x = ConDeclField GhcPs
x
dropConsBang x :: ConDecl GhcPs
x = ConDecl GhcPs
x
isUnboxedTuple :: HsType GhcPs -> Bool
isUnboxedTuple :: HsType GhcPs -> Bool
isUnboxedTuple (HsTupleTy _ HsUnboxedTuple _) = Bool
True
isUnboxedTuple _ = Bool
False