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

Trailing comma on match block goes missing when guard is on its own line #4998

Merged

Conversation

ulyssa
Copy link
Contributor

@ulyssa ulyssa commented Sep 21, 2021

After making some changes to a guard on a match arm, I noticed that rustfmt was removing a comma from the end of the block even though I had match_block_trailing_comma set to true. Here's an example program where you can see the behaviour:

fn main() {
    let val = Some(5);

    match val {
        Some(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ) if abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ < 1000 => {
            println!("a");
        },
        Some(a) if a > 1000 => {
            println!("b");
        },
        Some(_) => {
            println!("c");
        },
        None => {
            println!("d");
        },
    }
}

Running rustfmt --emit stdout --config match_block_trailing_comma=true test.rs on this file, you can see that all branches but the one where the guard needed to be wrapped keep the comma:

fn main() {
    let val = Some(5);

    match val {
        Some(abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ)
            if abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ < 1000 =>
        {
            println!("a");
        }
        Some(a) if a > 1000 => {
            println!("b");
        },
        Some(_) => {
            println!("c");
        },
        None => {
            println!("d");
        },
    }
}

Copy link
Member

@calebcartwright calebcartwright left a comment

Choose a reason for hiding this comment

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

Thanks so much for this! Was quite literally in the process of preparing the next release which will include the stabilization of this option, so thrilled to get another bug fixed preemptively.

@calebcartwright calebcartwright merged commit dd445ab into rust-lang:master Sep 21, 2021
@ulyssa ulyssa deleted the guarded-block-trailing-comma branch September 21, 2021 01:45
Emoun added a commit to Emoun/duplicate that referenced this pull request Dec 26, 2021
The updated nightly version fixes a bug with the
'match_block_trailing_comma' rustfmt config.
Previously not all missing commas were caught.
Probably related to rust-lang/rustfmt#4998.

When installing cargo-expand, we now use '--locked' to ensure
lock-files are used.
This is a workaround for the release of v4.7.0 of the 'syntect' crates,
which violated semantic versioning. Using the lock files avoids this
version. For details see sharkdp/bat#1991.
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.

2 participants