diff --git a/src/imports.rs b/src/imports.rs index 8d41c881589..914c5471121 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -589,7 +589,7 @@ impl UseTree { // Normalise foo::{bar} -> foo::bar if let UseSegmentKind::List(ref list) = last.kind { - 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; } } @@ -683,9 +683,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(import_granularity) { + for flattened in &mut nested_use_tree.clone().flatten(import_granularity) { 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, diff --git a/tests/source/imports/imports.rs b/tests/source/imports/imports.rs index 4dfc6ed94e3..1df83f9a646 100644 --- a/tests/source/imports/imports.rs +++ b/tests/source/imports/imports.rs @@ -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, diff --git a/tests/source/issue-3984/imports_granularity_crate.rs b/tests/source/issue-3984/imports_granularity_crate.rs new file mode 100644 index 00000000000..e45cd5cbe0a --- /dev/null +++ b/tests/source/issue-3984/imports_granularity_crate.rs @@ -0,0 +1,15 @@ +// rustfmt-imports_granularity: Crate + +// See https://github.com/rust-lang/rustfmt/issues/3984 +use a::{item /* comment */}; +use b::{ + a, + // comment + item, +}; +use c::item /* comment */; +use d::item; // really long comment (with `use` exactly 100 characters) ____________________________ + +use std::e::{/* it's a comment! */ bar /* and another */}; +use std::f::{/* it's a comment! */ bar}; +use std::g::{bar /* and another */}; diff --git a/tests/source/issue-3984/imports_granularity_item.rs b/tests/source/issue-3984/imports_granularity_item.rs new file mode 100644 index 00000000000..4d950d5de4f --- /dev/null +++ b/tests/source/issue-3984/imports_granularity_item.rs @@ -0,0 +1,15 @@ +// rustfmt-imports_granularity: Item + +// See https://github.com/rust-lang/rustfmt/issues/3984 +use a::{item /* comment */}; +use b::{ + a, + // comment + item, +}; +use c::item /* comment */; +use d::item; // really long comment (with `use` exactly 100 characters) ____________________________ + +use std::e::{/* it's a comment! */ bar /* and another */}; +use std::f::{/* it's a comment! */ bar}; +use std::g::{bar /* and another */}; diff --git a/tests/source/issue-3984/imports_granularity_module.rs b/tests/source/issue-3984/imports_granularity_module.rs new file mode 100644 index 00000000000..0ec9f6b595b --- /dev/null +++ b/tests/source/issue-3984/imports_granularity_module.rs @@ -0,0 +1,15 @@ +// rustfmt-imports_granularity: Module + +// See https://github.com/rust-lang/rustfmt/issues/3984 +use a::{item /* comment */}; +use b::{ + a, + // comment + item, +}; +use c::item /* comment */; +use d::item; // really long comment (with `use` exactly 100 characters) ____________________________ + +use std::e::{/* it's a comment! */ bar /* and another */}; +use std::f::{/* it's a comment! */ bar}; +use std::g::{bar /* and another */}; diff --git a/tests/source/issue-3984/imports_granularity_one.rs b/tests/source/issue-3984/imports_granularity_one.rs new file mode 100644 index 00000000000..87c2997f886 --- /dev/null +++ b/tests/source/issue-3984/imports_granularity_one.rs @@ -0,0 +1,15 @@ +// rustfmt-imports_granularity: One + +// See https://github.com/rust-lang/rustfmt/issues/3984 +use a::{item /* comment */}; +use b::{ + a, + // comment + item, +}; +use c::item /* comment */; +use d::item; // really long comment (with `use` exactly 100 characters) ____________________________ + +use std::e::{/* it's a comment! */ bar /* and another */}; +use std::f::{/* it's a comment! */ bar}; +use std::g::{bar /* and another */}; diff --git a/tests/source/issue-3984/imports_granularity_preserve.rs b/tests/source/issue-3984/imports_granularity_preserve.rs new file mode 100644 index 00000000000..4e51d186a94 --- /dev/null +++ b/tests/source/issue-3984/imports_granularity_preserve.rs @@ -0,0 +1,15 @@ +// rustfmt-imports_granularity: Preserve + +// See https://github.com/rust-lang/rustfmt/issues/3984 +use a::{item /* comment */}; +use b::{ + a, + // comment + item, +}; +use c::item /* comment */; +use d::item; // really long comment (with `use` exactly 100 characters) ____________________________ + +use std::e::{/* it's a comment! */ bar /* and another */}; +use std::f::{/* it's a comment! */ bar}; +use std::g::{bar /* and another */}; diff --git a/tests/target/imports/imports.rs b/tests/target/imports/imports.rs index 87584d89f66..12a958a78ad 100644 --- a/tests/target/imports/imports.rs +++ b/tests/target/imports/imports.rs @@ -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, diff --git a/tests/target/issue-3984/imports_granularity_crate.rs b/tests/target/issue-3984/imports_granularity_crate.rs new file mode 100644 index 00000000000..0dd087e322f --- /dev/null +++ b/tests/target/issue-3984/imports_granularity_crate.rs @@ -0,0 +1,15 @@ +// rustfmt-imports_granularity: Crate + +// See https://github.com/rust-lang/rustfmt/issues/3984 +use a::{item /* comment */}; +use b::{ + a, + // comment + item, +}; +use c::item; /* comment */ +use d::item; // really long comment (with `use` exactly 100 characters) ____________________________ + +use std::e::{/* it's a comment! */ bar /* and another */}; +use std::f::{/* it's a comment! */ bar}; +use std::g::{bar /* and another */}; diff --git a/tests/target/issue-3984/imports_granularity_item.rs b/tests/target/issue-3984/imports_granularity_item.rs new file mode 100644 index 00000000000..a5f0f7dac86 --- /dev/null +++ b/tests/target/issue-3984/imports_granularity_item.rs @@ -0,0 +1,15 @@ +// rustfmt-imports_granularity: Item + +// See https://github.com/rust-lang/rustfmt/issues/3984 +use a::{item /* comment */}; +use b::{ + a, + // comment + item, +}; +use c::item; /* comment */ +use d::item; // really long comment (with `use` exactly 100 characters) ____________________________ + +use std::e::{/* it's a comment! */ bar /* and another */}; +use std::f::{/* it's a comment! */ bar}; +use std::g::{bar /* and another */}; diff --git a/tests/target/issue-3984/imports_granularity_module.rs b/tests/target/issue-3984/imports_granularity_module.rs new file mode 100644 index 00000000000..2ce31a0ee94 --- /dev/null +++ b/tests/target/issue-3984/imports_granularity_module.rs @@ -0,0 +1,15 @@ +// rustfmt-imports_granularity: Module + +// See https://github.com/rust-lang/rustfmt/issues/3984 +use a::{item /* comment */}; +use b::{ + a, + // comment + item, +}; +use c::item; /* comment */ +use d::item; // really long comment (with `use` exactly 100 characters) ____________________________ + +use std::e::{/* it's a comment! */ bar /* and another */}; +use std::f::{/* it's a comment! */ bar}; +use std::g::{bar /* and another */}; diff --git a/tests/target/issue-3984/imports_granularity_one.rs b/tests/target/issue-3984/imports_granularity_one.rs new file mode 100644 index 00000000000..ee7af1f045e --- /dev/null +++ b/tests/target/issue-3984/imports_granularity_one.rs @@ -0,0 +1,15 @@ +// rustfmt-imports_granularity: One + +// See https://github.com/rust-lang/rustfmt/issues/3984 +use a::{item /* comment */}; +use b::{ + a, + // comment + item, +}; +use c::item; /* comment */ +use d::item; // really long comment (with `use` exactly 100 characters) ____________________________ + +use std::e::{/* it's a comment! */ bar /* and another */}; +use std::f::{/* it's a comment! */ bar}; +use std::g::{bar /* and another */}; diff --git a/tests/target/issue-3984/imports_granularity_preserve.rs b/tests/target/issue-3984/imports_granularity_preserve.rs new file mode 100644 index 00000000000..639c03f4980 --- /dev/null +++ b/tests/target/issue-3984/imports_granularity_preserve.rs @@ -0,0 +1,15 @@ +// rustfmt-imports_granularity: Preserve + +// See https://github.com/rust-lang/rustfmt/issues/3984 +use a::{item /* comment */}; +use b::{ + a, + // comment + item, +}; +use c::item; /* comment */ +use d::item; // really long comment (with `use` exactly 100 characters) ____________________________ + +use std::e::{/* it's a comment! */ bar /* and another */}; +use std::f::{/* it's a comment! */ bar}; +use std::g::{bar /* and another */}; diff --git a/tests/target/issue-4708/imports_granularity_crate.rs b/tests/target/issue-4708/imports_granularity_crate.rs new file mode 100644 index 00000000000..9f77fde6602 --- /dev/null +++ b/tests/target/issue-4708/imports_granularity_crate.rs @@ -0,0 +1,13 @@ +// rustfmt-imports_granularity: Crate + +// See https://github.com/rust-lang/rustfmt/issues/4708 +pub use serenity::model::{ + // channel::{Message, Reaction, ReactionType}, + // gateway::Ready, + // guild::{Member, Role}, + // id::{ChannelId, GuildId, MessageId, RoleId, UserId}, + // permissions::Permissions, + prelude::*, + // user::User, +}; +use {std::env::args /* this comment gets removed */}; diff --git a/tests/target/issue-4708/imports_granularity_item.rs b/tests/target/issue-4708/imports_granularity_item.rs new file mode 100644 index 00000000000..955f1aae213 --- /dev/null +++ b/tests/target/issue-4708/imports_granularity_item.rs @@ -0,0 +1,13 @@ +// rustfmt-imports_granularity: Item + +// See https://github.com/rust-lang/rustfmt/issues/4708 +pub use serenity::model::{ + // channel::{Message, Reaction, ReactionType}, + // gateway::Ready, + // guild::{Member, Role}, + // id::{ChannelId, GuildId, MessageId, RoleId, UserId}, + // permissions::Permissions, + prelude::*, + // user::User, +}; +use {std::env::args /* this comment gets removed */}; diff --git a/tests/target/issue-4708/imports_granularity_module.rs b/tests/target/issue-4708/imports_granularity_module.rs new file mode 100644 index 00000000000..6cb17470c60 --- /dev/null +++ b/tests/target/issue-4708/imports_granularity_module.rs @@ -0,0 +1,13 @@ +// rustfmt-imports_granularity: Module + +// See https://github.com/rust-lang/rustfmt/issues/4708 +pub use serenity::model::{ + // channel::{Message, Reaction, ReactionType}, + // gateway::Ready, + // guild::{Member, Role}, + // id::{ChannelId, GuildId, MessageId, RoleId, UserId}, + // permissions::Permissions, + prelude::*, + // user::User, +}; +use {std::env::args /* this comment gets removed */}; diff --git a/tests/target/issue-4708/imports_granularity_one.rs b/tests/target/issue-4708/imports_granularity_one.rs new file mode 100644 index 00000000000..36817e97e34 --- /dev/null +++ b/tests/target/issue-4708/imports_granularity_one.rs @@ -0,0 +1,13 @@ +// rustfmt-imports_granularity: One + +// See https://github.com/rust-lang/rustfmt/issues/4708 +pub use serenity::model::{ + // channel::{Message, Reaction, ReactionType}, + // gateway::Ready, + // guild::{Member, Role}, + // id::{ChannelId, GuildId, MessageId, RoleId, UserId}, + // permissions::Permissions, + prelude::*, + // user::User, +}; +use {std::env::args /* this comment gets removed */}; diff --git a/tests/target/issue-4708/imports_granularity_preserve.rs b/tests/target/issue-4708/imports_granularity_preserve.rs new file mode 100644 index 00000000000..dcb1a1864b8 --- /dev/null +++ b/tests/target/issue-4708/imports_granularity_preserve.rs @@ -0,0 +1,13 @@ +// rustfmt-imports_granularity: Preserve + +// See https://github.com/rust-lang/rustfmt/issues/4708 +pub use serenity::model::{ + // channel::{Message, Reaction, ReactionType}, + // gateway::Ready, + // guild::{Member, Role}, + // id::{ChannelId, GuildId, MessageId, RoleId, UserId}, + // permissions::Permissions, + prelude::*, + // user::User, +}; +use {std::env::args /* this comment gets removed */};