Skip to content

Improve error message for statements used as expressions #105431

Closed
@mcoblenz

Description

@mcoblenz

The code below has superfluous semicolons in the branches of the if expression:

fn test() -> i32 {
    let x = if (true) {
        3;
    }
    else {
        4;
    };
    x
}

The error message that results is:

error[E0308]: mismatched types
  --> src/lib.rs:11:5
   |
4  | fn test() -> i32 {
   |              --- expected `i32` because of return type
...
11 |     x
   |     ^ expected `i32`, found `()`

This can be confusing to beginners, who may not understand unit type. However, since the expressions 3 and 4 can be shown to have no side effects, the code (as is) can't be what the user intended. Could the compiler produce a more targeted error message that mentions the superfluous semicolon?

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.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