Skip to content

Commit b27348b

Browse files
committed
reorder: use versionsort for use statements
1 parent 4f3b166 commit b27348b

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

rustfmt-core/rustfmt-lib/src/imports.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::config::{Edition, IndentStyle};
1111
use crate::lists::{
1212
definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator,
1313
};
14+
use crate::reorder::{compare_as_versions, compare_opt_ident_as_versions};
1415
use crate::rewrite::{Rewrite, RewriteContext};
1516
use crate::shape::Shape;
1617
use crate::source_map::SpanUtils;
@@ -677,18 +678,8 @@ impl Ord for UseSegment {
677678
if !is_upper_snake_case(ia) && is_upper_snake_case(ib) {
678679
return Ordering::Less;
679680
}
680-
let ident_ord = ia.cmp(ib);
681-
if ident_ord != Ordering::Equal {
682-
return ident_ord;
683-
}
684-
match (aa, ab) {
685-
(None, Some(_)) => Ordering::Less,
686-
(Some(_), None) => Ordering::Greater,
687-
(Some(aas), Some(abs)) => aas
688-
.trim_start_matches("r#")
689-
.cmp(abs.trim_start_matches("r#")),
690-
(None, None) => Ordering::Equal,
691-
}
681+
682+
compare_as_versions(&ia, &ib).then_with(|| compare_opt_ident_as_versions(&aa, &ab))
692683
}
693684
(&List(ref a), &List(ref b)) => {
694685
for (a, b) in a.iter().zip(b.iter()) {

rustfmt-core/rustfmt-lib/src/reorder.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,26 @@ pub(crate) fn compare_as_versions(left: &str, right: &str) -> Ordering {
117117
}
118118
}
119119

120+
/// Compare identifiers, trimming `r#` if present, according to version sort
121+
pub(crate) fn compare_ident_as_versions(left: &str, right: &str) -> Ordering {
122+
compare_as_versions(
123+
left.trim_start_matches("r#"),
124+
right.trim_start_matches("r#"),
125+
)
126+
}
127+
128+
pub(crate) fn compare_opt_ident_as_versions<S>(left: &Option<S>, right: &Option<S>) -> Ordering
129+
where
130+
S: AsRef<str>,
131+
{
132+
match (left, right) {
133+
(None, None) => Ordering::Equal,
134+
(None, Some(_)) => Ordering::Less,
135+
(Some(_), None) => Ordering::Greater,
136+
(Some(left), Some(right)) => compare_ident_as_versions(left.as_ref(), right.as_ref()),
137+
}
138+
}
139+
120140
/// Choose the ordering between the given two items.
121141
fn compare_items(a: &ast::Item, b: &ast::Item) -> Ordering {
122142
match (&a.kind, &b.kind) {

rustfmt-core/rustfmt-lib/tests/source/imports-reorder.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
use path::{C,/*A*/ A, B /* B */, self /* self */};
44

55
use {ab, ac, aa, Z, b};
6+
7+
// The sort order shall follow versionsort
8+
use {u8, u128, u64, u16, u32};
9+
use {v1, v0200, v0030, v0002, v02000, v02001};

rustfmt-core/rustfmt-lib/tests/target/imports-reorder.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33
use path::{self /* self */, /* A */ A, B /* B */, C};
44

55
use {aa, ab, ac, b, Z};
6+
7+
// The sort order shall follow versionsort
8+
use {u8, u16, u32, u64, u128};
9+
use {v0002, v0030, v0200, v02000, v02001, v1};

0 commit comments

Comments
 (0)