Skip to content

Commit b4a4bf0

Browse files
authored
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
1 parent 5c558e2 commit b4a4bf0

File tree

7 files changed

+251
-23
lines changed

7 files changed

+251
-23
lines changed

Diff for: src/items.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1374,17 +1374,21 @@ fn format_empty_struct_or_tuple(
13741374
result.push_str(&offset.to_string_with_newline(context.config))
13751375
}
13761376
result.push_str(opener);
1377-
match rewrite_missing_comment(span, Shape::indented(offset, context.config), context) {
1377+
1378+
// indented shape for proper indenting of multi-line comments
1379+
let shape = Shape::indented(offset.block_indent(context.config), context.config);
1380+
match rewrite_missing_comment(span, shape, context) {
13781381
Some(ref s) if s.is_empty() => (),
13791382
Some(ref s) => {
1380-
if !is_single_line(s) || first_line_contains_single_line_comment(s) {
1383+
let is_multi_line = !is_single_line(s);
1384+
if is_multi_line || first_line_contains_single_line_comment(s) {
13811385
let nested_indent_str = offset
13821386
.block_indent(context.config)
13831387
.to_string_with_newline(context.config);
13841388
result.push_str(&nested_indent_str);
13851389
}
13861390
result.push_str(s);
1387-
if last_line_contains_single_line_comment(s) {
1391+
if is_multi_line || last_line_contains_single_line_comment(s) {
13881392
result.push_str(&offset.to_string_with_newline(context.config));
13891393
}
13901394
}

Diff for: tests/source/issue_4854.rs

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
struct Struct {
2+
// Multiline comment
3+
// should be formatted
4+
// properly.
5+
}
6+
7+
struct Struct2 {
8+
// This formatting
9+
// Should be changed
10+
}
11+
12+
struct Struct3(
13+
// This
14+
// is
15+
// correct
16+
);
17+
18+
struct Struct4(
19+
// This
20+
// is
21+
// not
22+
// correct
23+
);
24+
25+
struct Struct5 {
26+
/*
27+
Comment block
28+
with many lines.
29+
*/
30+
}
31+
32+
struct Struct6(
33+
/*
34+
Comment block
35+
with many lines.
36+
*/
37+
);
38+
39+
struct Struct7 {
40+
/*
41+
Invalid
42+
format
43+
*/
44+
}
45+
46+
struct Struct8(
47+
/*
48+
Invalid
49+
format
50+
*/
51+
);
52+
53+
struct Struct9 { /* bar */ }
54+
55+
struct Struct10 { /* bar
56+
baz
57+
*/ }
58+
59+
mod module {
60+
struct Struct {
61+
// Multiline comment
62+
// should be formatted
63+
// properly.
64+
}
65+
66+
struct Struct2 {
67+
// This formatting
68+
// Should be changed
69+
}
70+
71+
struct Struct3(
72+
// This
73+
// is
74+
// correct
75+
);
76+
77+
struct Struct4(
78+
// This
79+
// is
80+
// not
81+
// correct
82+
);
83+
84+
struct Struct5 {
85+
/*
86+
Comment block
87+
with many lines.
88+
*/
89+
}
90+
91+
struct Struct6(
92+
/*
93+
Comment block
94+
with many lines.
95+
*/
96+
);
97+
98+
struct Struct7 {
99+
/*
100+
Invalid
101+
format
102+
*/
103+
}
104+
105+
struct Struct8(
106+
/*
107+
Invalid
108+
format
109+
*/
110+
);
111+
112+
struct Struct9 { /* bar */ }
113+
}

Diff for: tests/target/comments-in-lists/format-doc-comments.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ pub enum E {
2525
}
2626

2727
pub enum E2 {
28-
// This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed
29-
// Expand as needed, numbers should be ascending according to the stage
30-
// through the inclusion pipeline, or according to the descriptions
28+
// Expand as needed, numbers should be ascending according to the stage
29+
// through the inclusion pipeline, or according to the descriptions
3130
}
3231

3332
pub struct S {
@@ -42,9 +41,8 @@ pub struct S {
4241
}
4342

4443
pub struct S2 {
45-
// This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed
46-
// Expand as needed, numbers should be ascending according to the stage
47-
// through the inclusion pipeline, or according to the descriptions
44+
// Expand as needed, numbers should be ascending according to the stage
45+
// through the inclusion pipeline, or according to the descriptions
4846
}
4947

5048
fn foo(

Diff for: tests/target/comments-in-lists/wrap-comments-false.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ pub enum E {
1313
}
1414

1515
pub enum E2 {
16-
// This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed
17-
// Expand as needed, numbers should be ascending according to the stage
18-
// through the inclusion pipeline, or according to the descriptions
16+
// Expand as needed, numbers should be ascending according to the stage
17+
// through the inclusion pipeline, or according to the descriptions
1918
}
2019

2120
pub struct S {
@@ -30,9 +29,8 @@ pub struct S {
3029
}
3130

3231
pub struct S2 {
33-
// This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed
34-
// Expand as needed, numbers should be ascending according to the stage
35-
// through the inclusion pipeline, or according to the descriptions
32+
// Expand as needed, numbers should be ascending according to the stage
33+
// through the inclusion pipeline, or according to the descriptions
3634
}
3735

3836
fn foo(

Diff for: tests/target/comments-in-lists/wrap-comments-not-normalized.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ pub enum E {
1414

1515
pub enum E2 {
1616
// This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed
17-
// Expand as needed, numbers should be ascending according to the stage
18-
// through the inclusion pipeline, or according to the descriptions
17+
// Expand as needed, numbers should be ascending according to the stage
18+
// through the inclusion pipeline, or according to the descriptions
1919
}
2020

2121
pub enum E3 {
@@ -42,8 +42,8 @@ pub struct S {
4242

4343
pub struct S2 {
4444
// This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed
45-
// Expand as needed, numbers should be ascending according to the stage
46-
// through the inclusion pipeline, or according to the descriptions
45+
// Expand as needed, numbers should be ascending according to the stage
46+
// through the inclusion pipeline, or according to the descriptions
4747
}
4848

4949
pub struct S3 {

Diff for: tests/target/comments-in-lists/wrap-comments-true.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ pub enum E {
1515

1616
pub enum E2 {
1717
// This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed
18-
// Expand as needed, numbers should be ascending according to the stage
19-
// through the inclusion pipeline, or according to the descriptions
18+
// Expand as needed, numbers should be ascending according to the stage
19+
// through the inclusion pipeline, or according to the descriptions
2020
}
2121

2222
pub enum E3 {
@@ -43,8 +43,8 @@ pub struct S {
4343

4444
pub struct S2 {
4545
// This can be changed once https://github.com/rust-lang/rustfmt/issues/4854 is fixed
46-
// Expand as needed, numbers should be ascending according to the stage
47-
// through the inclusion pipeline, or according to the descriptions
46+
// Expand as needed, numbers should be ascending according to the stage
47+
// through the inclusion pipeline, or according to the descriptions
4848
}
4949

5050
pub struct S3 {

Diff for: tests/target/issue_4854.rs

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
struct Struct {
2+
// Multiline comment
3+
// should be formatted
4+
// properly.
5+
}
6+
7+
struct Struct2 {
8+
// This formatting
9+
// Should be changed
10+
}
11+
12+
struct Struct3(
13+
// This
14+
// is
15+
// correct
16+
);
17+
18+
struct Struct4(
19+
// This
20+
// is
21+
// not
22+
// correct
23+
);
24+
25+
struct Struct5 {
26+
/*
27+
Comment block
28+
with many lines.
29+
*/
30+
}
31+
32+
struct Struct6(
33+
/*
34+
Comment block
35+
with many lines.
36+
*/
37+
);
38+
39+
struct Struct7 {
40+
/*
41+
Invalid
42+
format
43+
*/
44+
}
45+
46+
struct Struct8(
47+
/*
48+
Invalid
49+
format
50+
*/
51+
);
52+
53+
struct Struct9 {/* bar */}
54+
55+
struct Struct10 {
56+
/* bar
57+
baz
58+
*/
59+
}
60+
61+
mod module {
62+
struct Struct {
63+
// Multiline comment
64+
// should be formatted
65+
// properly.
66+
}
67+
68+
struct Struct2 {
69+
// This formatting
70+
// Should be changed
71+
}
72+
73+
struct Struct3(
74+
// This
75+
// is
76+
// correct
77+
);
78+
79+
struct Struct4(
80+
// This
81+
// is
82+
// not
83+
// correct
84+
);
85+
86+
struct Struct5 {
87+
/*
88+
Comment block
89+
with many lines.
90+
*/
91+
}
92+
93+
struct Struct6(
94+
/*
95+
Comment block
96+
with many lines.
97+
*/
98+
);
99+
100+
struct Struct7 {
101+
/*
102+
Invalid
103+
format
104+
*/
105+
}
106+
107+
struct Struct8(
108+
/*
109+
Invalid
110+
format
111+
*/
112+
);
113+
114+
struct Struct9 {/* bar */}
115+
}

0 commit comments

Comments
 (0)