Skip to content

Error from function call with incorrect return type outside braces in unit valued match suggests semicolon to be added; suggestion is incorrect #83892

Closed
@the6p4c

Description

@the6p4c

This small snippet

fn func() -> u8 {
    0
}

fn main() {
    match () {
        () => func()
    }
}

suggests adding a semicolon after the function call to rectify the mismatched types.

error[E0308]: mismatched types
 --> src/main.rs:7:15
  |
5 | fn main() {
  |           - expected `()` because of default return type
6 |     match () {
7 |         () => func()
  |               ^^^^^^- help: consider using a semicolon here: `;`
  |               |
  |               expected `()`, found `u8`

error: aborting due to previous error

However, this is also a syntax error (below), and the correct suggestion would be to wrap the function call in braces.

error: `match` arm body without braces
 --> src/main.rs:7:15
  |
7 |         () => func();
  |            -- ^^^^^^- help: use a comma to end a `match` arm expression: `,`
  |            |  |
  |            |  this statement is not surrounded by a body
  |            while parsing the `match` arm starting here

error: aborting due to previous error

Meta

Using rustc nightly 2021-04-04 c755ee4ce8cae6ea977d (playground)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions