Fix import_granularity option when the use tree has an alias

This commit is contained in:
Stéphane Campinas
2022-01-31 23:45:30 +01:00
committed by Caleb Cartwright
parent 606894eb0b
commit b2c7a52ea8
3 changed files with 69 additions and 2 deletions
+4 -2
View File
@@ -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 @@ fn flatten(self) -> Vec<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;
+33
View File
@@ -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);
}
}
}
+32
View File
@@ -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);
}
}
}