Skip to content

Provide better compiler output when using ? on Option in fn returning Result or vice-versa? #71089

Closed
@alvinhochun

Description

@alvinhochun

It seems that rustc should provide a better error message or hint when one tries to use the ? operator on an Option in a function returning a Result or vice-versa.

Currently this is the error you get:

error[E0277]: `?` couldn't convert the error to `MyErrorType`
  --> src/main.rs:10:19
   |
10 |     let a = foo(i)?;
   |                   ^ the trait `std::convert::From<std::option::NoneError>` is not implemented for `MyErrorType`
   |
   = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
   = note: required by `std::convert::From::from`

However, it would be better to suggest the use of Option::ok_or or Option::ok_or_else, or Result::ok, instead of referencing something dependent on the try_trait feature (NoneError).

This issue has been assigned to @Duddino via this comment.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.F-on_unimplementedError messages that can be tackled with `#[rustc_on_unimplemented]`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