Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix list use formatting #6283

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
wafarm marked this conversation as resolved.
Show resolved Hide resolved
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/three.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use core::{
self,

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/three.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use core::{self};
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};
Loading