Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not remove comment from an import with a single item #3999

Merged
merged 1 commit into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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