From e10d2d77180a6030d4dfa0d8064357cf2ca0c266 Mon Sep 17 00:00:00 2001 From: David Bar-On Date: Wed, 17 Mar 2021 11:55:51 +0200 Subject: [PATCH] Fix issue with extra semicolon when import comment preceeds semicolon --- src/formatting/lists.rs | 4 +- ...imports_granularity_crate-with-comments.rs | 32 ++++++++++++++++ ...ports_granularity_default-with-comments.rs | 38 +++++++++++++++++++ .../imports_granularity_item-with-comments.rs | 24 ++++++++++++ ...mports_granularity_module-with-comments.rs | 34 +++++++++++++++++ ...imports_granularity_crate-with-comments.rs | 25 ++++++++++++ ...ports_granularity_default-with-comments.rs | 38 +++++++++++++++++++ .../imports_granularity_item-with-comments.rs | 21 ++++++++++ ...mports_granularity_module-with-comments.rs | 27 +++++++++++++ 9 files changed, 241 insertions(+), 2 deletions(-) create mode 100644 tests/source/imports/imports_granularity_crate-with-comments.rs create mode 100644 tests/source/imports/imports_granularity_default-with-comments.rs create mode 100644 tests/source/imports/imports_granularity_item-with-comments.rs create mode 100644 tests/source/imports/imports_granularity_module-with-comments.rs create mode 100644 tests/target/imports/imports_granularity_crate-with-comments.rs create mode 100644 tests/target/imports/imports_granularity_default-with-comments.rs create mode 100644 tests/target/imports/imports_granularity_item-with-comments.rs create mode 100644 tests/target/imports/imports_granularity_module-with-comments.rs diff --git a/src/formatting/lists.rs b/src/formatting/lists.rs index 2678adf1c44..6d9aa529f7c 100644 --- a/src/formatting/lists.rs +++ b/src/formatting/lists.rs @@ -745,11 +745,11 @@ pub(crate) fn extract_post_comment( post_snippet.trim_matches(white_space) } // not comment or over two lines - else if post_snippet.ends_with(',') + else if post_snippet.ends_with(separator) && (!post_snippet.trim().starts_with("//") || post_snippet.trim().contains('\n')) { post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space) - } else if let Some(sep_pos) = post_snippet.find_uncommented(",") { + } else if let Some(sep_pos) = post_snippet.find_uncommented(separator) { _post_snippet_without_sep = [ post_snippet[..sep_pos] .trim_matches(white_space) diff --git a/tests/source/imports/imports_granularity_crate-with-comments.rs b/tests/source/imports/imports_granularity_crate-with-comments.rs new file mode 100644 index 00000000000..3826d8837e6 --- /dev/null +++ b/tests/source/imports/imports_granularity_crate-with-comments.rs @@ -0,0 +1,32 @@ +// rustfmt-imports_granularity: Crate + +// With one comment per item - after the the `;` +use foo1 ; /* 2nd foo1 - comment after ; */ +use crate::foo2::bar ; /* 1st foo1::bar - comment after ; */ +use crate::foo2::bar ; + +// With one comment per item - before the the `;` +use foo3 /* 2nd foo3 - comment before ; */ ; +use crate::foo4::bar /* 1st foo4::bar - comment before ; */ ; +use crate::foo4::bar ; + +// With multiline comments or multi comments - after the `;` +use crate::foo5; /* foo5 - Multiline comment before ; line 1 + * foo5 - Multiline comment before ; line 2 */ +use crate::foo5; +use crate::foo6; // foo6- mixed comments before ; - 1st line comment ; + /* foo6- mixed comments before ; - 2nd block comment */ +use crate::foo6; + +// With multiline comments or multi comments - before the `;` +use crate::foo7 /* foo7 - Multiline comment before ; line 1 + * foo7 - Multiline comment before ; line 2 */ ; +use crate::foo7; +use crate::foo8 // foo8- mixed comments before ; - 1st line comment ; + /* foo8- mixed comments before ; - 2nd block comment */ ; +use crate::foo8; + +// With one comment for a module +use crate::foo21::{self} ; /* external comment for foo21 {self} */ +use crate::foo21::{foo} ; +use crate::foo21::{bar} ; diff --git a/tests/source/imports/imports_granularity_default-with-comments.rs b/tests/source/imports/imports_granularity_default-with-comments.rs new file mode 100644 index 00000000000..2037a3835ac --- /dev/null +++ b/tests/source/imports/imports_granularity_default-with-comments.rs @@ -0,0 +1,38 @@ +// With one comment per item - after the the `;` +use crate::foo1 ; +use crate::foo1 ; /* 2nd foo1 - comment after ; */ +use crate::foo2::bar ; /* 1st foo1::bar - comment after ; */ +use crate::foo2::bar ; + +// With one comment per item - before the the `;` +use crate::foo3 ; +use crate::foo3 /* 2nd foo3 - comment before ; */ ; +use crate::foo4::bar /* 1st foo4::bar - comment before ; */ ; +use crate::foo4::bar ; + +// With multiline comments or multi comments - after the `;` +use crate::foo5; /* foo5 - Multiline comment before ; line 1 + * foo5 - Multiline comment before ; line 2 */ +use crate::foo5; +use crate::foo6; // foo6- mixed comments before ; - 1st line comment ; + /* foo6- mixed comments before ; - 2nd block comment */ +use crate::foo6; + +// With multiline comments or multi comments - before the `;` +use crate::foo8 // foo8- mixed comments before ; - 1st line comment ; + /* foo8- mixed comments before ; - 2nd block comment */ ; +use crate::foo8; + +// With two comments per item +use crate::foo11 ; /* 1st foo11 - comment */ +use crate::foo11 ; /* 2nd foo11 - comment */ + +// With one comment for a module +use crate::foo21::{self} ; /* external comment for foo21 {self} */ +use crate::foo21::{foo} ; /* external comment for foo21 {foo} */ +use crate::foo21::{bar} ; + +// With internal and external comment for a module +use crate::foo22::{self} ; /* external comment for foo22 {self} */ +use crate::foo22::{foo /* internal comment for foo22 {foo} */} ; +use crate::foo22::{bar /* internal comment for foo22 {bar} */} ; diff --git a/tests/source/imports/imports_granularity_item-with-comments.rs b/tests/source/imports/imports_granularity_item-with-comments.rs new file mode 100644 index 00000000000..de5c9d40a9b --- /dev/null +++ b/tests/source/imports/imports_granularity_item-with-comments.rs @@ -0,0 +1,24 @@ +// rustfmt-imports_granularity: Item + +// With one comment per item - after the the `;` +use crate::foo2::bar ; /* 1st foo1::bar - comment after ; */ +use crate::foo2::bar ; + +// With one comment per item - before the the `;` +use crate::foo4::bar /* 1st foo4::bar - comment before ; */ ; +use crate::foo4::bar ; + +// With multiline comments or multi comments - after the `;` +use crate::foo5; /* foo5 - Multiline comment before ; line 1 + * foo5 - Multiline comment before ; line 2 */ +use crate::foo6; // foo6- mixed comments before ; - 1st line comment ; + /* foo6- mixed comments before ; - 2nd block comment */ + +// With multiline comments or multi comments - before the `;` +use crate::foo8 // foo8- mixed comments before ; - 1st line comment ; + /* foo8- mixed comments before ; - 2nd block comment */ ; + +// With one comment for a module +use crate::foo21::{self} ; /* external comment for foo21 {self} */ +use crate::foo21::{foo} ; /* external comment for foo21 {foo} */ +use crate::foo21::{bar} ; diff --git a/tests/source/imports/imports_granularity_module-with-comments.rs b/tests/source/imports/imports_granularity_module-with-comments.rs new file mode 100644 index 00000000000..aea733b7410 --- /dev/null +++ b/tests/source/imports/imports_granularity_module-with-comments.rs @@ -0,0 +1,34 @@ +// rustfmt-imports_granularity: Module + +// With one comment per item - after the the `;` +use crate::foo1 ; +use crate::foo1 ; /* 2nd foo1 - comment after ; */ +use crate::foo2::bar ; /* 1st foo1::bar - comment after ; */ +use crate::foo2::bar ; + +// With one comment per item - before the the `;` +use crate::foo3 ; +use crate::foo3 /* 2nd foo3 - comment before ; */ ; +use crate::foo4::bar /* 1st foo4::bar - comment before ; */ ; +use crate::foo4::bar ; + +// With multiline comments or multi comments - after the `;` +use crate::foo5; /* foo5 - Multiline comment before ; line 1 + * foo5 - Multiline comment before ; line 2 */ +use crate::foo5; +use crate::foo6; // foo6- mixed comments before ; - 1st line comment ; + /* foo6- mixed comments before ; - 2nd block comment */ +use crate::foo6; + +// With multiline comments or multi comments - before the `;` +use crate::foo7 /* foo7 - Multiline comment before ; line 1 + * foo7 - Multiline comment before ; line 2 */ ; +use crate::foo7; +use crate::foo8 // foo8- mixed comments before ; - 1st line comment ; + /* foo8- mixed comments before ; - 2nd block comment */ ; +use crate::foo8; + +// With one comment for a module +use crate::foo21::{self} ; /* external comment for foo21 {self} */ +use crate::foo21::{foo} ; +use crate::foo21::{bar} ; diff --git a/tests/target/imports/imports_granularity_crate-with-comments.rs b/tests/target/imports/imports_granularity_crate-with-comments.rs new file mode 100644 index 00000000000..a27d6c43907 --- /dev/null +++ b/tests/target/imports/imports_granularity_crate-with-comments.rs @@ -0,0 +1,25 @@ +// rustfmt-imports_granularity: Crate + +// With one comment per item - after the the `;` +use crate::foo2::bar; /* 1st foo1::bar - comment after ; */ +use foo1; /* 2nd foo1 - comment after ; */ + +// With one comment per item - before the the `;` +use crate::foo4::bar; /* 1st foo4::bar - comment before ; */ +use foo3; /* 2nd foo3 - comment before ; */ + +// With multiline comments or multi comments - after the `;` +use crate::foo5; /* foo5 - Multiline comment before ; line 1 + * foo5 - Multiline comment before ; line 2 */ +use crate::{foo5, foo6}; // foo6- mixed comments before ; - 1st line comment ; +/* foo6- mixed comments before ; - 2nd block comment */ +use crate::foo6; + +// With multiline comments or multi comments - before the `;` +use crate::foo8; // foo8- mixed comments before ; - 1st line comment ; +/* foo8- mixed comments before ; - 2nd block comment */ +use crate::{foo7, foo8}; /* foo7 - Multiline comment before ; line 1 + * foo7 - Multiline comment before ; line 2 */ + +// With one comment for a module +use crate::foo21::{self, bar, foo}; /* external comment for foo21 {self} */ diff --git a/tests/target/imports/imports_granularity_default-with-comments.rs b/tests/target/imports/imports_granularity_default-with-comments.rs new file mode 100644 index 00000000000..68e2a5d57f7 --- /dev/null +++ b/tests/target/imports/imports_granularity_default-with-comments.rs @@ -0,0 +1,38 @@ +// With one comment per item - after the the `;` +use crate::foo1; +use crate::foo1; /* 2nd foo1 - comment after ; */ +use crate::foo2::bar; /* 1st foo1::bar - comment after ; */ +use crate::foo2::bar; + +// With one comment per item - before the the `;` +use crate::foo3; +use crate::foo3; /* 2nd foo3 - comment before ; */ +use crate::foo4::bar; /* 1st foo4::bar - comment before ; */ +use crate::foo4::bar; + +// With multiline comments or multi comments - after the `;` +use crate::foo5; /* foo5 - Multiline comment before ; line 1 + * foo5 - Multiline comment before ; line 2 */ +use crate::foo5; +use crate::foo6; // foo6- mixed comments before ; - 1st line comment ; +/* foo6- mixed comments before ; - 2nd block comment */ +use crate::foo6; + +// With multiline comments or multi comments - before the `;` +use crate::foo8; // foo8- mixed comments before ; - 1st line comment ; +/* foo8- mixed comments before ; - 2nd block comment */ +use crate::foo8; + +// With two comments per item +use crate::foo11; /* 1st foo11 - comment */ +use crate::foo11; /* 2nd foo11 - comment */ + +// With one comment for a module +use crate::foo21::bar; +use crate::foo21::foo; /* external comment for foo21 {foo} */ +use crate::foo21::{self}; /* external comment for foo21 {self} */ + +// With internal and external comment for a module +use crate::foo22::{self}; /* external comment for foo22 {self} */ +use crate::foo22::{bar /* internal comment for foo22 {bar} */}; +use crate::foo22::{foo /* internal comment for foo22 {foo} */}; diff --git a/tests/target/imports/imports_granularity_item-with-comments.rs b/tests/target/imports/imports_granularity_item-with-comments.rs new file mode 100644 index 00000000000..a1c83ccc4ea --- /dev/null +++ b/tests/target/imports/imports_granularity_item-with-comments.rs @@ -0,0 +1,21 @@ +// rustfmt-imports_granularity: Item + +// With one comment per item - after the the `;` +use crate::foo2::bar; /* 1st foo1::bar - comment after ; */ + +// With one comment per item - before the the `;` +use crate::foo4::bar; /* 1st foo4::bar - comment before ; */ + +// With multiline comments or multi comments - after the `;` +use crate::foo5; /* foo5 - Multiline comment before ; line 1 + * foo5 - Multiline comment before ; line 2 */ +use crate::foo6; // foo6- mixed comments before ; - 1st line comment ; +/* foo6- mixed comments before ; - 2nd block comment */ + +// With multiline comments or multi comments - before the `;` +use crate::foo8; + +// With one comment for a module +use crate::foo21::bar; +use crate::foo21::foo; /* external comment for foo21 {foo} */ +use crate::foo21::{self}; /* external comment for foo21 {self} */ diff --git a/tests/target/imports/imports_granularity_module-with-comments.rs b/tests/target/imports/imports_granularity_module-with-comments.rs new file mode 100644 index 00000000000..cc6f94eff5e --- /dev/null +++ b/tests/target/imports/imports_granularity_module-with-comments.rs @@ -0,0 +1,27 @@ +// rustfmt-imports_granularity: Module + +// With one comment per item - after the the `;` +use crate::foo1; +use crate::foo1; /* 2nd foo1 - comment after ; */ +use crate::foo2::bar; /* 1st foo1::bar - comment after ; */ + +// With one comment per item - before the the `;` +use crate::foo3; +use crate::foo3; /* 2nd foo3 - comment before ; */ +use crate::foo4::bar; /* 1st foo4::bar - comment before ; */ + +// With multiline comments or multi comments - after the `;` +use crate::foo5; /* foo5 - Multiline comment before ; line 1 + * foo5 - Multiline comment before ; line 2 */ +use crate::{foo5, foo6}; // foo6- mixed comments before ; - 1st line comment ; +/* foo6- mixed comments before ; - 2nd block comment */ +use crate::foo6; + +// With multiline comments or multi comments - before the `;` +use crate::foo8; // foo8- mixed comments before ; - 1st line comment ; +/* foo8- mixed comments before ; - 2nd block comment */ +use crate::{foo7, foo8}; /* foo7 - Multiline comment before ; line 1 + * foo7 - Multiline comment before ; line 2 */ + +// With one comment for a module +use crate::foo21::{self, bar, foo}; /* external comment for foo21 {self} */