Skip to content

Commit 606894e

Browse files
ytmimicalebcartwright
authored andcommitted
Retain trailing separator when extracting the last inline post comment
Fixes 5042 Previously, trailing commas were removed from the last inline comment. This lead to rustfmt refusing to format code snippets because the original comment did not match the rewritten comment. Now, when rustfmt extracts the last inline comment it leaves trailing separators alone. Rustfmt does not need to remove these separators because they are commented out.
1 parent 368a9b7 commit 606894e

9 files changed

+149
-7
lines changed

src/lists.rs

+20-7
Original file line numberDiff line numberDiff line change
@@ -611,15 +611,30 @@ pub(crate) fn extract_post_comment(
611611
post_snippet: &str,
612612
comment_end: usize,
613613
separator: &str,
614+
is_last: bool,
614615
) -> Option<String> {
615616
let white_space: &[_] = &[' ', '\t'];
616617

617618
// Cleanup post-comment: strip separators and whitespace.
618619
let post_snippet = post_snippet[..comment_end].trim();
620+
621+
let last_inline_comment_ends_with_separator = if is_last {
622+
if let Some(line) = post_snippet.lines().last() {
623+
line.ends_with(separator) && line.trim().starts_with("//")
624+
} else {
625+
false
626+
}
627+
} else {
628+
false
629+
};
630+
619631
let post_snippet_trimmed = if post_snippet.starts_with(|c| c == ',' || c == ':') {
620632
post_snippet[1..].trim_matches(white_space)
621633
} else if let Some(stripped) = post_snippet.strip_prefix(separator) {
622634
stripped.trim_matches(white_space)
635+
} else if last_inline_comment_ends_with_separator {
636+
// since we're on the last item it's fine to keep any trailing separators in comments
637+
post_snippet.trim_matches(white_space)
623638
}
624639
// not comment or over two lines
625640
else if post_snippet.ends_with(',')
@@ -748,14 +763,12 @@ where
748763
.snippet_provider
749764
.span_to_snippet(mk_sp((self.get_hi)(&item), next_start))
750765
.unwrap_or("");
751-
let comment_end = get_comment_end(
752-
post_snippet,
753-
self.separator,
754-
self.terminator,
755-
self.inner.peek().is_none(),
756-
);
766+
let is_last = self.inner.peek().is_none();
767+
let comment_end =
768+
get_comment_end(post_snippet, self.separator, self.terminator, is_last);
757769
let new_lines = has_extra_newline(post_snippet, comment_end);
758-
let post_comment = extract_post_comment(post_snippet, comment_end, self.separator);
770+
let post_comment =
771+
extract_post_comment(post_snippet, comment_end, self.separator, is_last);
759772

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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
fn main() {
2+
// 5042 deals with trailing commas, not the indentation issue of these comments
3+
// When a future PR fixes the inentation issues these test can be updated
4+
let _ = std::ops::Add::add(10, 20
5+
// ...
6+
// ...,
7+
);
8+
9+
let _ = std::ops::Add::add(10, 20
10+
/* ... */
11+
// ...,
12+
);
13+
14+
let _ = std::ops::Add::add(10, 20
15+
// ...,
16+
// ...,
17+
);
18+
19+
let _ = std::ops::Add::add(10, 20
20+
// ...,
21+
/* ...
22+
*/,
23+
);
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
fn main() {
2+
// 5042 deals with trailing commas, not the indentation issue of these comments
3+
// When a future PR fixes the inentation issues these test can be updated
4+
let _ = std::ops::Add::add(10, 20
5+
// ...
6+
// ...
7+
);
8+
9+
let _ = std::ops::Add::add(10, 20
10+
/* ... */
11+
// ...
12+
);
13+
14+
let _ = std::ops::Add::add(10, 20
15+
// ...
16+
// ...
17+
);
18+
19+
let _ = std::ops::Add::add(10, 20
20+
// ...
21+
/* ...
22+
*/
23+
);
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn main() {
2+
let _ = std::ops::Add::add(10, 20
3+
// ...,
4+
);
5+
6+
let _ = std::ops::Add::add(10, 20
7+
/* ... */,
8+
);
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fn main() {
2+
let _ = std::ops::Add::add(10, 20
3+
// ...
4+
);
5+
6+
let _ = std::ops::Add::add(10, 20
7+
/* ... */
8+
);
9+
}
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
fn main() {
2+
// 5042 deals with trailing commas, not the indentation issue of these comments
3+
// When a future PR fixes the inentation issues these test can be updated
4+
let _ = std::ops::Add::add(
5+
10, 20, // ...
6+
// ...,
7+
);
8+
9+
let _ = std::ops::Add::add(
10+
10, 20, /* ... */
11+
// ...,
12+
);
13+
14+
let _ = std::ops::Add::add(
15+
10, 20, // ...,
16+
// ...,
17+
);
18+
19+
let _ = std::ops::Add::add(
20+
10, 20, // ...,
21+
/* ...
22+
*/
23+
);
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
fn main() {
2+
// 5042 deals with trailing commas, not the indentation issue of these comments
3+
// When a future PR fixes the inentation issues these test can be updated
4+
let _ = std::ops::Add::add(
5+
10, 20, // ...
6+
// ...
7+
);
8+
9+
let _ = std::ops::Add::add(
10+
10, 20, /* ... */
11+
// ...
12+
);
13+
14+
let _ = std::ops::Add::add(
15+
10, 20, // ...
16+
// ...
17+
);
18+
19+
let _ = std::ops::Add::add(
20+
10, 20, // ...
21+
/* ...
22+
*/
23+
);
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
let _ = std::ops::Add::add(
3+
10, 20, // ...,
4+
);
5+
6+
let _ = std::ops::Add::add(10, 20 /* ... */);
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
let _ = std::ops::Add::add(
3+
10, 20, // ...
4+
);
5+
6+
let _ = std::ops::Add::add(10, 20 /* ... */);
7+
}

0 commit comments

Comments
 (0)