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 Aug 15, 2024
1 parent caaa612 commit a733d07
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 @@ -627,8 +627,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 @@ -637,7 +650,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 a733d07

Please sign in to comment.