Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions tests/source/5131_crate.rs
Original file line number Diff line number Diff line change
@@ -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;
33 changes: 33 additions & 0 deletions tests/source/5131_module.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
15 changes: 15 additions & 0 deletions tests/source/5131_one.rs
Original file line number Diff line number Diff line change
@@ -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;
9 changes: 9 additions & 0 deletions tests/target/5131_crate.rs
Original file line number Diff line number Diff line change
@@ -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};
32 changes: 32 additions & 0 deletions tests/target/5131_module.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
12 changes: 12 additions & 0 deletions tests/target/5131_one.rs
Original file line number Diff line number Diff line change
@@ -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},
};