Fix failure with => in comment after match => (#6092)

* Find arrow with find_last_uncommented
* Add version gate for arrow finding fix
This commit is contained in:
ding-young 2024-03-12 11:26:28 +09:00 committed by GitHub
parent 73c81495cd
commit 9580747a76
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 59 additions and 2 deletions

View File

@ -5,7 +5,7 @@
use rustc_ast::{ast, ptr}; use rustc_ast::{ast, ptr};
use rustc_span::{BytePos, Span}; 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::lists::*;
use crate::config::{Config, ControlBraceStyle, IndentStyle, MatchArmLeadingPipe, Version}; use crate::config::{Config, ControlBraceStyle, IndentStyle, MatchArmLeadingPipe, Version};
use crate::expr::{ use crate::expr::{
@ -402,7 +402,11 @@ fn rewrite_match_body(
let arrow_snippet = context.snippet(arrow_span).trim(); 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 // search for the arrow starting from the end of the snippet since there may be a match
// expression within the guard // 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 = `=>` // 2 = `=>`
let comment_str = arrow_snippet[arrow_index + 2..].trim(); let comment_str = arrow_snippet[arrow_index + 2..].trim();
if comment_str.is_empty() { if comment_str.is_empty() {

View File

@ -0,0 +1,10 @@
// rustfmt-version: Two
fn main() {
match a {
_ =>
// comment with =>
{
println!("A")
}
}
}

View File

@ -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"); }
}
}
}

View File

@ -0,0 +1,10 @@
// rustfmt-version: Two
fn main() {
match a {
_ =>
// comment with =>
{
println!("A")
}
}
}

View File

@ -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");
}
}
}
}
}