Skip to content

type annotations needed for if let Some(whatever) = todo!() { not very helpful #141742

Open
@matthiaskrgr

Description

@matthiaskrgr

Code

fn main() {
    if let Some(whatever) = todo!() {
        todo!();
    } else {
        todo!();
    }
}

Current output

error[E0282]: type annotations needed
 --> src/main.rs:2:17
  |
2 |     if let Some(whatever) = todo!() {
  |                 ^^^^^^^^ cannot infer type

For more information about this error, try `rustc --explain E0282`.

It's not really clear how rustc would like to see the type annotations, without adding an extra binding and without type_ascription

What does not work:

if let Some(whatever): Option<i32> = todo!() {
or
if let Some(whatever: i32) = todo!() {

what did end up working:

if let Some(x) = Some(todo!()) {
or
if let Some(x @ ()) = todo!() {

zulip thread: https://rust-lang.zulipchat.com/#narrow/channel/147480-t-compiler.2Fdiagnostics/topic/type.20annotations.20needed.20.60if.20let.20Some.28x.29.20.3D.20todo!.28.29.60/with/521126024

Rust Version

rustc 1.89.0-nightly (6f6971078 2025-05-28)
binary: rustc
commit-hash: 6f69710780d579b180ab38da4c1384d630f7bd31
commit-date: 2025-05-28
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceA-patternsRelating to patterns and pattern matchingD-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.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