Detect when `?` is being called on a type *after* `.unwrap` or `.expect` was called on a `Result` or `Option`
Open
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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