Skip to content

Confusing errors with missing let in if let #77238

Closed
@camelid

Description

@camelid

I thought this code would provide one of the nice suggestions that @estebank implemented in #75931, but it doesn't. (By the way, making cache a reference to Cache will give an ICE because of #77218.)

pub struct Cache {
    data: Vec<i32>,
}

pub fn list_data(cache: Cache, key: &usize) {
    for reference in vec![1, 2, 3] {
        if /*let*/ Some(reference) = cache.data.get(*key) {
            unimplemented!()
        }
    }
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
 --> src/lib.rs:7:38
  |
7 |         if /*let*/ Some(reference) = cache.data.get(*key) {
  |                                      ^^^^^^^^^^^^^^^^^^^^ expected integer, found `&i32`
  |
  = note: expected enum `Option<{integer}>`
             found enum `Option<&i32>`

error[E0308]: mismatched types
 --> src/lib.rs:7:20
  |
7 |         if /*let*/ Some(reference) = cache.data.get(*key) {
  |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `bool`, found `()`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground`

To learn more, run the command again with --verbose.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-type-systemArea: Type systemD-confusingDiagnostics: Confusing error or lint that should be reworked.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions