Skip to content

Noisy error messages for wrong if let #54931

Closed
@leonardo-m

Description

@leonardo-m

This is wrong code, the programmer forgot to add a "let" after the "if":

fn foo(n: Option<u32>) -> u32 {
    if Some(b) = n {
        return b;
    }
    0
}
fn main() {}

rustc 1.31.0-nightly (423d810 2018-10-08) gives:

error[E0425]: cannot find value `b` in this scope
 --> ...\test.rs:2:13
  |
2 |     if Some(b) = n {
  |             ^ did you mean `n`?

error[E0425]: cannot find value `b` in this scope
 --> ...\test.rs:3:16
  |
3 |         return b;
  |                ^ did you mean `n`?

error[E0308]: mismatched types
 --> ...\test.rs:2:8
  |
2 |     if Some(b) = n {
  |        ^^^^^^^^^^^
  |        |
  |        expected bool, found ()
  |        help: try comparing for equality: `Some(b) == n`
  |
  = note: expected type `bool`
             found type `()`

error: aborting due to 3 previous errors

Some errors occurred: E0308, E0425.
For more information about an error, try `rustc --explain E0308`.

Perhaps rustc could give only one smart error message in this situation?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions