Skip to content

Commit

Permalink
Rewrite paths ending in ::self to ::{self}.
Browse files Browse the repository at this point in the history
  • Loading branch information
msmorgan committed Jan 16, 2021
1 parent 47fc453 commit 93b4f7e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
23 changes: 17 additions & 6 deletions src/formatting/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ pub(crate) fn merge_use_trees(use_trees: Vec<UseTree>, 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
}

Expand Down Expand Up @@ -550,11 +560,6 @@ impl UseTree {
fn flatten(self) -> Vec<UseTree> {
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 {
Expand Down Expand Up @@ -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::*"
]
)
}

Expand Down
5 changes: 0 additions & 5 deletions src/formatting/reorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
2 changes: 1 addition & 1 deletion tests/target/imports_granularity_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

0 comments on commit 93b4f7e

Please sign in to comment.