rust-lang / rust-lang/rust

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

Open
#141,742 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-inference A-patterns D-lack-of-suggestion T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the supplied Rust example with the noted nightly version and compare the current E0282 diagnostic with the issue's working alternatives. Trace the compiler diagnostic path that reports “cannot infer type”; done means the error explains how to provide a useful annotation for this pattern without requiring an extra binding.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.