Skip to content
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

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

Closed
antoyo opened this issue Nov 24, 2015 · 3 comments · Fixed by #48338 or #71433
Closed

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

antoyo opened this issue Nov 24, 2015 · 3 comments · Fixed by #48338 or #71433
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics

Comments

@antoyo
Copy link
Contributor

antoyo commented Nov 24, 2015

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.

@apasel422 apasel422 added the A-diagnostics Area: Messages for errors, warnings, and lints label Nov 24, 2015
@nagisa
Copy link
Member

nagisa commented Nov 24, 2015

This… would be pretty non-trivial to improve. What really bugs me is help being pretty useless, which for I’m going to fill another issue.

@steveklabnik steveklabnik removed the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum Mark-Simulacrum added the A-parser Area: The parsing of Rust source code to an AST label Jun 4, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 24, 2017
@estebank estebank added E-needs-mentor WG-diagnostics Working group: Diagnostics labels Nov 29, 2017
@antoyo
Copy link
Contributor Author

antoyo commented Apr 17, 2018

As of today, I still get the same issue:

error: expected `{`, found `}`
 --> src/main.rs:8:1
  |
6 |     if true == {
  |     -- this `if` statement has a condition, but no block
7 |     }
8 | }
  | ^

@estebank
Copy link
Contributor

FWIW, the originally reported output was:

error: expected `{`, found `}`
 --> src/main.rs:8:1
  |
8 | }
  | ^

@jonas-schievink jonas-schievink added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 22, 2020
@bors bors closed this as completed in be3faf3 Apr 30, 2020
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 A-parser Area: The parsing of Rust source code to an AST C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics
Projects
None yet
8 participants