Skip to content

Commit efca09b

Browse files
committed
Fix failure with => in comment after match =>
1 parent b5dcc6f commit efca09b

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

src/matches.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,16 @@ fn rewrite_match_body(
402402
let arrow_snippet = context.snippet(arrow_span).trim();
403403
// search for the arrow starting from the end of the snippet since there may be a match
404404
// expression within the guard
405-
let arrow_index = arrow_snippet.rfind("=>").unwrap();
405+
let mut arrow_index = arrow_snippet.rfind("=>").unwrap();
406+
// check whether `=>` is included in the comment
407+
if arrow_index != 0 {
408+
let prev_arrow = arrow_snippet[..arrow_index].trim();
409+
let single_line_comment_index = prev_arrow.rfind("//").unwrap_or(0);
410+
let new_line_index = prev_arrow.rfind("\n").unwrap_or(0);
411+
if single_line_comment_index > new_line_index {
412+
arrow_index = 0;
413+
}
414+
}
406415
// 2 = `=>`
407416
let comment_str = arrow_snippet[arrow_index + 2..].trim();
408417
if comment_str.is_empty() {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rustfmt-wrap_comments: true
2+
fn main() {
3+
match a {
4+
_ =>
5+
// comment with =>
6+
{
7+
println!("A")
8+
}
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// rustfmt-wrap_comments: true
2+
fn main() {
3+
match a {
4+
_ =>
5+
// comment with =>
6+
{
7+
println!("A")
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)