Enable automatic import merging by default #6662
Closed
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.
This PR implements automatic merging of related use statements from the same crate or module, addressing the feature request to improve code readability and reduce import duplication.
Problem
Currently, rustfmt does not automatically merge multiple use statements from the same crate or module. For example:
Would remain unchanged instead of being merged into a cleaner, more idiomatic format.
Solution
Changed the default
ImportGranularity
setting fromPreserve
toCrate
, enabling automatic import merging by default. This leverages existing, well-tested functionality that was previously opt-in.Results
With this change, the above imports are now automatically formatted as:
More complex examples also work correctly:
Compatibility
imports_granularity
settings continue to work exactly as beforeimports_granularity = "Preserve"
in their configurationBenefits
This change makes rustfmt more helpful out of the box while maintaining full backward compatibility for users with specific formatting preferences.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.