Skip to content

Fixed trailing comment being moved when comma is inserted #4655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: rustfmt-2.0.0-rc.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/formatting/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,23 +632,28 @@ pub(crate) fn extract_post_comment(
post_snippet: &str,
comment_end: usize,
separator: &str,
leave_last: bool,
) -> Option<String> {
let white_space: &[_] = &[' ', '\t'];

// Cleanup post-comment: strip separators and whitespace.
let post_snippet = post_snippet[..comment_end].trim();
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) {
let (post_snippet, comment_end) = post_snippet.split_at(comment_end);
let post_snippet_trimmed = post_snippet.trim();

let post_snippet_trimmed = if post_snippet_trimmed.starts_with(|c| c == ',' || c == ':') {
post_snippet_trimmed[1..].trim_matches(white_space)
} else if let Some(post_snippet) = post_snippet_trimmed.strip_prefix(separator) {
post_snippet.trim_matches(white_space)
}
// not comment or over two lines
else if post_snippet.ends_with(',')
&& (!post_snippet.trim().starts_with("//") || post_snippet.trim().contains('\n'))
else if post_snippet_trimmed.ends_with(',')
&& (!post_snippet_trimmed.starts_with("//") || post_snippet_trimmed.contains('\n'))
{
post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space)
post_snippet_trimmed[..(post_snippet_trimmed.len() - 1)].trim_matches(white_space)
} else if comment_end == ")" && !leave_last {
post_snippet_trimmed
} else {
post_snippet
post_snippet.trim_start_matches(white_space).trim_end()
};
// FIXME(#3441): post_snippet includes 'const' now
// it should not include here
Expand Down Expand Up @@ -776,7 +781,8 @@ where
self.inner.peek().is_none(),
);
let new_lines = has_extra_newline(post_snippet, comment_end);
let post_comment = extract_post_comment(post_snippet, comment_end, self.separator);
let post_comment =
extract_post_comment(post_snippet, comment_end, self.separator, self.leave_last);

self.prev_span_end = (self.get_hi)(&item) + BytePos(comment_end as u32);

Expand Down
45 changes: 45 additions & 0 deletions tests/source/issue-4654.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
struct Foo {
bar: ()
// Comment
}

struct Bar {
baz: ()
/*
Comment
*/
}

struct Baz(
()
// Comment
);

fn main() {
let _ = Foo {
bar: ()
// Comment
};

let _ = Bar {
baz: ()
/*
Comment
*/
};

let _ = Baz(
()

// Comment
);

match a {
0 => {}
// Foo
1 => {} // Bar
// Baz
2 => {}
// Qux
}
}
3 changes: 2 additions & 1 deletion tests/target/issue-3532.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
fn foo(a: T) {
match a {
1 => {}
0 => {} // _ => panic!("doesn't format!"),
0 => {}
// _ => panic!("doesn't format!"),
}
}
44 changes: 44 additions & 0 deletions tests/target/issue-4654.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
struct Foo {
bar: (),
// Comment
}

struct Bar {
baz: (),
/*
Comment
*/
}

struct Baz(
(),
// Comment
);

fn main() {
let _ = Foo {
bar: (),
// Comment
};

let _ = Bar {
baz: (),
/*
Comment
*/
};

let _ = Baz(
(),
// Comment
);

match a {
0 => {}
// Foo
1 => {} // Bar
// Baz
2 => {}
// Qux
}
}