Skip to content

Commit 83f93ba

Browse files
topecongiroytmimi
authored andcommitted
Backport 3999
Do not remove comment from an import with a single item
1 parent 5ff7b63 commit 83f93ba

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

src/imports.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ impl UseTree {
509509

510510
// Normalise foo::{bar} -> foo::bar
511511
if let UseSegment::List(ref list) = last {
512-
if list.len() == 1 && list[0].to_string() != "self" {
512+
if list.len() == 1 && !list[0].has_comment() && list[0].to_string() != "self" {
513513
normalize_sole_list = true;
514514
}
515515
}
@@ -582,11 +582,8 @@ impl UseTree {
582582
}
583583

584584
fn flatten(self) -> Vec<UseTree> {
585-
if self.path.is_empty() {
586-
return vec![self];
587-
}
588-
match self.path.clone().last().unwrap() {
589-
UseSegment::List(list) => {
585+
match self.path.clone().last() {
586+
Some(UseSegment::List(list)) => {
590587
if list.len() == 1 && list[0].path.len() == 1 {
591588
if let UseSegment::Slf(..) = list[0].path[0] {
592589
return vec![self];
@@ -595,9 +592,9 @@ impl UseTree {
595592
let prefix = &self.path[..self.path.len() - 1];
596593
let mut result = vec![];
597594
for nested_use_tree in list {
598-
for flattend in &mut nested_use_tree.clone().flatten() {
595+
for flattened in &mut nested_use_tree.clone().flatten() {
599596
let mut new_path = prefix.to_vec();
600-
new_path.append(&mut flattend.path);
597+
new_path.append(&mut flattened.path);
601598
result.push(UseTree {
602599
path: new_path,
603600
span: self.span,

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 rustc_ast::ast::{
3235
ItemForeignMod,

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 rustc_ast::ast::{
3538
ItemDefaultImpl, ItemForeignMod, ItemImpl, ItemMac, ItemMod, ItemStatic,

0 commit comments

Comments
 (0)