Skip to content

Fix issue with extra semicolon when import comment precedes semicolon #4759

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

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
4 changes: 2 additions & 2 deletions src/formatting/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions tests/source/imports/imports_granularity_crate-with-comments.rs
Original file line number Diff line number Diff line change
@@ -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} ;
38 changes: 38 additions & 0 deletions tests/source/imports/imports_granularity_default-with-comments.rs
Original file line number Diff line number Diff line change
@@ -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} */} ;
24 changes: 24 additions & 0 deletions tests/source/imports/imports_granularity_item-with-comments.rs
Original file line number Diff line number Diff line change
@@ -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} ;
34 changes: 34 additions & 0 deletions tests/source/imports/imports_granularity_module-with-comments.rs
Original file line number Diff line number Diff line change
@@ -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} ;
25 changes: 25 additions & 0 deletions tests/target/imports/imports_granularity_crate-with-comments.rs
Original file line number Diff line number Diff line change
@@ -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} */
38 changes: 38 additions & 0 deletions tests/target/imports/imports_granularity_default-with-comments.rs
Original file line number Diff line number Diff line change
@@ -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} */};
21 changes: 21 additions & 0 deletions tests/target/imports/imports_granularity_item-with-comments.rs
Original file line number Diff line number Diff line change
@@ -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} */
27 changes: 27 additions & 0 deletions tests/target/imports/imports_granularity_module-with-comments.rs
Original file line number Diff line number Diff line change
@@ -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} */