Skip to content

Rustc suggests wrong solution to if assignment #75814

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

Closed
NotWearingPants opened this issue Aug 22, 2020 · 1 comment
Closed

Rustc suggests wrong solution to if assignment #75814

NotWearingPants opened this issue Aug 22, 2020 · 1 comment
Labels
C-bug Category: This is a bug.

Comments

@NotWearingPants
Copy link
Contributor

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
@NotWearingPants NotWearingPants added the C-bug Category: This is a bug. label Aug 22, 2020
@estebank
Copy link
Contributor

Thank you for the report! I agree that this should be fixed, but it is a duplicate of my oldest still open ticket: #44990 🙂. As you already mention it is not that easy to detect and will require us to jump through some hoops to fix it, but we will (eventually).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

2 participants