Skip to content

Commit 74768b7

Browse files
authored
Fixed Issue 'Trailing comma is duplicated when obstructed by comments' (#4520)
* Fixed Issue 'Trailing comma is duplicated when obstructed by comments' * Put separator at the begining of the post comment * Removed accidentally pushed tmp file * Add test cases for #4714 * Avoid ownership issue by a tricky way and added test cases for block comments * Fixed tests
1 parent 2e1a982 commit 74768b7

File tree

5 files changed

+126
-1
lines changed

5 files changed

+126
-1
lines changed

Diff for: src/formatting/lists.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,12 @@ where
417417
result.push_str(&formatted_comment);
418418
}
419419

420-
if separate && sep_place.is_back() {
420+
let need_extra_separator = separate
421+
&& last
422+
&& item.post_comment.as_ref().map_or(true, |cmnt| {
423+
cmnt.find_uncommented(formatting.separator).is_none()
424+
});
425+
if separate && sep_place.is_back() && (!last || need_extra_separator) {
421426
result.push_str(formatting.separator);
422427
}
423428

@@ -733,6 +738,7 @@ pub(crate) fn extract_post_comment(
733738

734739
// Cleanup post-comment: strip separators and whitespace.
735740
let post_snippet = post_snippet[..comment_end].trim();
741+
let mut _post_snippet_without_sep = String::new();
736742
let post_snippet_trimmed = if post_snippet.starts_with(|c| c == ',' || c == ':') {
737743
post_snippet[1..].trim_matches(white_space)
738744
} else if let Some(post_snippet) = post_snippet.strip_prefix(separator) {
@@ -743,6 +749,19 @@ pub(crate) fn extract_post_comment(
743749
&& (!post_snippet.trim().starts_with("//") || post_snippet.trim().contains('\n'))
744750
{
745751
post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space)
752+
} else if let Some(sep_pos) = post_snippet.find_uncommented(",") {
753+
_post_snippet_without_sep = [
754+
post_snippet[..sep_pos]
755+
.trim_matches(white_space)
756+
.trim_matches('\n'),
757+
"\n",
758+
post_snippet[sep_pos + separator.len()..]
759+
.trim_matches(white_space)
760+
.trim_matches('\n'),
761+
]
762+
.concat();
763+
764+
&_post_snippet_without_sep
746765
} else {
747766
post_snippet
748767
};

Diff for: tests/source/issue-3876.rs

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
fn f<S>(s: S)
2+
where
3+
S: Send /* */,
4+
//
5+
{
6+
}
7+
8+
fn main() {
9+
foo(
10+
x
11+
// comment 1
12+
,
13+
// comment 2
14+
)
15+
}
16+
17+
fn main() {
18+
foo(
19+
x
20+
// comment 1
21+
22+
23+
,
24+
25+
26+
27+
// comment 2
28+
)
29+
}
30+
31+
fn main() {
32+
foo(
33+
x
34+
/* Comment 1 */
35+
,
36+
/* Comment 2 */
37+
)
38+
}
39+
40+
fn main() {
41+
foo(
42+
x /* Comment 1 */ , /* Comment 2 */
43+
)
44+
}
45+
46+
fn main() {
47+
foo(
48+
x
49+
/* comment 1 */
50+
51+
52+
,
53+
54+
55+
56+
/* comment 2 */
57+
)
58+
}

Diff for: tests/source/issue-4714.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
struct Something {
2+
field: u32/*a*/,/*b*/
3+
}

Diff for: tests/target/issue-3876.rs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
fn f<S>(s: S)
2+
where
3+
S: Send, /* */
4+
//
5+
{
6+
}
7+
8+
fn main() {
9+
foo(
10+
x, // comment 1
11+
// comment 2
12+
)
13+
}
14+
15+
fn main() {
16+
foo(
17+
x, // comment 1
18+
// comment 2
19+
)
20+
}
21+
22+
fn main() {
23+
foo(
24+
x, /* Comment 1 */
25+
/* Comment 2 */
26+
)
27+
}
28+
29+
fn main() {
30+
foo(
31+
x, /* Comment 1 */
32+
/* Comment 2 */
33+
)
34+
}
35+
36+
fn main() {
37+
foo(
38+
x, /* comment 1 */
39+
/* comment 2 */
40+
)
41+
}

Diff for: tests/target/issue-4714.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
struct Something {
2+
field: u32, /*a*/
3+
/*b*/
4+
}

0 commit comments

Comments
 (0)