Fix formatting of comments in empty structs (#5171)

* Fix formatting of comments in empty structs

* Add tests

* Add single line tests

* Fix block comments

* Revert changes of test source files
This commit is contained in:
Szymon Gibała 2022-01-29 05:55:47 +01:00 committed by GitHub
parent 5c558e2c11
commit b4a4bf0bf8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 251 additions and 23 deletions

View File

@ -1374,17 +1374,21 @@ 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) {
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));
}
}

113
tests/source/issue_4854.rs Normal file
View File

@ -0,0 +1,113 @@
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
*/
);
struct Struct9 { /* bar */ }
struct Struct10 { /* bar
baz
*/ }
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
*/
);
struct Struct9 { /* bar */ }
}

View File

@ -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(

View File

@ -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(

View File

@ -14,8 +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,8 +42,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 {

View File

@ -15,8 +15,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 {
@ -43,8 +43,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 {

115
tests/target/issue_4854.rs Normal file
View File

@ -0,0 +1,115 @@
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
*/
);
struct Struct9 {/* bar */}
struct Struct10 {
/* bar
baz
*/
}
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
*/
);
struct Struct9 {/* bar */}
}