Skip to content

Commit f3c6c56

Browse files
committed
Fix #4654 - consistent format of struct post-comments in new line - with and without trailing comma
1 parent ee2bed9 commit f3c6c56

10 files changed

+329
-33
lines changed

src/lists.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,16 @@ pub(crate) fn extract_post_comment(
616616
let white_space: &[_] = &[' ', '\t'];
617617

618618
// Cleanup post-comment: strip separators and whitespace.
619-
let post_snippet = post_snippet[..comment_end].trim();
619+
// If trimmed string starts with comment - preserve leading new-line.
620+
let post_snippet_start_trimmed = post_snippet[..comment_end].trim_start();
621+
let post_snippet = if post_snippet_start_trimmed.starts_with("//")
622+
|| post_snippet_start_trimmed.starts_with("/*")
623+
{
624+
post_snippet[..comment_end].trim_matches(white_space)
625+
} else {
626+
post_snippet_start_trimmed
627+
}
628+
.trim_end();
620629

621630
let last_inline_comment_ends_with_separator = if is_last {
622631
if let Some(line) = post_snippet.lines().last() {

tests/source/issue-4654.rs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Struct comments that start in new line
2+
3+
// No trailing comma
4+
5+
struct Foo {
6+
bar: ()
7+
// Comment
8+
}
9+
10+
struct Bar {
11+
baz: ()
12+
/*
13+
Comment
14+
*/
15+
}
16+
17+
struct Baz(
18+
()
19+
// Comment
20+
);
21+
22+
fn main() {
23+
let _ = Foo {
24+
bar: ()
25+
/*
26+
Comment
27+
*/
28+
};
29+
30+
let _ = Bar {
31+
baz: ()
32+
/*
33+
Comment
34+
*/
35+
};
36+
37+
let _ = Baz(
38+
()
39+
// Comment
40+
);
41+
}
42+
43+
// With trailing comma
44+
45+
struct Foo {
46+
bar: (),
47+
// Comment
48+
}
49+
50+
struct Bar {
51+
baz: (),
52+
/*
53+
Comment
54+
*/
55+
}
56+
57+
struct Baz(
58+
(),
59+
// Comment
60+
);
61+
62+
fn main() {
63+
let _ = Foo {
64+
bar: (),
65+
/*
66+
Comment
67+
*/
68+
};
69+
70+
let _ = Bar {
71+
baz: (),
72+
/*
73+
Comment
74+
*/
75+
};
76+
77+
let _ = Baz(
78+
(),
79+
// Comment
80+
);
81+
}
82+
83+
// With new line before trailing comma
84+
85+
struct Foo {
86+
bar: ()
87+
,
88+
// Comment
89+
}
90+
91+
struct Bar {
92+
baz: ()
93+
,
94+
/*
95+
Comment
96+
*/
97+
}
98+
99+
struct Baz(
100+
()
101+
,
102+
// Comment
103+
);
104+
105+
fn main() {
106+
let _ = Foo {
107+
bar: ()
108+
,
109+
/*
110+
Comment
111+
*/
112+
};
113+
114+
let _ = Bar {
115+
baz: ()
116+
,
117+
/*
118+
Comment
119+
*/
120+
};
121+
122+
let _ = Baz(
123+
()
124+
,
125+
// Comment
126+
);
127+
}

tests/target/issue-3198.rs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
impl TestTrait {
22
fn foo_one_pre(/* Important comment1 */ self) {}
33

4-
fn foo_one_post(self /* Important comment1 */) {}
4+
fn foo_one_post(
5+
self,
6+
/* Important comment1 */
7+
) {
8+
}
59

610
fn foo_pre(/* Important comment1 */ self, /* Important comment2 */ a: i32) {}
711

8-
fn foo_post(self /* Important comment1 */, a: i32 /* Important comment2 */) {}
12+
fn foo_post(
13+
self,
14+
/* Important comment1 */
15+
a: i32,
16+
/* Important comment2 */
17+
) {
18+
}
919

1020
fn bar_pre(/* Important comment1 */ &mut self, /* Important comment2 */ a: i32) {}
1121

12-
fn bar_post(&mut self /* Important comment1 */, a: i32 /* Important comment2 */) {}
22+
fn bar_post(
23+
&mut self,
24+
/* Important comment1 */
25+
a: i32,
26+
/* Important comment2 */
27+
) {
28+
}
1329

1430
fn baz_pre(
1531
/* Important comment1 */
@@ -20,8 +36,10 @@ impl TestTrait {
2036
}
2137

2238
fn baz_post(
23-
self: X<'a, 'b>, /* Important comment1 */
24-
a: i32, /* Important comment2 */
39+
self: X<'a, 'b>,
40+
/* Important comment1 */
41+
a: i32,
42+
/* Important comment2 */
2543
) {
2644
}
2745

@@ -36,9 +54,12 @@ impl TestTrait {
3654
}
3755

3856
fn baz_tree_post(
39-
self: X<'a, 'b>, /* Important comment1 */
40-
a: i32, /* Important comment2 */
41-
b: i32, /* Important comment3 */
57+
self: X<'a, 'b>,
58+
/* Important comment1 */
59+
a: i32,
60+
/* Important comment2 */
61+
b: i32,
62+
/* Important comment3 */
4263
) {
4364
}
4465

tests/target/issue-3532.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
fn foo(a: T) {
22
match a {
33
1 => {}
4-
0 => {} // _ => panic!("doesn't format!"),
4+
0 => {}
5+
// _ => panic!("doesn't format!"),
56
}
67
}

tests/target/issue-3675.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
fn main() {
22
println!(
3-
"{}", // comment
3+
"{}",
4+
// comment
45
111
56
);
67
}

tests/target/issue-4654.rs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Struct comments that start in new line
2+
3+
// No trailing comma
4+
5+
struct Foo {
6+
bar: (),
7+
// Comment
8+
}
9+
10+
struct Bar {
11+
baz: (),
12+
/*
13+
Comment
14+
*/
15+
}
16+
17+
struct Baz(
18+
(),
19+
// Comment
20+
);
21+
22+
fn main() {
23+
let _ = Foo {
24+
bar: (),
25+
/*
26+
Comment
27+
*/
28+
};
29+
30+
let _ = Bar {
31+
baz: (),
32+
/*
33+
Comment
34+
*/
35+
};
36+
37+
let _ = Baz(
38+
(),
39+
// Comment
40+
);
41+
}
42+
43+
// With trailing comma
44+
45+
struct Foo {
46+
bar: (),
47+
// Comment
48+
}
49+
50+
struct Bar {
51+
baz: (),
52+
/*
53+
Comment
54+
*/
55+
}
56+
57+
struct Baz(
58+
(),
59+
// Comment
60+
);
61+
62+
fn main() {
63+
let _ = Foo {
64+
bar: (),
65+
/*
66+
Comment
67+
*/
68+
};
69+
70+
let _ = Bar {
71+
baz: (),
72+
/*
73+
Comment
74+
*/
75+
};
76+
77+
let _ = Baz(
78+
(),
79+
// Comment
80+
);
81+
}
82+
83+
// With new line before trailing comma
84+
85+
struct Foo {
86+
bar: (),
87+
// Comment
88+
}
89+
90+
struct Bar {
91+
baz: (),
92+
/*
93+
Comment
94+
*/
95+
}
96+
97+
struct Baz(
98+
(),
99+
// Comment
100+
);
101+
102+
fn main() {
103+
let _ = Foo {
104+
bar: (),
105+
/*
106+
Comment
107+
*/
108+
};
109+
110+
let _ = Bar {
111+
baz: (),
112+
/*
113+
Comment
114+
*/
115+
};
116+
117+
let _ = Baz(
118+
(),
119+
// Comment
120+
);
121+
}

tests/target/issue-5042/multi-line_comment_with_trailing_comma.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,27 @@ fn main() {
22
// 5042 deals with trailing commas, not the indentation issue of these comments
33
// When a future PR fixes the inentation issues these test can be updated
44
let _ = std::ops::Add::add(
5-
10, 20, // ...
6-
// ...,
5+
10, 20,
6+
// ...
7+
// ...,
78
);
89

910
let _ = std::ops::Add::add(
10-
10, 20, /* ... */
11-
// ...,
11+
10, 20,
12+
/* ... */
13+
// ...,
1214
);
1315

1416
let _ = std::ops::Add::add(
15-
10, 20, // ...,
16-
// ...,
17+
10, 20,
18+
// ...,
19+
// ...,
1720
);
1821

1922
let _ = std::ops::Add::add(
20-
10, 20, // ...,
21-
/* ...
22-
*/
23+
10, 20,
24+
// ...,
25+
/* ...
26+
*/
2327
);
2428
}

0 commit comments

Comments
 (0)