Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Mar 12, 2024

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
src/matches.rs Outdated Show resolved Hide resolved
src/matches.rs Outdated Show resolved Hide resolved
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
27 checks passed
@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