Skip to content

Commit

Permalink
fix(import_granularity): do not merge aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
sivizius committed Nov 27, 2024
1 parent 8cb2820 commit 4763a59
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,9 +845,18 @@ fn merge_use_trees_inner(trees: &mut Vec<UseTree>, use_tree: UseTree, merge_by:
// tree `use_tree` should be merge.
// In other cases `similarity` won't be used, so set it to `0` as a dummy value.
let similarity = if merge_by == SharedPrefix::One {
let tree_depth = tree.path.len();

// Only merge prefixes, not leaves,
// e.g. `foo::i` with `foo::i as j`.
let max_depth = (tree_depth == use_tree.path.len())
.then_some(tree_depth - 1)
.unwrap_or(usize::MAX);

tree.path
.iter()
.zip(&use_tree.path)
.take(max_depth)
.take_while(|(a, b)| a.equal_except_alias(b))
.count()
} else {
Expand Down
1 change: 1 addition & 0 deletions tests/source/5131_one.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ use bar::d::e;
use bar::d::e as e2;
use qux::h;
use qux::i;
use qux::i as j;
4 changes: 2 additions & 2 deletions tests/target/5131_one.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ pub use foo::{x, x as x2, y};
use {
bar::{
a,
b::{self, f, g},
b::{self, f, f as f2, g},
c,
d::{e, e as e2},
},
qux::{h, i},
qux::{h, i, i as j},
};

0 comments on commit 4763a59

Please sign in to comment.