Skip to content

Conversation

tqwewe
Copy link

@tqwewe tqwewe commented Oct 6, 2025

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 lists
  • UseTree::normalize() - when normalizing nested imports
  • merge_use_trees_inner() - when merging use trees

This ensures duplicate imports are removed in a single formatting pass, matching the behavior of Crate and Item 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):

use risingwave_batch::executor::{BoxedExecutor, JoinType};
use risingwave_batch::executor::{BoxedExecutor, JoinType};
// First pass: {BoxedExecutor, BoxedExecutor, JoinType, JoinType}
// Second pass: {BoxedExecutor, JoinType}

After (stable, single pass):

use risingwave_batch::executor::{BoxedExecutor, JoinType};

Copy link
Member

@Manishearth Manishearth left a comment

Choose a reason for hiding this comment

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

In any situation where rustfmt produces a duplicate list, that resultant code will not compile. Thus this change cannot be breaking.

extern crate foo;

use std::cell::*;
use std::{
Copy link
Member

Choose a reason for hiding this comment

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

Hmm. Should we have this test at all? This original code won't compile, and now the use tree is much smaller. We should tweak this to import a lot of items

cc @calebcartwright if you have an idea as to what this test was doing before

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

strange behavior for duplicate imports with imports_granularity="Module"
3 participants