Skip to content

Commit b2c7a52

Browse files
scampicalebcartwright
authored andcommitted
Fix import_granularity option when the use tree has an alias
1 parent 606894e commit b2c7a52

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

src/imports.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ impl fmt::Display for UseSegment {
238238
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
239239
match *self {
240240
UseSegment::Glob => write!(f, "*"),
241-
UseSegment::Ident(ref s, _) => write!(f, "{}", s),
241+
UseSegment::Ident(ref s, Some(ref alias)) => write!(f, "{} as {}", s, alias),
242+
UseSegment::Ident(ref s, None) => write!(f, "{}", s),
242243
UseSegment::Slf(..) => write!(f, "self"),
243244
UseSegment::Super(..) => write!(f, "super"),
244245
UseSegment::Crate(..) => write!(f, "crate"),
@@ -622,7 +623,8 @@ impl UseTree {
622623
fn merge(&mut self, other: &UseTree, merge_by: SharedPrefix) {
623624
let mut prefix = 0;
624625
for (a, b) in self.path.iter().zip(other.path.iter()) {
625-
if a.equal_except_alias(b) {
626+
// only discard the alias at the root of the tree
627+
if (prefix == 0 && a.equal_except_alias(b)) || a == b {
626628
prefix += 1;
627629
} else {
628630
break;

tests/source/5131.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// rustfmt-imports_granularity: Module
2+
3+
#![allow(dead_code)]
4+
5+
mod a {
6+
pub mod b {
7+
pub struct Data {
8+
pub a: i32,
9+
}
10+
}
11+
12+
use crate::a::b::Data;
13+
use crate::a::b::Data as Data2;
14+
15+
pub fn data(a: i32) -> Data {
16+
Data { a }
17+
}
18+
19+
pub fn data2(a: i32) -> Data2 {
20+
Data2 { a }
21+
}
22+
23+
#[cfg(test)]
24+
mod tests {
25+
use super::*;
26+
27+
#[test]
28+
pub fn test() {
29+
data(1);
30+
data2(1);
31+
}
32+
}
33+
}

tests/target/5131.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// rustfmt-imports_granularity: Module
2+
3+
#![allow(dead_code)]
4+
5+
mod a {
6+
pub mod b {
7+
pub struct Data {
8+
pub a: i32,
9+
}
10+
}
11+
12+
use crate::a::b::{Data, Data as Data2};
13+
14+
pub fn data(a: i32) -> Data {
15+
Data { a }
16+
}
17+
18+
pub fn data2(a: i32) -> Data2 {
19+
Data2 { a }
20+
}
21+
22+
#[cfg(test)]
23+
mod tests {
24+
use super::*;
25+
26+
#[test]
27+
pub fn test() {
28+
data(1);
29+
data2(1);
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)