-
Notifications
You must be signed in to change notification settings - Fork 733
Hlint: suggest nub alternative #11825
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
base: master
Are you sure you want to change the base?
Changes from all commits
167449f
7aa9b7c
b9c3971
ace504f
0831be3
632ff82
6b90b24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ import Distribution.Compat.Prelude | |||||||||||||||||||||||||||||||||||||||||||
| import Prelude () | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| import Distribution.PackageDescription.Check | ||||||||||||||||||||||||||||||||||||||||||||
| import Distribution.Simple.Utils (ordNub) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| import Test.Tasty | ||||||||||||||||||||||||||||||||||||||||||||
| import Test.Tasty.HUnit | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -26,7 +27,7 @@ tests = | |||||||||||||||||||||||||||||||||||||||||||
| allExplanationIdStrings = map ppCheckExplanationId [minBound..maxBound] | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| uniqueNames :: Bool | ||||||||||||||||||||||||||||||||||||||||||||
| uniqueNames = length allExplanationIdStrings == length (nub allExplanationIdStrings) | ||||||||||||||||||||||||||||||||||||||||||||
| uniqueNames = length allExplanationIdStrings == length (ordNub allExplanationIdStrings) | ||||||||||||||||||||||||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a regression, but this is wasteful. Would be nice to have a helper
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please be more specific? -- Like `uniqueNames` in the way it is implemented but taking a `[a]`.
areUnique :: Ord a => [a] -> Bool
areUnique xs = length xs == length (ordNub xs)I went looking in this repository for other cabal/Cabal-tests/tests/UnitTests/Distribution/Compat/Graph.hs Lines 64 to 71 in e7aaad8
cabal/cabal-install/tests/UnitTests/Distribution/Client/UserConfig.hs Lines 93 to 96 in e7aaad8
cabal/cabal-install/tests/UnitTests/Distribution/Solver/Modular/WeightedPSQ.hs Lines 52 to 54 in e7aaad8
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also found these two doing the same thing, the opposite of all items being unique, all items being duplicates: cabal/solver-benchmarks/HackageBenchmark.hs Lines 351 to 352 in e7aaad8
cabal/cabal-install-solver/src/Distribution/Solver/Modular/Linking.hs Lines 518 to 521 in e7aaad8
Other implementations found hoogling: allEqual :: Eq a => [a] -> Bool
allEqual [] = True
allEqual (x : xs) = all (== x) xsallSame :: Eq a => [a] -> Bool
allSame [] = True
allSame (x:xs) = all (x ==) xs
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean that It should be something like isUnique :: [a] -> Bool
isUnique = go S.empty
where
go !_ [] = True
go seen (x : xs) = if S.member x seen then False else go (S.insert x seen) xs
Up to you whether you want to fix these things in this PR, it's indeed a bit of scope creep. |
||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| longerThan :: [CheckExplanationIDString] | ||||||||||||||||||||||||||||||||||||||||||||
| longerThan = filter ((>25). length) allExplanationIdStrings | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -130,7 +130,7 @@ data BuildTarget | |
| BuildTargetModule ComponentName ModuleName | ||
| | -- | A specific file within a specific component. | ||
| BuildTargetFile ComponentName FilePath | ||
| deriving (Eq, Show, Generic) | ||
| deriving (Eq, Ord, Show, Generic) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally this deserves |
||
|
|
||
| instance Binary BuildTarget | ||
|
|
||
|
|
@@ -853,7 +853,7 @@ type Confidence = Int | |
| data MatchError | ||
| = MatchErrorExpected String String | ||
| | MatchErrorNoSuch String String | ||
| deriving (Show, Eq) | ||
| deriving (Show, Eq, Ord) | ||
|
|
||
| instance Alternative Match where | ||
| empty = mzero | ||
|
|
@@ -943,13 +943,13 @@ increaseConfidence = ExactMatch 1 [()] | |
| increaseConfidenceFor :: Match a -> Match a | ||
| increaseConfidenceFor m = m >>= \r -> increaseConfidence >> return r | ||
|
|
||
| nubMatches :: Eq a => Match a -> Match a | ||
| nubMatches :: Ord a => Match a -> Match a | ||
| nubMatches (NoMatch d msgs) = NoMatch d msgs | ||
| nubMatches (ExactMatch d xs) = ExactMatch d (nub xs) | ||
| nubMatches (InexactMatch d xs) = InexactMatch d (nub xs) | ||
| nubMatches (ExactMatch d xs) = ExactMatch d (ordNub xs) | ||
| nubMatches (InexactMatch d xs) = InexactMatch d (ordNub xs) | ||
|
|
||
| nubMatchErrors :: Match a -> Match a | ||
| nubMatchErrors (NoMatch d msgs) = NoMatch d (nub msgs) | ||
| nubMatchErrors (NoMatch d msgs) = NoMatch d (ordNub msgs) | ||
| nubMatchErrors (ExactMatch d xs) = ExactMatch d xs | ||
| nubMatchErrors (InexactMatch d xs) = InexactMatch d xs | ||
|
|
||
|
|
@@ -970,14 +970,14 @@ tryEach = exactMatches | |
| -- | Given a matcher and a key to look up, use the matcher to find all the | ||
| -- possible matches. There may be 'None', a single 'Unambiguous' match or | ||
| -- you may have an 'Ambiguous' match with several possibilities. | ||
| findMatch :: Eq b => Match b -> MaybeAmbiguous b | ||
| findMatch :: Ord b => Match b -> MaybeAmbiguous b | ||
| findMatch match = | ||
| case match of | ||
| NoMatch _ msgs -> None (nub msgs) | ||
| NoMatch _ msgs -> None (ordNub msgs) | ||
| ExactMatch _ xs -> checkAmbiguous xs | ||
| InexactMatch _ xs -> checkAmbiguous xs | ||
| where | ||
| checkAmbiguous xs = case nub xs of | ||
| checkAmbiguous xs = case ordNub xs of | ||
| [x] -> Unambiguous x | ||
| xs' -> Ambiguous xs' | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess the sort no longer needed
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not quite:
ordNubdoes not change the order of elements, so it's not semantically equivalent to dropsort. (I don't know whether sorting makes any difference for the purposes of this function and elsewhere, but perhaps we can leave figuring this out for another time)