Skip to content

Commit

Permalink
Do not remove comment from an import with a single item (#3999)
Browse files Browse the repository at this point in the history
  • Loading branch information
topecongiro committed Jan 7, 2020
1 parent 0c1b396 commit b76fff3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
13 changes: 5 additions & 8 deletions rustfmt-core/rustfmt-lib/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,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 @@ -538,11 +538,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 {
match list[0].path[0] {
UseSegment::Slf(..) => return vec![self],
Expand All @@ -552,9 +549,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 rustfmt-core/rustfmt-lib/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 syntax::ast::{
ItemForeignMod,
Expand Down
3 changes: 3 additions & 0 deletions rustfmt-core/rustfmt-lib/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 syntax::ast::{
ItemDefaultImpl, ItemForeignMod, ItemImpl, ItemMac, ItemMod, ItemStatic,
Expand Down

0 comments on commit b76fff3

Please sign in to comment.