Skip to content

Commit b76fff3

Browse files
authored
Do not remove comment from an import with a single item (#3999)
1 parent 0c1b396 commit b76fff3

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

rustfmt-core/rustfmt-lib/src/imports.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ impl UseTree {
471471

472472
// Normalise foo::{bar} -> foo::bar
473473
if let UseSegment::List(ref list) = last {
474-
if list.len() == 1 && list[0].to_string() != "self" {
474+
if list.len() == 1 && !list[0].has_comment() && list[0].to_string() != "self" {
475475
normalize_sole_list = true;
476476
}
477477
}
@@ -538,11 +538,8 @@ impl UseTree {
538538
}
539539

540540
fn flatten(self) -> Vec<UseTree> {
541-
if self.path.is_empty() {
542-
return vec![self];
543-
}
544-
match self.path.clone().last().unwrap() {
545-
UseSegment::List(list) => {
541+
match self.path.clone().last() {
542+
Some(UseSegment::List(list)) => {
546543
if list.len() == 1 && list[0].path.len() == 1 {
547544
match list[0].path[0] {
548545
UseSegment::Slf(..) => return vec![self],
@@ -552,9 +549,9 @@ impl UseTree {
552549
let prefix = &self.path[..self.path.len() - 1];
553550
let mut result = vec![];
554551
for nested_use_tree in list {
555-
for flattend in &mut nested_use_tree.clone().flatten() {
552+
for flattened in &mut nested_use_tree.clone().flatten() {
556553
let mut new_path = prefix.to_vec();
557-
new_path.append(&mut flattend.path);
554+
new_path.append(&mut flattened.path);
558555
result.push(UseTree {
559556
path: new_path,
560557
span: self.span,

rustfmt-core/rustfmt-lib/tests/source/imports.rs

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ use self;
2727
use std::io::{self};
2828
use std::io::self;
2929

30+
use a::{/* comment */ item};
31+
use a::{item /* comment */};
32+
3033
mod Foo {
3134
pub use syntax::ast::{
3235
ItemForeignMod,

rustfmt-core/rustfmt-lib/tests/target/imports.rs

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ use {Bar /* comment */, /* Pre-comment! */ Foo};
3030
use std::io;
3131
use std::io::{self};
3232

33+
use a::{/* comment */ item};
34+
use a::{item /* comment */};
35+
3336
mod Foo {
3437
pub use syntax::ast::{
3538
ItemDefaultImpl, ItemForeignMod, ItemImpl, ItemMac, ItemMod, ItemStatic,

0 commit comments

Comments
 (0)