From 9467033a8e7f369b5a3d8cc68a51283fd19f4955 Mon Sep 17 00:00:00 2001 From: =?utf8?q?St=C3=A9phane=20Campinas?= Date: Wed, 7 Nov 2018 15:00:33 +0100 Subject: [PATCH] fix the logic for retaining a comment before the arrow in a match --- src/matches.rs | 4 +++- tests/source/issue-3131.rs | 8 ++++++++ tests/target/issue-3131.rs | 8 ++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 tests/source/issue-3131.rs create mode 100644 tests/target/issue-3131.rs diff --git a/src/matches.rs b/src/matches.rs index 442f38b28ea..fc7d4cca020 100644 --- a/src/matches.rs +++ b/src/matches.rs @@ -379,7 +379,9 @@ fn rewrite_match_body( // Look for comments between `=>` and the start of the body. let arrow_comment = { let arrow_snippet = context.snippet(arrow_span).trim(); - let arrow_index = arrow_snippet.find("=>").unwrap(); + // search for the arrow starting from the end of the snippet since there may be a match + // expression within the guard + let arrow_index = arrow_snippet.rfind("=>").unwrap(); // 2 = `=>` let comment_str = arrow_snippet[arrow_index + 2..].trim(); if comment_str.is_empty() { diff --git a/tests/source/issue-3131.rs b/tests/source/issue-3131.rs new file mode 100644 index 00000000000..c4cb2d8c03c --- /dev/null +++ b/tests/source/issue-3131.rs @@ -0,0 +1,8 @@ +fn main() { + match 3 { + t if match t { + _ => true, + } => {}, + _ => {} + } +} diff --git a/tests/target/issue-3131.rs b/tests/target/issue-3131.rs new file mode 100644 index 00000000000..c7304dd5585 --- /dev/null +++ b/tests/target/issue-3131.rs @@ -0,0 +1,8 @@ +fn main() { + match 3 { + t if match t { + _ => true, + } => {} + _ => {} + } +} -- 2.44.0