Skip to content

Commit

Permalink
Don't merge crates when imports_granularity="Module"
Browse files Browse the repository at this point in the history
When merging imports using `imports_granularity="Module"`, individual
crate imports should get their own `use` statement on a new line.

```
// Before this commit:
use {library1, library2 as lib2, library3};

// After this commit:
use library1;
use library2 as lib2;
use library3;
```

Fixes: rust-lang#6191
  • Loading branch information
mnkhouri committed Jun 11, 2024
1 parent c97996f commit bb85c12
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,12 @@ impl UseTree {
match shared_prefix {
SharedPrefix::Crate => self.path[0] == other.path[0],
SharedPrefix::Module => {
self.path[..self.path.len() - 1] == other.path[..other.path.len() - 1]
if self.path.len() == 1 && other.path.len() == 1 {
// When importing an entire crate, use the crate name for comparison.
self.path[0] == other.path[0]
} else {
self.path[..self.path.len() - 1] == other.path[..other.path.len() - 1]
}
}
SharedPrefix::One => true,
}
Expand Down
4 changes: 4 additions & 0 deletions tests/source/imports/imports_granularity_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ use b::v::{
};
use b::t::{/* Before b::t::self */ self};
use b::c;

// Issue #6191: grouping of top-level modules
use library1;
use {library2 as lib2, library3};
4 changes: 4 additions & 0 deletions tests/source/issue-6191.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// rustfmt-imports_granularity: Module

use library1;
use {library2 as lib2, library3};
5 changes: 5 additions & 0 deletions tests/target/imports/imports_granularity_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,8 @@ use b::{
/* Before b::l group */ l::{self, m, n::o, p::*},
q,
};

// Issue #6191: grouping of top-level modules
use library1;
use library2 as lib2;
use library3;
5 changes: 5 additions & 0 deletions tests/target/issue-6191.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// rustfmt-imports_granularity: Module

use library1;
use library2 as lib2;
use library3;

0 comments on commit bb85c12

Please sign in to comment.