Skip to content

Commit

Permalink
Fix list use formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
wafarm committed Sep 21, 2024
1 parent 6157568 commit b93201e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,8 +628,21 @@ impl UseTree {
}

// Recursively normalize elements of a list use (including sorting the list).
let mut should_renormalize = false;
if let UseSegmentKind::List(list) = last.kind {
let mut list = list.into_iter().map(UseTree::normalize).collect::<Vec<_>>();
let original_size = list.len();
let mut list = list
.into_iter()
.map(UseTree::normalize)
// Filter away empty elements
.filter(|elem| !elem.path.is_empty())
.collect::<Vec<_>>();

// We need to normalize again if list length is reduced to
// 0 (may remove) or 1 (may extract the element)
should_renormalize =
original_size != list.len() && (list.len() == 0 || list.len() == 1);

list.sort();
last = UseSegment {
kind: UseSegmentKind::List(list),
Expand All @@ -638,7 +651,12 @@ impl UseTree {
}

self.path.push(last);
self

if should_renormalize {
self.normalize()
} else {
self
}
}

fn has_comment(&self) -> bool {
Expand Down
5 changes: 5 additions & 0 deletions tests/source/issue-6277/one.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use core::{
slice,

fmt::{},
};
5 changes: 5 additions & 0 deletions tests/source/issue-6277/two.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use core::{

fmt::{Debug, Display},
slice::{},
};
1 change: 1 addition & 0 deletions tests/target/issue-6277/one.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use core::slice;
1 change: 1 addition & 0 deletions tests/target/issue-6277/two.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use core::fmt::{Debug, Display};

0 comments on commit b93201e

Please sign in to comment.