From b187855c87b7fe2b5c6a55b485b0db3ef4c195bd Mon Sep 17 00:00:00 2001 From: ding-young Date: Sat, 24 Feb 2024 21:08:21 +0900 Subject: [PATCH 1/4] Fix failure with `=>` in comment after match `=>` --- src/matches.rs | 11 ++++++++++- .../arrow_in_comments/arrow_in_single_comment.rs | 10 ++++++++++ .../arrow_in_comments/arrow_in_single_comment.rs | 10 ++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 tests/source/arrow_in_comments/arrow_in_single_comment.rs create mode 100644 tests/target/arrow_in_comments/arrow_in_single_comment.rs diff --git a/src/matches.rs b/src/matches.rs index 5e36d9e857d..478a76e5d11 100644 --- a/src/matches.rs +++ b/src/matches.rs @@ -402,7 +402,16 @@ 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 mut arrow_index = arrow_snippet.rfind("=>").unwrap(); + // check whether `=>` is included in the comment + if arrow_index != 0 { + let prev_arrow = arrow_snippet[..arrow_index].trim(); + let single_line_comment_index = prev_arrow.rfind("//").unwrap_or(0); + let new_line_index = prev_arrow.rfind("\n").unwrap_or(0); + if single_line_comment_index > new_line_index { + arrow_index = 0; + } + } // 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..c4ae0634832 --- /dev/null +++ b/tests/source/arrow_in_comments/arrow_in_single_comment.rs @@ -0,0 +1,10 @@ +// rustfmt-wrap_comments: true +fn main() { + match a { + _ => + // comment with => + { + println!("A") + } + } +} 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..a46370c08e8 --- /dev/null +++ b/tests/target/arrow_in_comments/arrow_in_single_comment.rs @@ -0,0 +1,10 @@ +// rustfmt-wrap_comments: true +fn main() { + match a { + _ => + // comment with => + { + println!("A") + } + } +} From 284f2e351b8b5d6b77d86192c3724b9aa7bab06c Mon Sep 17 00:00:00 2001 From: ding-young Date: Sun, 3 Mar 2024 20:56:17 +0900 Subject: [PATCH 2/4] Add test case --- .../arrow_in_comments/multiple_arrows.rs | 14 ++++++++++++++ .../arrow_in_comments/multiple_arrows.rs | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 tests/source/arrow_in_comments/multiple_arrows.rs create mode 100644 tests/target/arrow_in_comments/multiple_arrows.rs 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..b827a20803c --- /dev/null +++ b/tests/source/arrow_in_comments/multiple_arrows.rs @@ -0,0 +1,14 @@ +// rustfmt-wrap_comments: true +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/multiple_arrows.rs b/tests/target/arrow_in_comments/multiple_arrows.rs new file mode 100644 index 00000000000..8a2b5061f72 --- /dev/null +++ b/tests/target/arrow_in_comments/multiple_arrows.rs @@ -0,0 +1,19 @@ +// rustfmt-wrap_comments: true +fn main() { + match a { + _ => + // comment with => + { + match b { + // one goes to => + one => { + println("1"); + } + // two goes to => + two => { + println("2"); + } + } + } + } +} From 234fe1048c80d9bb424d34ac659f37c2935a262e Mon Sep 17 00:00:00 2001 From: ding-young Date: Thu, 7 Mar 2024 14:23:36 +0900 Subject: [PATCH 3/4] Find arrow with find_last_uncommented --- src/matches.rs | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/matches.rs b/src/matches.rs index 478a76e5d11..9fd07cc1bf4 100644 --- a/src/matches.rs +++ b/src/matches.rs @@ -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::{ @@ -402,16 +402,7 @@ 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 mut arrow_index = arrow_snippet.rfind("=>").unwrap(); - // check whether `=>` is included in the comment - if arrow_index != 0 { - let prev_arrow = arrow_snippet[..arrow_index].trim(); - let single_line_comment_index = prev_arrow.rfind("//").unwrap_or(0); - let new_line_index = prev_arrow.rfind("\n").unwrap_or(0); - if single_line_comment_index > new_line_index { - arrow_index = 0; - } - } + let arrow_index = arrow_snippet.find_last_uncommented("=>").unwrap(); // 2 = `=>` let comment_str = arrow_snippet[arrow_index + 2..].trim(); if comment_str.is_empty() { From b33aa37353730530bf8131f1b600645c910ef564 Mon Sep 17 00:00:00 2001 From: ding-young Date: Sat, 9 Mar 2024 18:26:35 +0900 Subject: [PATCH 4/4] Add version gate for arrow finding fix --- src/matches.rs | 6 +++++- tests/source/arrow_in_comments/arrow_in_single_comment.rs | 2 +- tests/source/arrow_in_comments/multiple_arrows.rs | 2 +- tests/target/arrow_in_comments/arrow_in_single_comment.rs | 2 +- tests/target/arrow_in_comments/multiple_arrows.rs | 2 +- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/matches.rs b/src/matches.rs index 9fd07cc1bf4..06e7078b11f 100644 --- a/src/matches.rs +++ b/src/matches.rs @@ -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.find_last_uncommented("=>").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 index c4ae0634832..fb0576a4822 100644 --- a/tests/source/arrow_in_comments/arrow_in_single_comment.rs +++ b/tests/source/arrow_in_comments/arrow_in_single_comment.rs @@ -1,4 +1,4 @@ -// rustfmt-wrap_comments: true +// rustfmt-version: Two fn main() { match a { _ => diff --git a/tests/source/arrow_in_comments/multiple_arrows.rs b/tests/source/arrow_in_comments/multiple_arrows.rs index b827a20803c..fc696b309f2 100644 --- a/tests/source/arrow_in_comments/multiple_arrows.rs +++ b/tests/source/arrow_in_comments/multiple_arrows.rs @@ -1,4 +1,4 @@ -// rustfmt-wrap_comments: true +// rustfmt-version: Two fn main() { match a { _ => // comment with => diff --git a/tests/target/arrow_in_comments/arrow_in_single_comment.rs b/tests/target/arrow_in_comments/arrow_in_single_comment.rs index a46370c08e8..deffdbeeaaf 100644 --- a/tests/target/arrow_in_comments/arrow_in_single_comment.rs +++ b/tests/target/arrow_in_comments/arrow_in_single_comment.rs @@ -1,4 +1,4 @@ -// rustfmt-wrap_comments: true +// rustfmt-version: Two fn main() { match a { _ => diff --git a/tests/target/arrow_in_comments/multiple_arrows.rs b/tests/target/arrow_in_comments/multiple_arrows.rs index 8a2b5061f72..b0443411816 100644 --- a/tests/target/arrow_in_comments/multiple_arrows.rs +++ b/tests/target/arrow_in_comments/multiple_arrows.rs @@ -1,4 +1,4 @@ -// rustfmt-wrap_comments: true +// rustfmt-version: Two fn main() { match a { _ =>