From ec54d6508bdad8d414507d81faff0f7d00aa1c06 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Wed, 8 Jan 2020 00:07:18 +0900 Subject: [PATCH 1/3] Backport 3999 Do not remove comment from an import with a single item --- src/imports.rs | 6 +++--- tests/source/imports/imports.rs | 3 +++ tests/target/imports/imports.rs | 3 +++ 3 files changed, 9 insertions(+), 3 deletions(-) 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/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, From 69f2d1d3516bda86d29970a90f2cee639d78c6a6 Mon Sep 17 00:00:00 2001 From: Yacin Tmimi Date: Thu, 21 Jul 2022 00:52:25 -0400 Subject: [PATCH 2/3] add test for 3984 This is fixed by backporting 3999 --- .../issue-3984/imports_granularity_crate.rs | 15 +++++++++++++++ .../source/issue-3984/imports_granularity_item.rs | 15 +++++++++++++++ .../issue-3984/imports_granularity_module.rs | 15 +++++++++++++++ .../source/issue-3984/imports_granularity_one.rs | 15 +++++++++++++++ .../issue-3984/imports_granularity_preserve.rs | 15 +++++++++++++++ .../issue-3984/imports_granularity_crate.rs | 15 +++++++++++++++ .../target/issue-3984/imports_granularity_item.rs | 15 +++++++++++++++ .../issue-3984/imports_granularity_module.rs | 15 +++++++++++++++ .../target/issue-3984/imports_granularity_one.rs | 15 +++++++++++++++ .../issue-3984/imports_granularity_preserve.rs | 15 +++++++++++++++ 10 files changed, 150 insertions(+) create mode 100644 tests/source/issue-3984/imports_granularity_crate.rs create mode 100644 tests/source/issue-3984/imports_granularity_item.rs create mode 100644 tests/source/issue-3984/imports_granularity_module.rs create mode 100644 tests/source/issue-3984/imports_granularity_one.rs create mode 100644 tests/source/issue-3984/imports_granularity_preserve.rs create mode 100644 tests/target/issue-3984/imports_granularity_crate.rs create mode 100644 tests/target/issue-3984/imports_granularity_item.rs create mode 100644 tests/target/issue-3984/imports_granularity_module.rs create mode 100644 tests/target/issue-3984/imports_granularity_one.rs create mode 100644 tests/target/issue-3984/imports_granularity_preserve.rs 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/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 */}; From cd8dc7c519f01864f2b5553812e7848f26a88962 Mon Sep 17 00:00:00 2001 From: Yacin Tmimi Date: Thu, 21 Jul 2022 00:53:06 -0400 Subject: [PATCH 3/3] add test for 4708 This is fixed by backporting 3999 --- .../target/issue-4708/imports_granularity_crate.rs | 13 +++++++++++++ tests/target/issue-4708/imports_granularity_item.rs | 13 +++++++++++++ .../target/issue-4708/imports_granularity_module.rs | 13 +++++++++++++ tests/target/issue-4708/imports_granularity_one.rs | 13 +++++++++++++ .../issue-4708/imports_granularity_preserve.rs | 13 +++++++++++++ 5 files changed, 65 insertions(+) create mode 100644 tests/target/issue-4708/imports_granularity_crate.rs create mode 100644 tests/target/issue-4708/imports_granularity_item.rs create mode 100644 tests/target/issue-4708/imports_granularity_module.rs create mode 100644 tests/target/issue-4708/imports_granularity_one.rs create mode 100644 tests/target/issue-4708/imports_granularity_preserve.rs 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 */};