From 354da64186d262873d26b1874cde35241427b39f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Giba=C5=82a?= Date: Fri, 14 Jan 2022 10:12:07 +0100 Subject: [PATCH 1/5] Fix formatting of comments in empty structs --- src/items.rs | 5 ++++- .../comments-in-lists/wrap-comments-not-normalized.rs | 10 ++++------ tests/source/comments-in-lists/wrap-comments-true.rs | 10 ++++------ tests/target/comments-in-lists/format-doc-comments.rs | 10 ++++------ tests/target/comments-in-lists/wrap-comments-false.rs | 10 ++++------ .../comments-in-lists/wrap-comments-not-normalized.rs | 10 ++++------ tests/target/comments-in-lists/wrap-comments-true.rs | 10 ++++------ 7 files changed, 28 insertions(+), 37 deletions(-) diff --git a/src/items.rs b/src/items.rs index babc56f86ed..53855216472 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1374,7 +1374,10 @@ fn format_empty_struct_or_tuple( result.push_str(&offset.to_string_with_newline(context.config)) } result.push_str(opener); - match rewrite_missing_comment(span, Shape::indented(offset, context.config), context) { + + // indented shape for proper indenting of multi-line comments + let shape = Shape::indented(offset.block_indent(context.config), context.config); + match rewrite_missing_comment(span, shape, context) { Some(ref s) if s.is_empty() => (), Some(ref s) => { if !is_single_line(s) || first_line_contains_single_line_comment(s) { diff --git a/tests/source/comments-in-lists/wrap-comments-not-normalized.rs b/tests/source/comments-in-lists/wrap-comments-not-normalized.rs index b96c02802d6..077e31a5cae 100644 --- a/tests/source/comments-in-lists/wrap-comments-not-normalized.rs +++ b/tests/source/comments-in-lists/wrap-comments-not-normalized.rs @@ -13,9 +13,8 @@ pub enum E { } pub enum E2 { - // This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed -// Expand as needed, numbers should be ascending according to the stage -// through the inclusion pipeline, or according to the descriptions + // Expand as needed, numbers should be ascending according to the stage + // through the inclusion pipeline, or according to the descriptions } pub enum E3 { @@ -39,9 +38,8 @@ pub struct S { } pub struct S2 { - // This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed -// Expand as needed, numbers should be ascending according to the stage -// through the inclusion pipeline, or according to the descriptions + // Expand as needed, numbers should be ascending according to the stage + // through the inclusion pipeline, or according to the descriptions } pub struct S3 { diff --git a/tests/source/comments-in-lists/wrap-comments-true.rs b/tests/source/comments-in-lists/wrap-comments-true.rs index 360b838520e..dffdfc9cb50 100644 --- a/tests/source/comments-in-lists/wrap-comments-true.rs +++ b/tests/source/comments-in-lists/wrap-comments-true.rs @@ -14,9 +14,8 @@ pub enum E { } pub enum E2 { - // This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed -// Expand as needed, numbers should be ascending according to the stage -// through the inclusion pipeline, or according to the descriptions + // Expand as needed, numbers should be ascending according to the stage + // through the inclusion pipeline, or according to the descriptions } pub enum E3 { @@ -40,9 +39,8 @@ pub struct S { } pub struct S2 { - // This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed -// Expand as needed, numbers should be ascending according to the stage -// through the inclusion pipeline, or according to the descriptions + // Expand as needed, numbers should be ascending according to the stage + // through the inclusion pipeline, or according to the descriptions } pub struct S3 { diff --git a/tests/target/comments-in-lists/format-doc-comments.rs b/tests/target/comments-in-lists/format-doc-comments.rs index be31bf0a331..be4b7a8c42e 100644 --- a/tests/target/comments-in-lists/format-doc-comments.rs +++ b/tests/target/comments-in-lists/format-doc-comments.rs @@ -25,9 +25,8 @@ pub enum E { } pub enum E2 { - // This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed -// Expand as needed, numbers should be ascending according to the stage -// through the inclusion pipeline, or according to the descriptions + // Expand as needed, numbers should be ascending according to the stage + // through the inclusion pipeline, or according to the descriptions } pub struct S { @@ -42,9 +41,8 @@ pub struct S { } pub struct S2 { - // This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed -// Expand as needed, numbers should be ascending according to the stage -// through the inclusion pipeline, or according to the descriptions + // Expand as needed, numbers should be ascending according to the stage + // through the inclusion pipeline, or according to the descriptions } fn foo( diff --git a/tests/target/comments-in-lists/wrap-comments-false.rs b/tests/target/comments-in-lists/wrap-comments-false.rs index 80aea59d1b5..db4da622372 100644 --- a/tests/target/comments-in-lists/wrap-comments-false.rs +++ b/tests/target/comments-in-lists/wrap-comments-false.rs @@ -13,9 +13,8 @@ pub enum E { } pub enum E2 { - // This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed -// Expand as needed, numbers should be ascending according to the stage -// through the inclusion pipeline, or according to the descriptions + // Expand as needed, numbers should be ascending according to the stage + // through the inclusion pipeline, or according to the descriptions } pub struct S { @@ -30,9 +29,8 @@ pub struct S { } pub struct S2 { - // This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed -// Expand as needed, numbers should be ascending according to the stage -// through the inclusion pipeline, or according to the descriptions + // Expand as needed, numbers should be ascending according to the stage + // through the inclusion pipeline, or according to the descriptions } fn foo( diff --git a/tests/target/comments-in-lists/wrap-comments-not-normalized.rs b/tests/target/comments-in-lists/wrap-comments-not-normalized.rs index 52315f470e4..fd9a1ea3155 100644 --- a/tests/target/comments-in-lists/wrap-comments-not-normalized.rs +++ b/tests/target/comments-in-lists/wrap-comments-not-normalized.rs @@ -13,9 +13,8 @@ pub enum E { } pub enum E2 { - // This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed -// Expand as needed, numbers should be ascending according to the stage -// through the inclusion pipeline, or according to the descriptions + // Expand as needed, numbers should be ascending according to the stage + // through the inclusion pipeline, or according to the descriptions } pub enum E3 { @@ -41,9 +40,8 @@ pub struct S { } pub struct S2 { - // This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed -// Expand as needed, numbers should be ascending according to the stage -// through the inclusion pipeline, or according to the descriptions + // Expand as needed, numbers should be ascending according to the stage + // through the inclusion pipeline, or according to the descriptions } pub struct S3 { diff --git a/tests/target/comments-in-lists/wrap-comments-true.rs b/tests/target/comments-in-lists/wrap-comments-true.rs index e0bfcf0b500..8d31bc5aff2 100644 --- a/tests/target/comments-in-lists/wrap-comments-true.rs +++ b/tests/target/comments-in-lists/wrap-comments-true.rs @@ -14,9 +14,8 @@ pub enum E { } pub enum E2 { - // This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed -// Expand as needed, numbers should be ascending according to the stage -// through the inclusion pipeline, or according to the descriptions + // Expand as needed, numbers should be ascending according to the stage + // through the inclusion pipeline, or according to the descriptions } pub enum E3 { @@ -42,9 +41,8 @@ pub struct S { } pub struct S2 { - // This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed -// Expand as needed, numbers should be ascending according to the stage -// through the inclusion pipeline, or according to the descriptions + // Expand as needed, numbers should be ascending according to the stage + // through the inclusion pipeline, or according to the descriptions } pub struct S3 { From 7f3f9bde110b06621bb4f7e9b2e1330f64a386e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Giba=C5=82a?= Date: Tue, 18 Jan 2022 18:00:09 +0100 Subject: [PATCH 2/5] Add tests --- tests/source/issue_4854.rs | 105 +++++++++++++++++++++++++++++++++++++ tests/target/issue_4854.rs | 97 ++++++++++++++++++++++++++++++++++ 2 files changed, 202 insertions(+) create mode 100644 tests/source/issue_4854.rs create mode 100644 tests/target/issue_4854.rs diff --git a/tests/source/issue_4854.rs b/tests/source/issue_4854.rs new file mode 100644 index 00000000000..48bf996be4f --- /dev/null +++ b/tests/source/issue_4854.rs @@ -0,0 +1,105 @@ +struct Struct { + // Multiline comment + // should be formatted + // properly. +} + +struct Struct2 { + // This formatting +// Should be changed +} + +struct Struct3( + // This + // is + // correct +); + +struct Struct4( + // This +// is +// not +// correct +); + +struct Struct5 { + /* + Comment block + with many lines. + */ +} + +struct Struct6( + /* + Comment block + with many lines. + */ +); + +struct Struct7 { + /* +Invalid +format +*/ +} + +struct Struct8( + /* +Invalid +format +*/ +); + +mod module { + struct Struct { + // Multiline comment + // should be formatted + // properly. + } + + struct Struct2 { + // This formatting +// Should be changed + } + + struct Struct3( + // This + // is + // correct + ); + + struct Struct4( + // This + // is + // not +// correct + ); + + struct Struct5 { + /* + Comment block + with many lines. + */ + } + + struct Struct6( + /* + Comment block + with many lines. + */ + ); + + struct Struct7 { + /* +Invalid +format +*/ + } + + struct Struct8( + /* +Invalid +format +*/ + ); +} diff --git a/tests/target/issue_4854.rs b/tests/target/issue_4854.rs new file mode 100644 index 00000000000..ab48ba78eee --- /dev/null +++ b/tests/target/issue_4854.rs @@ -0,0 +1,97 @@ +struct Struct { + // Multiline comment + // should be formatted + // properly. +} + +struct Struct2 { + // This formatting + // Should be changed +} + +struct Struct3( + // This + // is + // correct +); + +struct Struct4( + // This + // is + // not + // correct +); + +struct Struct5 { + /* + Comment block + with many lines. + */} + +struct Struct6( + /* + Comment block + with many lines. + */); + +struct Struct7 { + /* + Invalid + format + */} + +struct Struct8( + /* + Invalid + format + */); + +mod module { + struct Struct { + // Multiline comment + // should be formatted + // properly. + } + + struct Struct2 { + // This formatting + // Should be changed + } + + struct Struct3( + // This + // is + // correct + ); + + struct Struct4( + // This + // is + // not + // correct + ); + + struct Struct5 { + /* + Comment block + with many lines. + */} + + struct Struct6( + /* + Comment block + with many lines. + */); + + struct Struct7 { + /* + Invalid + format + */} + + struct Struct8( + /* + Invalid + format + */); +} From 06a0a34b3068cac7a737370d054a2eaf118416de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Giba=C5=82a?= Date: Tue, 18 Jan 2022 18:12:08 +0100 Subject: [PATCH 3/5] Add single line tests --- tests/source/issue_4854.rs | 8 ++++++++ tests/target/issue_4854.rs | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/tests/source/issue_4854.rs b/tests/source/issue_4854.rs index 48bf996be4f..35d6e21affe 100644 --- a/tests/source/issue_4854.rs +++ b/tests/source/issue_4854.rs @@ -50,6 +50,12 @@ format */ ); +struct Struct9 { /* bar */ } + +struct Struct10 { /* bar +baz +*/ } + mod module { struct Struct { // Multiline comment @@ -102,4 +108,6 @@ Invalid format */ ); + + struct Struct9 { /* bar */ } } diff --git a/tests/target/issue_4854.rs b/tests/target/issue_4854.rs index ab48ba78eee..0b7070ea6cf 100644 --- a/tests/target/issue_4854.rs +++ b/tests/target/issue_4854.rs @@ -46,6 +46,13 @@ struct Struct8( format */); +struct Struct9 {/* bar */} + +struct Struct10 { + /* bar + baz + */} + mod module { struct Struct { // Multiline comment @@ -94,4 +101,6 @@ mod module { Invalid format */); + + struct Struct9 {/* bar */} } From 7dece813e9e84311e63eaaefa984b397958f188a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Giba=C5=82a?= Date: Wed, 19 Jan 2022 16:41:45 +0100 Subject: [PATCH 4/5] Fix block comments --- src/items.rs | 5 +++-- tests/target/issue_4854.rs | 27 ++++++++++++++++++--------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/items.rs b/src/items.rs index 53855216472..4c7a33c86d2 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1380,14 +1380,15 @@ fn format_empty_struct_or_tuple( match rewrite_missing_comment(span, shape, context) { Some(ref s) if s.is_empty() => (), Some(ref s) => { - if !is_single_line(s) || first_line_contains_single_line_comment(s) { + let is_multi_line = !is_single_line(s); + if is_multi_line || first_line_contains_single_line_comment(s) { let nested_indent_str = offset .block_indent(context.config) .to_string_with_newline(context.config); result.push_str(&nested_indent_str); } result.push_str(s); - if last_line_contains_single_line_comment(s) { + if is_multi_line || last_line_contains_single_line_comment(s) { result.push_str(&offset.to_string_with_newline(context.config)); } } diff --git a/tests/target/issue_4854.rs b/tests/target/issue_4854.rs index 0b7070ea6cf..a81c5a5171f 100644 --- a/tests/target/issue_4854.rs +++ b/tests/target/issue_4854.rs @@ -26,32 +26,37 @@ struct Struct5 { /* Comment block with many lines. - */} + */ +} struct Struct6( /* Comment block with many lines. - */); + */ +); struct Struct7 { /* Invalid format - */} + */ +} struct Struct8( /* Invalid format - */); + */ +); struct Struct9 {/* bar */} struct Struct10 { /* bar baz - */} + */ +} mod module { struct Struct { @@ -82,25 +87,29 @@ mod module { /* Comment block with many lines. - */} + */ + } struct Struct6( /* Comment block with many lines. - */); + */ + ); struct Struct7 { /* Invalid format - */} + */ + } struct Struct8( /* Invalid format - */); + */ + ); struct Struct9 {/* bar */} } From 21dc3404693160114bb2df83e6455033c7a34bab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szymon=20Giba=C5=82a?= Date: Fri, 28 Jan 2022 09:45:41 +0100 Subject: [PATCH 5/5] Revert changes of test source files --- .../comments-in-lists/wrap-comments-not-normalized.rs | 10 ++++++---- tests/source/comments-in-lists/wrap-comments-true.rs | 10 ++++++---- .../comments-in-lists/wrap-comments-not-normalized.rs | 2 ++ tests/target/comments-in-lists/wrap-comments-true.rs | 2 ++ 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/tests/source/comments-in-lists/wrap-comments-not-normalized.rs b/tests/source/comments-in-lists/wrap-comments-not-normalized.rs index 077e31a5cae..b96c02802d6 100644 --- a/tests/source/comments-in-lists/wrap-comments-not-normalized.rs +++ b/tests/source/comments-in-lists/wrap-comments-not-normalized.rs @@ -13,8 +13,9 @@ pub enum E { } pub enum E2 { - // Expand as needed, numbers should be ascending according to the stage - // through the inclusion pipeline, or according to the descriptions + // This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed +// Expand as needed, numbers should be ascending according to the stage +// through the inclusion pipeline, or according to the descriptions } pub enum E3 { @@ -38,8 +39,9 @@ pub struct S { } pub struct S2 { - // Expand as needed, numbers should be ascending according to the stage - // through the inclusion pipeline, or according to the descriptions + // This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed +// Expand as needed, numbers should be ascending according to the stage +// through the inclusion pipeline, or according to the descriptions } pub struct S3 { diff --git a/tests/source/comments-in-lists/wrap-comments-true.rs b/tests/source/comments-in-lists/wrap-comments-true.rs index dffdfc9cb50..360b838520e 100644 --- a/tests/source/comments-in-lists/wrap-comments-true.rs +++ b/tests/source/comments-in-lists/wrap-comments-true.rs @@ -14,8 +14,9 @@ pub enum E { } pub enum E2 { - // Expand as needed, numbers should be ascending according to the stage - // through the inclusion pipeline, or according to the descriptions + // This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed +// Expand as needed, numbers should be ascending according to the stage +// through the inclusion pipeline, or according to the descriptions } pub enum E3 { @@ -39,8 +40,9 @@ pub struct S { } pub struct S2 { - // Expand as needed, numbers should be ascending according to the stage - // through the inclusion pipeline, or according to the descriptions + // This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed +// Expand as needed, numbers should be ascending according to the stage +// through the inclusion pipeline, or according to the descriptions } pub struct S3 { diff --git a/tests/target/comments-in-lists/wrap-comments-not-normalized.rs b/tests/target/comments-in-lists/wrap-comments-not-normalized.rs index fd9a1ea3155..9b9147eb124 100644 --- a/tests/target/comments-in-lists/wrap-comments-not-normalized.rs +++ b/tests/target/comments-in-lists/wrap-comments-not-normalized.rs @@ -13,6 +13,7 @@ pub enum E { } pub enum E2 { + // This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed // Expand as needed, numbers should be ascending according to the stage // through the inclusion pipeline, or according to the descriptions } @@ -40,6 +41,7 @@ pub struct S { } pub struct S2 { + // This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed // Expand as needed, numbers should be ascending according to the stage // through the inclusion pipeline, or according to the descriptions } diff --git a/tests/target/comments-in-lists/wrap-comments-true.rs b/tests/target/comments-in-lists/wrap-comments-true.rs index 8d31bc5aff2..c1531d22a4a 100644 --- a/tests/target/comments-in-lists/wrap-comments-true.rs +++ b/tests/target/comments-in-lists/wrap-comments-true.rs @@ -14,6 +14,7 @@ pub enum E { } pub enum E2 { + // This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed // Expand as needed, numbers should be ascending according to the stage // through the inclusion pipeline, or according to the descriptions } @@ -41,6 +42,7 @@ pub struct S { } pub struct S2 { + // This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed // Expand as needed, numbers should be ascending according to the stage // through the inclusion pipeline, or according to the descriptions }