Skip to content

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

Open
@estebank

Description

@estebank

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.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