diff --git a/src/matches.rs b/src/matches.rs index 5e36d9e857d..06e7078b11f 100644 --- a/src/matches.rs +++ b/src/matches.rs @@ -5,7 +5,7 @@ use rustc_ast::{ast, ptr}; use rustc_span::{BytePos, Span}; -use crate::comment::{combine_strs_with_missing_comments, rewrite_comment}; +use crate::comment::{combine_strs_with_missing_comments, rewrite_comment, FindUncommented}; use crate::config::lists::*; use crate::config::{Config, ControlBraceStyle, IndentStyle, MatchArmLeadingPipe, Version}; use crate::expr::{ @@ -402,7 +402,11 @@ fn rewrite_match_body( let arrow_snippet = context.snippet(arrow_span).trim(); // 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(); + let arrow_index = if context.config.version() == Version::One { + arrow_snippet.rfind("=>").unwrap() + } else { + arrow_snippet.find_last_uncommented("=>").unwrap() + }; // 2 = `=>` let comment_str = arrow_snippet[arrow_index + 2..].trim(); if comment_str.is_empty() { diff --git a/tests/source/arrow_in_comments/arrow_in_single_comment.rs b/tests/source/arrow_in_comments/arrow_in_single_comment.rs new file mode 100644 index 00000000000..fb0576a4822 --- /dev/null +++ b/tests/source/arrow_in_comments/arrow_in_single_comment.rs @@ -0,0 +1,10 @@ +// rustfmt-version: Two +fn main() { + match a { + _ => + // comment with => + { + println!("A") + } + } +} diff --git a/tests/source/arrow_in_comments/multiple_arrows.rs b/tests/source/arrow_in_comments/multiple_arrows.rs new file mode 100644 index 00000000000..fc696b309f2 --- /dev/null +++ b/tests/source/arrow_in_comments/multiple_arrows.rs @@ -0,0 +1,14 @@ +// rustfmt-version: Two +fn main() { + match a { + _ => // comment with => + match b { + // one goes to => + one => { + println("1"); + } + // two goes to => + two => { println("2"); } + } + } +} diff --git a/tests/target/arrow_in_comments/arrow_in_single_comment.rs b/tests/target/arrow_in_comments/arrow_in_single_comment.rs new file mode 100644 index 00000000000..deffdbeeaaf --- /dev/null +++ b/tests/target/arrow_in_comments/arrow_in_single_comment.rs @@ -0,0 +1,10 @@ +// rustfmt-version: Two +fn main() { + match a { + _ => + // comment with => + { + println!("A") + } + } +} diff --git a/tests/target/arrow_in_comments/multiple_arrows.rs b/tests/target/arrow_in_comments/multiple_arrows.rs new file mode 100644 index 00000000000..b0443411816 --- /dev/null +++ b/tests/target/arrow_in_comments/multiple_arrows.rs @@ -0,0 +1,19 @@ +// rustfmt-version: Two +fn main() { + match a { + _ => + // comment with => + { + match b { + // one goes to => + one => { + println("1"); + } + // two goes to => + two => { + println("2"); + } + } + } + } +}