Skip to content

Add suggestion when returning Result<_, A> when Result<_, B> is expected #47560

Closed
@estebank

Description

@estebank

When returning Result<_, A> but Result<_, B> is expected and A: Into<B>, provide a suggestion to return Ok(x?) instead:

error[E0308]: mismatched types
  --> src/bin/ruby-stacktrace.rs:86:20
   |
86 |             return version;
   |                    ^^^^^^^ expected struct `failure::Error`, found enum `ruby_stacktrace::address_finder::AddressFinderError`
   |
   = note: expected type `std::result::Result<_, failure::Error>`
              found type `std::result::Result<_, ruby_stacktrace::address_finder::AddressFinderError>`
   = help: because `ruby_stacktrace::address_finder::AddressFinderError` implements `failure::Error`, you can use `?` to coerce it:
   |
86 |             return Ok(version?);
   |                    ^^^^^^^^^^^^

Case taken from https://jvns.ca/blog/2018/01/13/rust-in-2018--way-easier-to-use/

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.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