Skip to content

Sort the first group in StdExternalCrate import grouping by their subset relation #6548

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

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2379,8 +2379,8 @@ Discard existing import groups, and create three groups for:
3. `self`, `super` and `crate` imports.

```rust
use alloc::alloc::Layout;
use core::f32;
use alloc::alloc::Layout;
use std::sync::Arc;

use broker::database::PooledConnection;
Expand Down
26 changes: 25 additions & 1 deletion src/reorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,31 @@ fn rewrite_reorderable_or_regroupable_items(
};

if context.config.reorder_imports() {
regrouped_items.iter_mut().for_each(|items| items.sort())
if context.config.group_imports() == GroupImportsTactic::StdExternalCrate {
// Sort the first group by core -> alloc -> std.
regrouped_items[0].sort_by(|a, b| {
if let (UseSegmentKind::Ident(a_id, _), UseSegmentKind::Ident(b_id, _)) =
(&a.path[0].kind, &b.path[0].kind)
{
match (a_id.as_ref(), b_id.as_ref()) {
("core", "alloc") | ("core", "std") | ("alloc", "std") => {
return Ordering::Less;
}
("alloc", "core") | ("std", "core") | ("std", "alloc") => {
return Ordering::Greater;
}
_ => {}
}
}
a.cmp(b)
});
regrouped_items
.iter_mut()
.skip(1)
.for_each(|items| items.sort());
} else {
regrouped_items.iter_mut().for_each(|items| items.sort());
}
}

// 4 = "use ", 1 = ";"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// rustfmt-group_imports: StdExternalCrate
// rustfmt-imports_granularity: Crate
use alloc::{alloc::Layout, vec::Vec};
use core::f32;
use alloc::{alloc::Layout, vec::Vec};
use std::sync::Arc;

use broker::database::PooledConnection;
Expand Down
2 changes: 1 addition & 1 deletion tests/target/configs/group_imports/StdExternalCrate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// rustfmt-group_imports: StdExternalCrate
use alloc::alloc::Layout;
use core::f32;
use alloc::alloc::Layout;
use std::sync::Arc;

use broker::database::PooledConnection;
Expand Down
Loading