Skip to content

Commit

Permalink
Fix failure with => in comment after match => (#6092)
Browse files Browse the repository at this point in the history
* Find arrow with find_last_uncommented
* Add version gate for arrow finding fix
  • Loading branch information
ding-young authored Mar 12, 2024
1 parent 73c8149 commit 9580747
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::iter::repeat;
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::{
Expand Down Expand Up @@ -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() {
Expand Down
10 changes: 10 additions & 0 deletions tests/source/arrow_in_comments/arrow_in_single_comment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// rustfmt-version: Two
fn main() {
match a {
_ =>
// comment with =>
{
println!("A")
}
}
}
14 changes: 14 additions & 0 deletions tests/source/arrow_in_comments/multiple_arrows.rs
Original file line number Diff line number Diff line change
@@ -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"); }
}
}
}
10 changes: 10 additions & 0 deletions tests/target/arrow_in_comments/arrow_in_single_comment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// rustfmt-version: Two
fn main() {
match a {
_ =>
// comment with =>
{
println!("A")
}
}
}
19 changes: 19 additions & 0 deletions tests/target/arrow_in_comments/multiple_arrows.rs
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
}
}

0 comments on commit 9580747

Please sign in to comment.