From 4c588b69e3694e5b0181db883527f160457b7463 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Campinas?= Date: Mon, 31 Jan 2022 23:45:30 +0100 Subject: [PATCH 1/2] Fix import_granularity option when the use tree has an alias --- src/imports.rs | 6 ++++-- tests/source/5131.rs | 33 +++++++++++++++++++++++++++++++++ tests/target/5131.rs | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 tests/source/5131.rs create mode 100644 tests/target/5131.rs diff --git a/src/imports.rs b/src/imports.rs index 40e0d06f99d..c60bec6d4a2 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -238,7 +238,8 @@ impl fmt::Display for UseSegment { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { UseSegment::Glob => write!(f, "*"), - UseSegment::Ident(ref s, _) => write!(f, "{}", s), + UseSegment::Ident(ref s, Some(ref alias)) => write!(f, "{} as {}", s, alias), + UseSegment::Ident(ref s, None) => write!(f, "{}", s), UseSegment::Slf(..) => write!(f, "self"), UseSegment::Super(..) => write!(f, "super"), UseSegment::Crate(..) => write!(f, "crate"), @@ -622,7 +623,8 @@ impl UseTree { fn merge(&mut self, other: &UseTree, merge_by: SharedPrefix) { let mut prefix = 0; for (a, b) in self.path.iter().zip(other.path.iter()) { - if a.equal_except_alias(b) { + // only discard the alias at the root of the tree + if (prefix == 0 && a.equal_except_alias(b)) || a == b { prefix += 1; } else { break; diff --git a/tests/source/5131.rs b/tests/source/5131.rs new file mode 100644 index 00000000000..3e9139177c5 --- /dev/null +++ b/tests/source/5131.rs @@ -0,0 +1,33 @@ +// rustfmt-imports_granularity: Module + +#![allow(dead_code)] + +mod a { + pub mod b { + pub struct Data { + pub a: i32, + } + } + + use crate::a::b::Data; + use crate::a::b::Data as Data2; + + pub fn data(a: i32) -> Data { + Data { a } + } + + pub fn data2(a: i32) -> Data2 { + Data2 { a } + } + + #[cfg(test)] + mod tests { + use super::*; + + #[test] + pub fn test() { + data(1); + data2(1); + } + } +} diff --git a/tests/target/5131.rs b/tests/target/5131.rs new file mode 100644 index 00000000000..763024d6fa4 --- /dev/null +++ b/tests/target/5131.rs @@ -0,0 +1,32 @@ +// rustfmt-imports_granularity: Module + +#![allow(dead_code)] + +mod a { + pub mod b { + pub struct Data { + pub a: i32, + } + } + + use crate::a::b::{Data, Data as Data2}; + + pub fn data(a: i32) -> Data { + Data { a } + } + + pub fn data2(a: i32) -> Data2 { + Data2 { a } + } + + #[cfg(test)] + mod tests { + use super::*; + + #[test] + pub fn test() { + data(1); + data2(1); + } + } +} From 3c3f37a011d5a94912d1da739cc984a0d4db7ab7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Campinas?= Date: Wed, 2 Feb 2022 15:43:31 +0100 Subject: [PATCH 2/2] Add tests for the One and Crate variants --- tests/source/5131_crate.rs | 14 ++++++++++++++ tests/source/{5131.rs => 5131_module.rs} | 0 tests/source/5131_one.rs | 15 +++++++++++++++ tests/target/5131_crate.rs | 9 +++++++++ tests/target/{5131.rs => 5131_module.rs} | 0 tests/target/5131_one.rs | 12 ++++++++++++ 6 files changed, 50 insertions(+) create mode 100644 tests/source/5131_crate.rs rename tests/source/{5131.rs => 5131_module.rs} (100%) create mode 100644 tests/source/5131_one.rs create mode 100644 tests/target/5131_crate.rs rename tests/target/{5131.rs => 5131_module.rs} (100%) create mode 100644 tests/target/5131_one.rs diff --git a/tests/source/5131_crate.rs b/tests/source/5131_crate.rs new file mode 100644 index 00000000000..96a31659022 --- /dev/null +++ b/tests/source/5131_crate.rs @@ -0,0 +1,14 @@ +// rustfmt-imports_granularity: Crate + +use foo::a; +use foo::a; +use foo::b; +use foo::b as b2; +use foo::b::f; +use foo::b::g; +use foo::b::g as g2; +use foo::c; +use foo::d::e; +use qux::h; +use qux::h as h2; +use qux::i; diff --git a/tests/source/5131.rs b/tests/source/5131_module.rs similarity index 100% rename from tests/source/5131.rs rename to tests/source/5131_module.rs diff --git a/tests/source/5131_one.rs b/tests/source/5131_one.rs new file mode 100644 index 00000000000..61ddf13410d --- /dev/null +++ b/tests/source/5131_one.rs @@ -0,0 +1,15 @@ +// rustfmt-imports_granularity: One + +pub use foo::x; +pub use foo::x as x2; +pub use foo::y; +use bar::a; +use bar::b; +use bar::b::f; +use bar::b::f as f2; +use bar::b::g; +use bar::c; +use bar::d::e; +use bar::d::e as e2; +use qux::h; +use qux::i; diff --git a/tests/target/5131_crate.rs b/tests/target/5131_crate.rs new file mode 100644 index 00000000000..557d6670355 --- /dev/null +++ b/tests/target/5131_crate.rs @@ -0,0 +1,9 @@ +// rustfmt-imports_granularity: Crate + +use foo::{ + a, b, b as b2, + b::{f, g, g as g2}, + c, + d::e, +}; +use qux::{h, h as h2, i}; diff --git a/tests/target/5131.rs b/tests/target/5131_module.rs similarity index 100% rename from tests/target/5131.rs rename to tests/target/5131_module.rs diff --git a/tests/target/5131_one.rs b/tests/target/5131_one.rs new file mode 100644 index 00000000000..a086dae5a42 --- /dev/null +++ b/tests/target/5131_one.rs @@ -0,0 +1,12 @@ +// rustfmt-imports_granularity: One + +pub use foo::{x, x as x2, y}; +use { + bar::{ + a, + b::{self, f, g}, + c, + d::{e, e as e2}, + }, + qux::{h, i}, +};