Skip to content

Formatting match arm with a comment between the expression and the comma causes invalid code #5784

Open
@nw-github

Description

@nw-github

Example code (playground link)

fn main() {
    match 100 {
        0 => println!("its zero")
            // eprintln!("its zero")
        ,
        _ => (),
    }
}

After format with rustfmt 1.5.2-stable (90c5418 2023-05-31):

fn main() {
    match 100 {
        0 => println!("its zero"),
        // eprintln!("its zero")
        ,
        _ => (),
    }
}

A comma is inserted after the println! call, causing this syntax error:

   Compiling playground v0.0.1 (/playground)
error: expected pattern, found `,`
 --> src/main.rs:5:9
  |
5 |         ,
  |         ^ expected pattern

error: could not compile `playground` (bin "playground") due to previous error

Metadata

Metadata

Assignees

No one assigned

    Labels

    a-commentsa-matchesmatch arms, patterns, blocks, etcbugPanic, non-idempotency, invalid code, etc.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions