diff --git a/src/formatting/imports.rs b/src/formatting/imports.rs index 17429028168..f1cac2bcf05 100644 --- a/src/formatting/imports.rs +++ b/src/formatting/imports.rs @@ -561,6 +561,11 @@ impl UseTree { for flattened in &mut nested_use_tree.clone().flatten() { let mut new_path = prefix.to_vec(); new_path.append(&mut flattened.path); + if flattened.path.len() == 1 { + if let UseSegment::Slf(..) = flattened.path[0] { + new_path.pop(); + } + } result.push(UseTree { path: new_path, span: self.span, diff --git a/src/formatting/reorder.rs b/src/formatting/reorder.rs index bd49089788b..193697f8ddd 100644 --- a/src/formatting/reorder.rs +++ b/src/formatting/reorder.rs @@ -238,6 +238,11 @@ 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(); + } + } let mut regrouped_items = match context.config.group_imports() { GroupImportsTactic::Preserve => vec![normalized_items], diff --git a/tests/source/imports_granularity_item.rs b/tests/source/imports_granularity_item.rs new file mode 100644 index 00000000000..d0e94df66ae --- /dev/null +++ b/tests/source/imports_granularity_item.rs @@ -0,0 +1,6 @@ +// rustfmt-imports_granularity: Item + +use a::{b, c, d}; +use a::{f::g, h::{i, j}}; +use a::{l::{self, m, n::o, p::*}}; +use a::q::{self}; diff --git a/tests/target/imports_granularity_item.rs b/tests/target/imports_granularity_item.rs index d2526081be6..a6602ef33d9 100644 --- a/tests/target/imports_granularity_item.rs +++ b/tests/target/imports_granularity_item.rs @@ -1,23 +1,13 @@ // rustfmt-imports_granularity: Item -use a::b::c; -use a::d::e; -use a::f; -use a::g::h; -use a::g::i; -use a::j; -use a::j::k; -use a::j::k::l; -use a::j::m; -use a::n::o::p; -use a::n::q; -pub use a::r::s; -pub use a::t; - -use foo::e; -#[cfg(test)] -use foo::{a::b, c::d}; - -use bar::a::b; -use bar::c::d; -use bar::e::f; +use a::b; +use a::c; +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::q::{self};