Skip to content

Commit 356c85b

Browse files
Preserve comments when flattening use statements
Issue rust-lang#4991
1 parent a37d3ab commit 356c85b

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

src/imports.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -610,13 +610,13 @@ impl UseTree {
610610
let prefix = &self.path[..self.path.len() - 1];
611611
let mut result = vec![];
612612
for nested_use_tree in list {
613-
for flattend in &mut nested_use_tree.clone().flatten(import_granularity) {
613+
for mut flattend in nested_use_tree.clone().flatten(import_granularity) {
614614
let mut new_path = prefix.to_vec();
615615
new_path.append(&mut flattend.path);
616616
result.push(UseTree {
617617
path: new_path,
618618
span: self.span,
619-
list_item: None,
619+
list_item: flattend.list_item,
620620
visibility: self.visibility.clone(),
621621
// only retain attributes for `ImportGranularity::Item`
622622
attrs: match import_granularity {
+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
// rustfmt-imports_granularity: Item
22

3-
use a::{b, c, d};
3+
use a::{
4+
b,
5+
c,
6+
// Comment before a::d
7+
d,
8+
e, // Comment after a::e
9+
};
410
use a::{f::g, h::{i, j}};
511
use a::{l::{self, m, n::o, p::*}};
612
use a::q::{self};
13+
// Comment before a::r
14+
use a::r;

tests/target/imports_granularity_item.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
use a::b;
44
use a::c;
5+
// Comment before a::d
56
use a::d;
7+
use a::e; // Comment after a::e
68
use a::f::g;
79
use a::h::i;
810
use a::h::j;
@@ -11,3 +13,5 @@ use a::l::n::o;
1113
use a::l::p::*;
1214
use a::l::{self};
1315
use a::q::{self};
16+
// Comment before a::r
17+
use a::r;

tests/target/imports_granularity_module.rs

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use foo::e;
1717
#[cfg(test)]
1818
use foo::{a::b, c::d};
1919

20+
// comment
2021
use bar::a::b;
22+
// more comment
2123
use bar::c::d;
2224
use bar::e::f;

0 commit comments

Comments
 (0)