diff --git a/src/formatting/imports.rs b/src/formatting/imports.rs index f1cac2bcf05..8595c73c38c 100644 --- a/src/formatting/imports.rs +++ b/src/formatting/imports.rs @@ -179,6 +179,16 @@ pub(crate) fn merge_use_trees(use_trees: Vec, merge_by: SharedPrefix) - } } } + for merged in result.iter_mut() { + // If a path ends with `::self`, rewrite it to `::{self}`. + if let Some(UseSegment::Slf(..)) = merged.path.last_mut() { + let slf_segment = merged.path.pop().unwrap(); + merged.path.push(UseSegment::List(vec![UseTree::from_path( + vec![slf_segment], + DUMMY_SP, + )])); + } + } result } @@ -550,11 +560,6 @@ impl UseTree { fn flatten(self) -> Vec { match self.path.last() { Some(UseSegment::List(list)) => { - if list.len() == 1 && list[0].path.len() == 1 { - if let UseSegment::Slf(..) = list[0].path[0] { - return vec![self]; - }; - } let prefix = &self.path[..self.path.len() - 1]; let mut result = vec![]; for nested_use_tree in list { @@ -1086,7 +1091,13 @@ mod test { test_merge!( NoPrefix, ["foo::{self, a, b::{c, d}, e::*}"], - ["foo::self", "foo::a", "foo::b::c", "foo::b::d", "foo::e::*"] + [ + "foo::{self}", + "foo::a", + "foo::b::c", + "foo::b::d", + "foo::e::*" + ] ) } diff --git a/src/formatting/reorder.rs b/src/formatting/reorder.rs index a65c94eecf8..bd49089788b 100644 --- a/src/formatting/reorder.rs +++ b/src/formatting/reorder.rs @@ -238,11 +238,6 @@ fn rewrite_reorderable_or_regroupable_items( } ImportGranularity::Preserve => normalized_items, }; - for item in normalized_items.iter_mut() { - if let Some(UseSegment::Slf(None)) = item.path.last() { - item.path.pop().unwrap(); - } - } let mut regrouped_items = match context.config.group_imports() { GroupImportsTactic::Preserve => vec![normalized_items], diff --git a/tests/target/imports_granularity_item.rs b/tests/target/imports_granularity_item.rs index a6602ef33d9..eace785e670 100644 --- a/tests/target/imports_granularity_item.rs +++ b/tests/target/imports_granularity_item.rs @@ -6,8 +6,8 @@ use a::d; use a::f::g; use a::h::i; use a::h::j; -use a::l; use a::l::m; use a::l::n::o; use a::l::p::*; +use a::l::{self}; use a::q::{self};