Skip to content

Commit

Permalink
Fixed Issue 'Trailing comma is duplicated when obstructed by comments' (
Browse files Browse the repository at this point in the history
#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
  • Loading branch information
whizsid authored Feb 27, 2021
1 parent 2e1a982 commit 74768b7
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/formatting/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,12 @@ where
result.push_str(&formatted_comment);
}

if separate && sep_place.is_back() {
let need_extra_separator = separate
&& last
&& item.post_comment.as_ref().map_or(true, |cmnt| {
cmnt.find_uncommented(formatting.separator).is_none()
});
if separate && sep_place.is_back() && (!last || need_extra_separator) {
result.push_str(formatting.separator);
}

Expand Down Expand Up @@ -733,6 +738,7 @@ pub(crate) fn extract_post_comment(

// Cleanup post-comment: strip separators and whitespace.
let post_snippet = post_snippet[..comment_end].trim();
let mut _post_snippet_without_sep = String::new();
let post_snippet_trimmed = if post_snippet.starts_with(|c| c == ',' || c == ':') {
post_snippet[1..].trim_matches(white_space)
} else if let Some(post_snippet) = post_snippet.strip_prefix(separator) {
Expand All @@ -743,6 +749,19 @@ pub(crate) fn extract_post_comment(
&& (!post_snippet.trim().starts_with("//") || post_snippet.trim().contains('\n'))
{
post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space)
} else if let Some(sep_pos) = post_snippet.find_uncommented(",") {
_post_snippet_without_sep = [
post_snippet[..sep_pos]
.trim_matches(white_space)
.trim_matches('\n'),
"\n",
post_snippet[sep_pos + separator.len()..]
.trim_matches(white_space)
.trim_matches('\n'),
]
.concat();

&_post_snippet_without_sep
} else {
post_snippet
};
Expand Down
58 changes: 58 additions & 0 deletions tests/source/issue-3876.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
fn f<S>(s: S)
where
S: Send /* */,
//
{
}

fn main() {
foo(
x
// comment 1
,
// comment 2
)
}

fn main() {
foo(
x
// comment 1


,



// comment 2
)
}

fn main() {
foo(
x
/* Comment 1 */
,
/* Comment 2 */
)
}

fn main() {
foo(
x /* Comment 1 */ , /* Comment 2 */
)
}

fn main() {
foo(
x
/* comment 1 */


,



/* comment 2 */
)
}
3 changes: 3 additions & 0 deletions tests/source/issue-4714.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
struct Something {
field: u32/*a*/,/*b*/
}
41 changes: 41 additions & 0 deletions tests/target/issue-3876.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
fn f<S>(s: S)
where
S: Send, /* */
//
{
}

fn main() {
foo(
x, // comment 1
// comment 2
)
}

fn main() {
foo(
x, // comment 1
// comment 2
)
}

fn main() {
foo(
x, /* Comment 1 */
/* Comment 2 */
)
}

fn main() {
foo(
x, /* Comment 1 */
/* Comment 2 */
)
}

fn main() {
foo(
x, /* comment 1 */
/* comment 2 */
)
}
4 changes: 4 additions & 0 deletions tests/target/issue-4714.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
struct Something {
field: u32, /*a*/
/*b*/
}

0 comments on commit 74768b7

Please sign in to comment.