@@ -77,6 +77,8 @@ f = \z -> foo $ bar x $ baz z where -- foo . bar x . baz
7777f = \z -> foo $ z $ baz z where
7878f = \x -> bar map (filter x) where -- bar map . filter
7979f = bar &+& \x -> f (g x)
80+ f = bar &+& \x -> f x -- f
81+ f = bar $ \x -> f (g x) -- f . g
8082foo = [\column -> set column [treeViewColumnTitle := printf "%s (match %d)" name (length candidnates)]]
8183foo = [\x -> x]
8284foo = [\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
223225lambdaExp 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 _ _ = []
322329varBody :: LHsExpr GhcPs
323330varBody = 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 @_@
326338fromLambda :: LHsExpr GhcPs -> ([LPat GhcPs ], LHsExpr GhcPs )
327339fromLambda (SimpleLambda ps1 (fromLambda . fromParen -> (ps2,x))) = (transformBi (f $ pvars ps2) ps1 ++ ps2, x)
0 commit comments