{-# LANGUAGE ViewPatterns, PatternGuards #-}
{-
Find and match:
<TEST>
yes = 1:2:[] -- [1,2]
yes = ['h','e','l','l','o'] -- "hello"
-- [a]++b -> a : b, but only if not in a chain of ++'s
yes = [x] ++ xs -- x : xs
no = "x" ++ xs
no = [x] ++ xs ++ ys
no = xs ++ [x] ++ ys
yes = [if a then b else c] ++ xs -- (if a then b else c) : xs
yes = [1] : [2] : [3] : [4] : [5] : [] -- [[1], [2], [3], [4], [5]]
yes = if x == e then l2 ++ xs else [x] ++ check_elem xs -- x : check_elem xs
data Yes = Yes (Maybe [Char]) -- Maybe String
yes = y :: [Char] -> a -- String -> a
instance C [Char]
foo = [a b] ++ xs -- a b : xs
</TEST>
-}
module Hint.List(listHint) where
import Control.Applicative
import Hint.Type
listHint :: DeclHintlistHint _ _ = listDecllistDecl :: Decl_ -> [Idea]
listDeclx = concatMap (listExpFalse) (childrenBix) ++stringTypex-- boolean = are you in a ++ chainlistExp :: Bool -> Exp_ -> [Idea]
listExpb (fromParen -> x) =
if nullres then concatMap (listExp$isAppendx) $childrenx else [headres]
where
res = [warnnamexx2 | (name,f) <- checks, Justx2 <- [fbx]]
isAppend (view -> App2 op _ _) = op~="++"isAppend _ = Falsechecks = let (*) = (,) in
["Use string literal"*useString
,"Use list literal"*useList
,"Use :"*useCons
]
useStringb (List _ xs) | xs/= [] &&allisCharxs = Just$Litan$Stringans (shows)
where s = mapfromCharxsuseStringb _ = NothinguseListb = fmap (Listan) .fTrue
where
ffirstx | x~="[]" = if first then Nothing else Just []
ffirst (view -> App2 c a b) | c~=":" = (a:) <$>fFalsebffirst _ = NothinguseConsFalse (view -> App2 op x y) | op~="++", Justx2 <- fx, not$isAppendy =
Just$InfixAppanx2 (QConOpan$list_cons_namean) y
where
f (List _ [x]) = Just$ if isAppx then x else parenxf _ = NothinguseCons _ _ = NothingtypeListChar = TyListan (TyConan (toNamed"Char"))
typeString = TyConan (toNamed"String")
stringType :: Decl_ -> [Idea]
stringTypex = case x of
InstDecl _ _ _ x -> fx
_ -> fx
where
fx = concatMapg$childrenBixg :: Type_ -> [Idea]
g (fromTyParen -> x) = [warn"Use String"x (transformfx) | any (=~=typeListChar) $universex]
where fx = if x=~=typeListChar then typeString else x