Fix duplicate import removal for imports_granularity="Module" #6677
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.
Fixes #6243 - Strange behavior for duplicate imports with imports_granularity="Module"
Problem
When using
imports_granularity="Module"
, duplicate imports were being merged but not deduplicated, requiring multiple formatting passes to reach a stable state. This was inconsistent with other granularity settings which properly remove duplicates.Solution
Added
.dedup()
calls after sorting import lists in three locations:merge_rest()
- when creating merged import listsUseTree::normalize()
- when normalizing nested importsmerge_use_trees_inner()
- when merging use treesThis ensures duplicate imports are removed in a single formatting pass, matching the behavior of
Crate
andItem
granularity settings.Test Changes
Updated
tests/target/multiple.rs
to expect deduplicated imports. I believe the previous test was documenting the buggy behavior rather than the expected behavior.Example
Before (unstable, requires 2 passes):
After (stable, single pass):