Skip to content

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

Closed
@Havvy

Description

@Havvy
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions