Skip to content

Commit f10b225

Browse files
authored
Merge pull request rust-lang#3181 from scampi/issue3131
fix the logic for retaining a comment before the arrow in a match
2 parents e4652fb + 9467033 commit f10b225

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/matches.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,9 @@ fn rewrite_match_body(
379379
// Look for comments between `=>` and the start of the body.
380380
let arrow_comment = {
381381
let arrow_snippet = context.snippet(arrow_span).trim();
382-
let arrow_index = arrow_snippet.find("=>").unwrap();
382+
// search for the arrow starting from the end of the snippet since there may be a match
383+
// expression within the guard
384+
let arrow_index = arrow_snippet.rfind("=>").unwrap();
383385
// 2 = `=>`
384386
let comment_str = arrow_snippet[arrow_index + 2..].trim();
385387
if comment_str.is_empty() {

tests/source/issue-3131.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
match 3 {
3+
t if match t {
4+
_ => true,
5+
} => {},
6+
_ => {}
7+
}
8+
}

tests/target/issue-3131.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
match 3 {
3+
t if match t {
4+
_ => true,
5+
} => {}
6+
_ => {}
7+
}
8+
}

0 commit comments

Comments
 (0)