Skip to content

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

Merged
ytmimi merged 4 commits intorust-lang:masterfrom
ding-young:comment-escape-arrow
Mar 12, 2024
Merged

Fix failure with => in comment after match =>#6092
ytmimi merged 4 commits intorust-lang:masterfrom
ding-young:comment-escape-arrow

Conversation

@ding-young
Copy link
Contributor

Fixes #5998

Background

(1) Comment without arrow =>

fn main() {
    match a {
        _ =>
        // comment with ==
                {
            println!("A")
        }
    }
}

(2) Comment without arrow =>

fn main() {
    match a {
        _ =>
        // comment with =>
                {
            println!("A")
        }
    }
}

Previously, second example fails to reformat unlike the first example. The cause was rustfmt returns original code snippet when comment is lost after rewrite. When rewrite_match_body, there's logic to check if comment exists between => and match body, but it tries to find another => in arrow_snippet considering the case with nested match. Since => in comment was also caught with rfind(), the comment was lost, which led to silent failure.

Solution

Add extra check whether found => was included in single line comment.

TODO

  • add more test case and verify
  • think for any other alternative approach

@ding-young ding-young marked this pull request as ready for review March 3, 2024 12:36
@ding-young ding-young changed the title [Draft] Fix failure with => in comment after match => Fix failure with => in comment after match => Mar 3, 2024
@ding-young ding-young force-pushed the comment-escape-arrow branch from ce0e40c to 0b73d9e Compare March 4, 2024 03:43
@ding-young ding-young force-pushed the comment-escape-arrow branch from f62ea61 to 234fe10 Compare March 7, 2024 05:26
Copy link
Contributor

@ytmimi ytmimi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just reviewed this again, and everything works well! Thanks again for taking the time to work on this one. Cheers on your first contribution to rustfmt 🎉

@ytmimi ytmimi merged commit 9580747 into rust-lang:master Mar 12, 2024
@ytmimi ytmimi added release-notes Needs an associated changelog entry and removed pr-follow-up-review-pending labels Mar 12, 2024
yuvraj-wale pushed a commit to yuvraj-wale/rustfmt that referenced this pull request Mar 28, 2024
* Find arrow with find_last_uncommented
* Add version gate for arrow finding fix
@ytmimi ytmimi removed the release-notes Needs an associated changelog entry label Jul 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rustfmt stops working if the comment just after the => of a "match" contains "=>"

3 participants