Skip to content

Suggest adding a comma when match arm expression is followed by a pattern #80112

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

Closed
Havvy opened this issue Dec 17, 2020 · 1 comment · Fixed by #97823
Closed

Suggest adding a comma when match arm expression is followed by a pattern #80112

Havvy opened this issue Dec 17, 2020 · 1 comment · Fixed by #97823
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@Havvy
Copy link
Contributor

Havvy commented Dec 17, 2020

enum Side { Left, Right }

fn match_side(s: Side) {
    match s {
        Side::Left => ()
        Side::Right => ()
    }
}

Currently shows:

error: expected one of `,`, `.`, `?`, `}`, or an operator, found `Side`
 --> src/lib.rs:6:9
  |
5 |         Side::Left => ()
  |                    --   - expected one of `,`, `.`, `?`, `}`, or an operator
  |                    |
  |                    while parsing the `match` arm starting here
6 |         Side::Right => ()
  |         ^^^^ unexpected token

It should be expanded with a help message to:

5 |          Side::Left => ()
 |                           ^ help: add , here

It should probably only emit this diagnostic if, by rechecking the match with the , there, it can find a pattern followed by =>.


For a more advanced case, consider this more convoluted example:

enum Side { Left(()), Right(()) }

fn match_side(s: Side) {
    match (s,) {
        (Side::Left(_),) => ()
        (Side::Right(_),) => ()
    }
}

It currently emits:

error: expected expression, found reserved identifier `_`
 --> src/lib.rs:6:22
  |
6 |         (Side::Right(_),) => ()
  |                      ^ expected expression

error: expected one of `,`, `.`, `?`, `}`, or an operator, found `=>`
 --> src/lib.rs:6:27
  |
5 |         (Side::Left(_),) => ()
  |                          -- while parsing the `match` arm starting here
6 |         (Side::Right(_),) => ()
  |                           ^^ expected one of `,`, `.`, `?`, `}`, or an operator

In this case, the pattern sort of looks like a function call and not an immediate incorrect token for an expression. The vast majority of cases can probably be caught by checking at the newline if what's after is a pattern. But there might also be a simple solution based on the pattern and expression grammars. Of course, it almost always triggers the found =>, so that could be used as a starting point.

@Havvy Havvy added the A-diagnostics Area: Messages for errors, warnings, and lints label Dec 17, 2020
@Havvy
Copy link
Contributor Author

Havvy commented Dec 17, 2020

If this gets done, it can easily be a quick fix in rust-analyzer, helping increase our helpful IDEs story.

JohnTitor added a commit to JohnTitor/rust that referenced this issue Jun 8, 2022
…-arm, r=estebank

Recover missing comma after match arm

If we're missing a comma after a match arm expression, try parsing another pattern and a following `=>`. If we find both of those, then recover by suggesting to insert a `,`.

Fixes rust-lang#80112
@bors bors closed this as completed in a90c5a3 Jun 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant