Skip to content

Commit

Permalink
Fix formatting of comments in empty structs (#5171)
Browse files Browse the repository at this point in the history
* Fix formatting of comments in empty structs

* Add tests

* Add single line tests

* Fix block comments

* Revert changes of test source files
  • Loading branch information
Szymongib authored Jan 29, 2022
1 parent 5c558e2 commit b4a4bf0
Show file tree
Hide file tree
Showing 7 changed files with 251 additions and 23 deletions.
10 changes: 7 additions & 3 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
113 changes: 113 additions & 0 deletions tests/source/issue_4854.rs
Original file line number Diff line number Diff line change
@@ -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 */ }
}
10 changes: 4 additions & 6 deletions tests/target/comments-in-lists/format-doc-comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(
Expand Down
10 changes: 4 additions & 6 deletions tests/target/comments-in-lists/wrap-comments-false.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions tests/target/comments-in-lists/wrap-comments-true.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
115 changes: 115 additions & 0 deletions tests/target/issue_4854.rs
Original file line number Diff line number Diff line change
@@ -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 */}
}

0 comments on commit b4a4bf0

Please sign in to comment.