Skip to content

Confusing error when accidentally inserting - in some lines #87291

Closed

Description

@elichai

Given the following code:

fn do_something(opt1: Option<()>, opt2: Option<()>) {
-   let a = opt1.map(|_| {
        if let Some(_) = opt {
    
        }
    });
}

https://play.rust-lang.org/?gist=710a6e61334a9cff89667599564d0a42

The current output is:

   Compiling playground v0.0.1 (/playground)
error[E0425]: cannot find value `opt` in this scope
 --> src/lib.rs:6:26
  |
6 |         if let Some(_) = opt {
  |                          ^^^ help: a local variable with a similar name exists: `opt1`

error[E0658]: `let` expressions in this position are experimental
 --> src/lib.rs:5:5
  |
5 |   -   let a = opt1.map(|_| {
  |  _____^
6 | |         if let Some(_) = opt {
7 | |     
8 | |         }
9 | |     });
  | |______^
  |
  = note: see issue #53667 <https://github.com/rust-lang/rust/issues/53667> for more information
  = help: you can write `matches!(<expr>, <pattern>)` instead of `let <pattern> = <expr>`

error: expected expression, found statement (`let`)
 --> src/lib.rs:5:5
  |
5 |   -   let a = opt1.map(|_| {
  |  _____^
6 | |         if let Some(_) = opt {
7 | |     
8 | |         }
9 | |     });
  | |______^
  |
  = note: variable declaration using `let` is a statement

error[E0600]: cannot apply unary operator `-` to type `bool`
 --> src/lib.rs:5:1
  |
5 | / -   let a = opt1.map(|_| {
6 | |         if let Some(_) = opt {
7 | |     
8 | |         }
9 | |     });
  | |______^ cannot apply unary operator `-`

error: aborting due to 4 previous errors

The last error is the closest one to the actual error.

I know this looks quite contrived but it actually happened to my twice today (new keyboard haha)

Thanks!

Activity

  1. added
    A-diagnostics
    T-compiler
    on Jul 19, 2021
  2. added
    A-parser
    D-papercut
    D-verbose
    on Jul 22, 2021
  3. estebank commented on Dec 28, 2022

    @estebank
    Contributor

    Triage: no change.

  4. estebank commented on Dec 5, 2024

    @estebank
    Contributor

    Current output:

    error: expected expression, found `let` statement
     --> src/lib.rs:5:5
      |
    5 | -   let a = opt1.map(|_| {
      |     ^^^
      |
      = note: only supported directly in conditions of `if` and `while` expressions
    
    error[E0425]: cannot find value `opt` in this scope
     --> src/lib.rs:6:26
      |
    6 |         if let Some(_) = opt {
      |                          ^^^ help: a local variable with a similar name exists: `opt1`
    
  5. added 6 commits that reference this issue on Oct 29, 2026
    32738a1
    ab1696d
    c5452ce
    3812ace
    0f8b90c
    0f53962
  6. added a commit that references this issue on Oct 31, 2026
    1b090c9
  7. added a commit that references this issue on Nov 2, 2026
    c5eec73
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsA-parserD-papercutD-verboseT-compiler

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions