Skip to content

Commit 3859cc6

Browse files
authored
Allow Avoid lambda to trigger in more cases (#1660)
1 parent 8452bdb commit 3859cc6

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

src/Hint/All.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ builtin x = case x of
7474
wrap = timed "Hint" (drop 4 $ show x) . forceList
7575
decl f = mempty{hintDecl=const $ \a b c -> wrap $ f a b c}
7676
modu f = mempty{hintModule=const $ \a b -> wrap $ f a b}
77-
mods f = mempty{hintModules=const $ \a -> wrap $ f a}
77+
mods f = mempty{hintModules=const $ wrap . f}
7878

7979
-- | A list of builtin hints, currently including entries such as @\"List\"@ and @\"Bracket\"@.
8080
builtinHints :: [(String, Hint)]

src/Hint/Lambda.hs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ f = \z -> foo $ bar x $ baz z where -- foo . bar x . baz
7777
f = \z -> foo $ z $ baz z where
7878
f = \x -> bar map (filter x) where -- bar map . filter
7979
f = bar &+& \x -> f (g x)
80+
f = bar &+& \x -> f x -- f
81+
f = bar $ \x -> f (g x) -- f . g
8082
foo = [\column -> set column [treeViewColumnTitle := printf "%s (match %d)" name (length candidnates)]]
8183
foo = [\x -> x]
8284
foo = [\m x -> insert x x m]
@@ -221,9 +223,14 @@ lambdaExp _ o@(L _ (HsPar _ (view -> App2 (view -> Var_ "flip") origf@(view -> R
221223
r = Replace Expr (toSSA o) [(var, toSSA y)] ("(" ++ op ++ " " ++ var ++ ")")
222224

223225
lambdaExp p o@(L _ (HsLam _ LamSingle _))
224-
| not $ any isOpApp p
225-
, (res, refact) <- niceLambdaR p [] o
226+
| (res, refact) <- niceLambdaR p [] o
226227
, not $ isLambda res
228+
-- Do not suggest "Avoid lambda" if both the parent and the result are `OpApps`.
229+
-- For example, this should be avoided: `bar &+& \x -> f (g x)` ==> `bar &+& f . g`,
230+
-- since it may not be valid depending on the precedence of `&+&`.
231+
-- An exception is when the parent is `$`. Since `$` has the lowest precedence, it is
232+
-- always safe to apply this hint.
233+
, not (any isOpApp p) || not (isOpApp res) || any isDollarApp p
227234
, not $ any isQuasiQuoteExpr $ universe res
228235
, not $ "runST" `Set.member` Set.map occNameString (freeVars o)
229236
, let name = "Avoid lambda" ++ (if countRightSections res > countRightSections o then " using `infix`" else "")
@@ -322,6 +329,11 @@ lambdaExp _ _ = []
322329
varBody :: LHsExpr GhcPs
323330
varBody = strToVar "body"
324331

332+
isDollarApp :: LHsExpr GhcPs -> Bool
333+
isDollarApp = \case
334+
(L _ (OpApp _ _ op _)) -> isDol op
335+
_ -> False
336+
325337
-- | Squash lambdas and replace any repeated pattern variable with @_@
326338
fromLambda :: LHsExpr GhcPs -> ([LPat GhcPs], LHsExpr GhcPs)
327339
fromLambda (SimpleLambda ps1 (fromLambda . fromParen -> (ps2,x))) = (transformBi (f $ pvars ps2) ps1 ++ ps2, x)

src/Hint/Monad.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ monadExp decl parentDo parentExpr x =
135135
_ -> []
136136
where
137137
f = monadNoResult (fromMaybe "" decl) id
138-
seenVoid wrap (L l (HsPar x y)) = seenVoid (wrap . L l . \y -> HsPar x y) y
138+
seenVoid wrap (L l (HsPar x y)) = seenVoid (wrap . L l . HsPar x) y
139139
seenVoid wrap x =
140140
-- Suggest `traverse_ f x` given `void $ traverse_ f x`
141141
[warn "Redundant void" (reLoc (wrap x)) (reLoc x) [Replace Expr (toSSA (wrap x)) [("a", toSSA x)] "a"] | returnsUnit x]
@@ -189,7 +189,7 @@ modifyAppHead :: forall a. (LIdP GhcPs -> (LIdP GhcPs, a)) -> LHsExpr GhcPs -> (
189189
modifyAppHead f = go id
190190
where
191191
go :: (LHsExpr GhcPs -> LHsExpr GhcPs) -> LHsExpr GhcPs -> (LHsExpr GhcPs, Maybe a)
192-
go wrap (L l (HsPar _ x)) = go (wrap . L l . \y -> HsPar noAnn y) x
192+
go wrap (L l (HsPar _ x)) = go (wrap . L l . HsPar noAnn) x
193193
go wrap (L l (HsApp _ x y)) = go (\x -> wrap $ L l (HsApp noExtField x y)) x
194194
go wrap (L l (OpApp _ x op y)) | isDol op = go (\x -> wrap $ L l (OpApp noExtField x op y)) x
195195
go wrap (L l (HsVar _ x)) = (wrap (L l (HsVar NoExtField x')), Just a)

0 commit comments

Comments
 (0)