Skip to content

Rustc suggests wrong solution to if assignment #75814

Closed
@NotWearingPants

Description

@NotWearingPants

I tried to compile this code:

fn main() {
    let value = Some(1);
    if Some(x) = value {
        println!("yes");
    } else {
        println!("no");
    }
}

and the compiler said:

error[E0425]: cannot find value `x` in this scope
 --> src\main.rs:3:13
  |
3 |     if Some(x) = value {
  |             ^ not found in this scope

error[E0308]: mismatched types
 --> src\main.rs:3:8
  |
3 |     if Some(x) = value {
  |        ^^^^^^^^^^^^^^^
  |        |
  |        expected `bool`, found `()`
  |        help: try comparing for equality: `Some(x) == value`

it suggested to change the = to ==, so I did, which fixed the second error but still caused:

error[E0425]: cannot find value `x` in this scope
 --> src\main.rs:3:13
  |
3 |     if Some(x) == value {
  |             ^ not found in this scope

Instead, the help should suggest to add let in front of Some(x), which will fix the issue.

I understand it's not trivial for the compiler to know which will solve the issue, but at least checking if x exists in this scope to suggest the let solution will probably help a lot of people starting out with rust.

Meta

rustc --version --verbose:

rustc 1.44.0 (49cae5576 2020-06-01)
binary: rustc
commit-hash: 49cae55760da0a43428eba73abcb659bb70cf2e4
commit-date: 2020-06-01
host: x86_64-pc-windows-gnu
release: 1.44.0
LLVM version: 9.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions