rust-lang / rust-lang/rust

Detect when `?` is being called on a type *after* `.unwrap` or `.expect` was called on a `Result` or `Option`

Open
#127,345 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics D-lack-of-suggestion D-newcomer-roadblock D-terse P-low T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

The following code

fn foo() -> Option<usize> {
    let x = Some(42).unwrap()?;
    x
}

currently emits

error[E0277]: the `?` operator can only be applied to values that implement `Try`
 --> src/lib.rs:2:13
  |
2 |     let x = Some(42).unwrap()?;
  |             ^^^^^^^^^^^^^^^^^^ the `?` operator cannot be applied to type `{integer}`
  |
  = help: the trait `Try` is not implemented for `{integer}`

but it should suggest removing the .unwrap().

Inspired by https://x.com/jonasfovea/status/1808961128812183774

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 issue with the Rust snippet in the report and locate the compiler diagnostic for applying ? to the result of .unwrap() or .expect(). Done means the diagnostic suggests removing the unnecessary method call for Result and Option cases, with coverage for the reported example.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.