Skip to content

Better error message when forgetting the right operand in an if condition #30035

@antoyo

Description

@antoyo

Hello.
The following code gives an error message that could be improved:

fn main() {
    let n = 1;
    if 5 == {
        println!("five");
    }
}

It gives the following error:

src/main.rs:6:1: 6:2 error: expected `{`, found `}`
src/main.rs:6 }
              ^
src/main.rs:6:1: 6:2 help: place this code inside a block

This is because the compiler sees the right operand as a block expression, and is looking for the condition body, something like:

fn main() {
    let n = 1;
    if 5 == {
        println!("five");
        5
    } {
        println!("Five");
    }
}

while we most probably mean to write something like:

fn main() {
    let n = 1;
    if 5 == n {
        println!("five");
    }
}

So, it would be nice to improve this error message.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions