Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dont suggest const when NamedFieldPuns are used #1521

Merged
merged 5 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
1453 Use FreeVars instead of Wildcard
  • Loading branch information
batkot committed Jun 12, 2023
commit 53e6d48e7cd2c70c1054624d539a1795dd53c4b5
2 changes: 2 additions & 0 deletions data/hlint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,8 @@
# yes = a & (mapped . b) %~ c -- a <&> b %~ c
# test a = foo (\x -> True) -- const True
# test a = foo (\_ -> True) -- const True
# {-# LANGUAGE NamedFieldPuns #-}; data Foo = Foo {foo :: Int}; issue1430_ctor x = f (\foo-> Foo{foo})
# {-# LANGUAGE NamedFieldPuns #-}; data Foo = Foo {foo :: Int}; issue1430_update x = f (\foo -> x{foo})
# test a = foo (\x -> x) -- id
# h a = flip f x (y z) -- f (y z) x
# h a = flip f x $ y z
Expand Down
1 change: 1 addition & 0 deletions src/GHC/Util/FreeVars.hs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ instance FreeVars (LocatedA (HsFieldBind (LocatedAn NoEpAnns (FieldOcc GhcPs)) (
freeVars o@(L _ (HsFieldBind _ _ x _)) = freeVars x

instance FreeVars (LocatedA (HsFieldBind (LocatedAn NoEpAnns (AmbiguousFieldOcc GhcPs)) (LocatedA (HsExpr GhcPs)))) where
freeVars (L _ (HsFieldBind _ x _ True)) = Set.singleton $ rdrNameOcc $ rdrNameAmbiguousFieldOcc $ unLoc x -- a pun
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I didn't know about rdrNameAmbiguousFieldOcc!

freeVars (L _ (HsFieldBind _ _ x _)) = freeVars x

instance FreeVars (LocatedA (HsFieldBind (LocatedAn NoEpAnns (FieldLabelStrings GhcPs)) (LocatedA (HsExpr GhcPs)))) where
Expand Down
2 changes: 1 addition & 1 deletion src/Hint/Match.hs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ checkSide x bind = maybe True bool x
isType "Compare" x = True -- Just a hint for proof stuff
isType "Atom" x = isAtom x
isType "WHNF" x = isWHNF x
isType "Wildcard" x = any isFieldPun (universeBi x) || any hasFieldsDotDot (universeBi x) || any isFieldPunUpdate (universeBi x)
isType "Wildcard" x = any isFieldPun (universeBi x) || any hasFieldsDotDot (universeBi x)
isType "Nat" (asInt -> Just x) | x >= 0 = True
isType "Pos" (asInt -> Just x) | x > 0 = True
isType "Neg" (asInt -> Just x) | x < 0 = True
Expand Down
23 changes: 0 additions & 23 deletions tests/hint.test
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,6 @@ Perhaps:

1 hint

---------------------------------------------------------------------
RUN tests/use-const.hs
FILE tests/use-const.hs
test = f (\x -> y)
OUTPUT
tests/use-const.hs:1:11-17: Suggestion: Use const
Found:
\ x -> y
Perhaps:
const y

1 hint

---------------------------------------------------------------------
RUN tests/use-const-named-puns.hs
FILE tests/use-const-named-puns.hs
{-# LANGUAGE NamedFieldPuns #-}
data Y = Y
data X = X {foo :: Y,bar :: Y}
test x = f (\foo -> x{foo})
OUTPUT
No hints

---------------------------------------------------------------------
RUN tests/typesig-ignore.hs
FILE tests/typesig-ignore.hs
Expand Down