Skip to content

Commit

Permalink
Backport 3999
Browse files Browse the repository at this point in the history
Do not remove comment from an import with a single item
  • Loading branch information
topecongiro authored and ytmimi committed Apr 1, 2022
1 parent 5ff7b63 commit 83f93ba
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ impl UseTree {

// Normalise foo::{bar} -> foo::bar
if let UseSegment::List(ref list) = last {
if list.len() == 1 && list[0].to_string() != "self" {
if list.len() == 1 && !list[0].has_comment() && list[0].to_string() != "self" {
normalize_sole_list = true;
}
}
Expand Down Expand Up @@ -582,11 +582,8 @@ impl UseTree {
}

fn flatten(self) -> Vec<UseTree> {
if self.path.is_empty() {
return vec![self];
}
match self.path.clone().last().unwrap() {
UseSegment::List(list) => {
match self.path.clone().last() {
Some(UseSegment::List(list)) => {
if list.len() == 1 && list[0].path.len() == 1 {
if let UseSegment::Slf(..) = list[0].path[0] {
return vec![self];
Expand All @@ -595,9 +592,9 @@ impl UseTree {
let prefix = &self.path[..self.path.len() - 1];
let mut result = vec![];
for nested_use_tree in list {
for flattend in &mut nested_use_tree.clone().flatten() {
for flattened in &mut nested_use_tree.clone().flatten() {
let mut new_path = prefix.to_vec();
new_path.append(&mut flattend.path);
new_path.append(&mut flattened.path);
result.push(UseTree {
path: new_path,
span: self.span,
Expand Down
3 changes: 3 additions & 0 deletions tests/source/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ use self;
use std::io::{self};
use std::io::self;

use a::{/* comment */ item};
use a::{item /* comment */};

mod Foo {
pub use rustc_ast::ast::{
ItemForeignMod,
Expand Down
3 changes: 3 additions & 0 deletions tests/target/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ use {Bar /* comment */, /* Pre-comment! */ Foo};
use std::io;
use std::io::{self};

use a::{/* comment */ item};
use a::{item /* comment */};

mod Foo {
pub use rustc_ast::ast::{
ItemDefaultImpl, ItemForeignMod, ItemImpl, ItemMac, ItemMod, ItemStatic,
Expand Down

0 comments on commit 83f93ba

Please sign in to comment.