From bd3630d469f4eb4f543f99bbad49f376a9eff252 Mon Sep 17 00:00:00 2001 From: Yacin Tmimi Date: Mon, 13 Jun 2022 11:36:20 -0400 Subject: [PATCH] Version gate raw identifier use statement sorting When useing `version=One` rustfmt will treat the leading `r#` as part of the `UseSegment` used for sorting. When using `version=Two` rustfmt will ignore the leading `r#` and only consider the name of the identifier when sorting the `UseSegment`. --- src/imports.rs | 24 ++++++++++++++----- .../version_One.rs} | 2 ++ .../imports_raw_identifiers/version_Two.rs} | 4 +++- .../imports_raw_identifiers/version_One.rs | 5 ++++ .../imports_raw_identifiers/version_Two.rs | 5 ++++ 5 files changed, 33 insertions(+), 7 deletions(-) rename tests/source/{imports_raw_identifiers.rs => imports_raw_identifiers/version_One.rs} (83%) rename tests/{target/imports_raw_identifiers.rs => source/imports_raw_identifiers/version_Two.rs} (83%) create mode 100644 tests/target/imports_raw_identifiers/version_One.rs create mode 100644 tests/target/imports_raw_identifiers/version_Two.rs diff --git a/src/imports.rs b/src/imports.rs index 58c5f31995e..8d41c881589 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -890,14 +890,21 @@ impl Ord for UseSegment { | (Super(ref a), Super(ref b)) | (Crate(ref a), Crate(ref b)) => match (a, b) { (Some(sa), Some(sb)) => { - sa.trim_start_matches("r#").cmp(sb.trim_start_matches("r#")) + if self.version == Version::Two { + sa.trim_start_matches("r#").cmp(sb.trim_start_matches("r#")) + } else { + a.cmp(b) + } } (_, _) => a.cmp(b), }, (Glob, Glob) => Ordering::Equal, (Ident(ref pia, ref aa), Ident(ref pib, ref ab)) => { - let ia = pia.trim_start_matches("r#"); - let ib = pib.trim_start_matches("r#"); + let (ia, ib) = if self.version == Version::Two { + (pia.trim_start_matches("r#"), pib.trim_start_matches("r#")) + } else { + (pia.as_str(), pib.as_str()) + }; // snake_case < CamelCase < UPPER_SNAKE_CASE if ia.starts_with(char::is_uppercase) && ib.starts_with(char::is_lowercase) { return Ordering::Greater; @@ -918,9 +925,14 @@ impl Ord for UseSegment { match (aa, ab) { (None, Some(_)) => Ordering::Less, (Some(_), None) => Ordering::Greater, - (Some(aas), Some(abs)) => aas - .trim_start_matches("r#") - .cmp(abs.trim_start_matches("r#")), + (Some(aas), Some(abs)) => { + if self.version == Version::Two { + aas.trim_start_matches("r#") + .cmp(abs.trim_start_matches("r#")) + } else { + aas.cmp(abs) + } + } (None, None) => Ordering::Equal, } } diff --git a/tests/source/imports_raw_identifiers.rs b/tests/source/imports_raw_identifiers/version_One.rs similarity index 83% rename from tests/source/imports_raw_identifiers.rs rename to tests/source/imports_raw_identifiers/version_One.rs index d5857290b62..bc4b5b13569 100644 --- a/tests/source/imports_raw_identifiers.rs +++ b/tests/source/imports_raw_identifiers/version_One.rs @@ -1,3 +1,5 @@ +// rustfmt-version:One + use websocket::client::ClientBuilder; use websocket::r#async::futures::Stream; use websocket::result::WebSocketError; diff --git a/tests/target/imports_raw_identifiers.rs b/tests/source/imports_raw_identifiers/version_Two.rs similarity index 83% rename from tests/target/imports_raw_identifiers.rs rename to tests/source/imports_raw_identifiers/version_Two.rs index 6ae085c8a5f..88e7fbd01ca 100644 --- a/tests/target/imports_raw_identifiers.rs +++ b/tests/source/imports_raw_identifiers/version_Two.rs @@ -1,3 +1,5 @@ -use websocket::r#async::futures::Stream; +// rustfmt-version:Two + use websocket::client::ClientBuilder; +use websocket::r#async::futures::Stream; use websocket::result::WebSocketError; diff --git a/tests/target/imports_raw_identifiers/version_One.rs b/tests/target/imports_raw_identifiers/version_One.rs new file mode 100644 index 00000000000..bc4b5b13569 --- /dev/null +++ b/tests/target/imports_raw_identifiers/version_One.rs @@ -0,0 +1,5 @@ +// rustfmt-version:One + +use websocket::client::ClientBuilder; +use websocket::r#async::futures::Stream; +use websocket::result::WebSocketError; diff --git a/tests/target/imports_raw_identifiers/version_Two.rs b/tests/target/imports_raw_identifiers/version_Two.rs new file mode 100644 index 00000000000..22bfe93122f --- /dev/null +++ b/tests/target/imports_raw_identifiers/version_Two.rs @@ -0,0 +1,5 @@ +// rustfmt-version:Two + +use websocket::r#async::futures::Stream; +use websocket::client::ClientBuilder; +use websocket::result::WebSocketError;