Skip to content

let-chain with missing let in the middle causes multiple errors #117977

@estebank

Description

@estebank

Code

fn main() { let x = Some(42); if let Some(_) = x && Some(x) = x {} }

Current output

Update:

error: expected expression, found `let` statement --> src/main.rs:3:8 | 3 | if let Some(_) = x | ^^^^^^^^^^^^^^^ | = note: only supported directly in conditions of `if` and `while` expressions help: you might have meant to continue the let-chain | 4 | && let Some(x) = x | +++ help: you might have meant to compare for equality | 4 | && Some(x) == x | + error[E0308]: mismatched types --> src/main.rs:4:12 | 4 | && Some(x) = x | ^^^^^^^ expected `bool`, found `Option<Option<{integer}>>` | = note: expected type `bool` found enum `Option<Option<{integer}>>` help: use `Option::is_some` to test if the `Option` has a value | 4 | && Some(x).is_some() = x | ++++++++++ error[E0308]: mismatched types --> src/main.rs:3:8 | 3 | if let Some(_) = x | ________^ 4 | | && Some(x) = x | |______________________^ expected `bool`, found `()` 

Previously:

error: expected expression, found `let` statement --> src/main.rs:3:8 | 3 | if let Some(_) = x | ^^^^^^^^^^^^^^^ | = note: only supported directly in conditions of `if` and `while` expressions error[E0308]: mismatched types --> src/main.rs:4:12 | 4 | && Some(x) = x | ^^^^^^^ expected `bool`, found `Option<Option<{integer}>>` | = note: expected type `bool` found enum `Option<Option<{integer}>>` help: use `Option::is_some` to test if the `Option` has a value | 4 | && Some(x).is_some() = x | ++++++++++ error[E0308]: mismatched types --> src/main.rs:3:8 | 3 | if let Some(_) = x | ________^ 4 | | && Some(x) = x | |______________________^ expected `bool`, found `()`

Desired output

Edit: we now provide appropriate suggestions in the first diagnostic, but we should notice that this should have been a let chain and silence the E0308 errors.

error: let-chain with missing `let` --> src/main.rs:3:8 | 3 | if let Some(_) = x | --------------- let-chain starting here 4 | && Some(x) = x | ^^^^^^^^^^^ expected `let` expression, found assignment | help: add `let` before the expression | 4 | && let Some(x) = x | +++

Rationale and extra context

No response

Other cases

No response

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-papercutDiagnostics: An error or lint that needs small tweaks.D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.F-let_chains`#![feature(let_chains)]`P-lowLow priorityT-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