Skip to content

Commit b70465c

Browse files
committed
Fix list use formatting
1 parent fbe0424 commit b70465c

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

src/imports.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,8 +627,21 @@ impl UseTree {
627627
}
628628

629629
// Recursively normalize elements of a list use (including sorting the list).
630+
let mut should_renormalize = false;
630631
if let UseSegmentKind::List(list) = last.kind {
631-
let mut list = list.into_iter().map(UseTree::normalize).collect::<Vec<_>>();
632+
let original_size = list.len();
633+
let mut list = list
634+
.into_iter()
635+
.map(UseTree::normalize)
636+
// Filter away empty elements
637+
.filter(|elem| !elem.path.is_empty())
638+
.collect::<Vec<_>>();
639+
640+
// We need to normalize again if list length is reduced to
641+
// 0 (may remove) or 1 (may extract the element)
642+
should_renormalize =
643+
original_size != list.len() && (list.len() == 0 || list.len() == 1);
644+
632645
list.sort();
633646
last = UseSegment {
634647
kind: UseSegmentKind::List(list),
@@ -637,7 +650,12 @@ impl UseTree {
637650
}
638651

639652
self.path.push(last);
640-
self
653+
654+
if should_renormalize {
655+
self.normalize()
656+
} else {
657+
self
658+
}
641659
}
642660

643661
fn has_comment(&self) -> bool {

tests/source/issue-6277/one.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use core::{
2+
slice,
3+
4+
fmt::{},
5+
};

tests/source/issue-6277/two.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use core::{
2+
3+
fmt::{Debug, Display},
4+
slice::{},
5+
};

tests/target/issue-6277/one.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use core::slice;

tests/target/issue-6277/two.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use core::fmt::{Debug, Display};

0 commit comments

Comments
 (0)