Skip to content

Invalid ? suggestion on mismatched Ok(T) #116967

Closed
@eopb

Description

@eopb

Code

pub fn foo() -> Result<String, ()> {
    let out: Result<(), ()> = Ok(());
    out
}

Current output

Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
 --> src/lib.rs:3:5
  |
1 | pub fn foo() -> Result<String, ()> {
  |                 ------------------ expected `Result<String, ()>` because of return type
2 |     let out: Result<(), ()> = Ok(());
3 |     out
  |     ^^^ expected `Result<String, ()>`, found `Result<(), ()>`
  |
  = note: expected enum `Result<String, _>`
             found enum `Result<(), _>`
help: use `?` to coerce and return an appropriate `Err`, and wrap the resulting value in `Ok` so the expression remains of type `Result`
  |
3 |     Ok(out?)
  |     +++   ++

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (lib) due to previous error

Desired output

Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
 --> src/lib.rs:3:5
  |
1 | pub fn foo() -> Result<String, ()> {
  |                 ------------------ expected `Result<String, ()>` because of return type
2 |     let out: Result<(), ()> = Ok(());
3 |     out
  |     ^^^ expected `Result<String, ()>`, found `Result<(), ()>`
  |
  = note: expected enum `Result<String, _>`
             found enum `Result<(), _>`
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` (lib) due to previous error

Rationale and extra context

The suggestion is invalid

Other cases

No response

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-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