Skip to content

Commit 01fdd5d

Browse files
committed
Fixed trailing comment being moved when comma is inserted (fixes #4654)
1 parent d11fde8 commit 01fdd5d

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/formatting/lists.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -632,23 +632,28 @@ pub(crate) fn extract_post_comment(
632632
post_snippet: &str,
633633
comment_end: usize,
634634
separator: &str,
635+
leave_last: bool,
635636
) -> Option<String> {
636637
let white_space: &[_] = &[' ', '\t'];
637638

638639
// Cleanup post-comment: strip separators and whitespace.
639-
let post_snippet = post_snippet[..comment_end].trim();
640-
let post_snippet_trimmed = if post_snippet.starts_with(|c| c == ',' || c == ':') {
641-
post_snippet[1..].trim_matches(white_space)
642-
} else if let Some(post_snippet) = post_snippet.strip_prefix(separator) {
640+
let (post_snippet, comment_end) = post_snippet.split_at(comment_end);
641+
let post_snippet_trimmed = post_snippet.trim();
642+
643+
let post_snippet_trimmed = if post_snippet_trimmed.starts_with(|c| c == ',' || c == ':') {
644+
post_snippet_trimmed[1..].trim_matches(white_space)
645+
} else if let Some(post_snippet) = post_snippet_trimmed.strip_prefix(separator) {
643646
post_snippet.trim_matches(white_space)
644647
}
645648
// not comment or over two lines
646-
else if post_snippet.ends_with(',')
647-
&& (!post_snippet.trim().starts_with("//") || post_snippet.trim().contains('\n'))
649+
else if post_snippet_trimmed.ends_with(',')
650+
&& (!post_snippet_trimmed.starts_with("//") || post_snippet_trimmed.contains('\n'))
648651
{
649-
post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space)
652+
post_snippet_trimmed[..(post_snippet_trimmed.len() - 1)].trim_matches(white_space)
653+
} else if comment_end == ")" && !leave_last {
654+
post_snippet_trimmed
650655
} else {
651-
post_snippet
656+
post_snippet.trim_start_matches(white_space).trim_end()
652657
};
653658
// FIXME(#3441): post_snippet includes 'const' now
654659
// it should not include here
@@ -776,7 +781,8 @@ where
776781
self.inner.peek().is_none(),
777782
);
778783
let new_lines = has_extra_newline(post_snippet, comment_end);
779-
let post_comment = extract_post_comment(post_snippet, comment_end, self.separator);
784+
let post_comment =
785+
extract_post_comment(post_snippet, comment_end, self.separator, self.leave_last);
780786

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

0 commit comments

Comments
 (0)