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

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

Open
nw-github opened this issue Jun 17, 2023 · 4 comments · May be fixed by #5793
Open

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

nw-github opened this issue Jun 17, 2023 · 4 comments · May be fixed by #5793
Labels
a-comments a-matches match arms, patterns, blocks, etc bug Panic, non-idempotency, invalid code, etc.

Comments

@nw-github
Copy link

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
@ytmimi
Copy link
Contributor

ytmimi commented Jun 17, 2023

@nw-github thanks for reaching out!

Confirming that I'm able to reproduce the issue with rustfmt 1.5.1-stable (90743e72 2023-01-10)

This seems similar to #5425, but happening in a different context so it may be totally unique

@ytmimi ytmimi added bug Panic, non-idempotency, invalid code, etc. a-comments a-matches match arms, patterns, blocks, etc labels Jun 17, 2023
@calebcartwright
Copy link
Member

@fee1-dead - would you be up for working on this one when you have a chance?

@fee1-dead
Copy link
Member

Yep. I will look into this when I have time

@fee1-dead
Copy link
Member

This reliably triggers for all comma-separated lists:

input:

struct Struct {
    field1: String
    // 1
    ,
    // 2
    filed2: String
    // 3
    ,
    // 4
}

pub fn testing(
    a: i32
    // 5
    ,
    // 6
    b: u32
    // 7
    ,
    // 8
) {
    testing(
        1
        // 9
        ,
        // 10
        2
        // 11
        ,
        // 12
    )
}

Out:

struct Struct {
    field1: String, // 1
                    ,
                    // 2
    filed2: String, // 3
                    ,
                    // 4
}

pub fn testing(
    a: i32, // 5
            ,
            // 6
    b: u32, // 7
            ,
            // 8
) {
    testing(
        1, // 9
           ,
           // 10
        2, // 11
          ,
          // 12
    )
}

Block comments, however, only seem to trigger for the last item according to the comment in the linked issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-comments a-matches match arms, patterns, blocks, etc bug Panic, non-idempotency, invalid code, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants