diff --git a/src/imports.rs b/src/imports.rs index cfc2f3bda19..9c3a943be58 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -845,9 +845,18 @@ fn merge_use_trees_inner(trees: &mut Vec, 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 { diff --git a/tests/source/5131_one.rs b/tests/source/5131_one.rs index 61ddf13410d..91d5fb0de95 100644 --- a/tests/source/5131_one.rs +++ b/tests/source/5131_one.rs @@ -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; diff --git a/tests/target/5131_one.rs b/tests/target/5131_one.rs index a086dae5a42..61ab9f3b8b3 100644 --- a/tests/target/5131_one.rs +++ b/tests/target/5131_one.rs @@ -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}, };