Skip to content

Commit 7d121b3

Browse files
committed
Fix list use formatting
1 parent 5f48fe9 commit 7d121b3

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
@@ -628,8 +628,21 @@ impl UseTree {
628628
}
629629

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

640653
self.path.push(last);
641-
self
654+
655+
if should_renormalize {
656+
self.normalize()
657+
} else {
658+
self
659+
}
642660
}
643661

644662
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)