[Use fewer imports]: combine field imports for same imported type, class, etc. #1133 #1654
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Combine multiple fields for any imported type, constructor, or class (this is called a "thing" in the GHC API).
Suppose a type, constructor, or class with fields will appear in at most one non-hiding import declaration (as guaranteed by
simplify
). The "use fewer imports" hint will combine multiple field imports into a single import list element.Hiding imports are left untouched.
For example:
would first
simplify
to:and then
simplifyFields
would yield:To sort or not to sort?
Note that sorting occurs only when field imports are combined.
The import list itself preserves the order of the types, etc. in the original imports. It also leaves field imports unsorted if there's only one import list element for that type, class, or constructor. The latter is easy (just don't change it), but the former requires a kind of stable nub.
We could use a similar stable nub to preserve the field import order from the original too.
Is that worth doing?
Test Plan
Unit tests
Added several cases to the section in Hint.Import:
~/Work/hlint $ cabal run -- hlint --test ... Testing (with refactoring) Source annotations .................... Input/outputs .......................................................................................................................................................................... Hint names ........ Hint annotations ........ Tests passed (975)
Running this hlint on hlint src
~/Work/hlint $ cabal run hlint -- src --report Writing report to report.html ... No hints