Skip to content

try wrapping the expression suggests wrapping Result with an extra Err #145294

@Patryk27

Description

@Patryk27

Code

fn foo() -> Result<Option<String>, ()> {
    todo!()
}

fn main() {
    assert_eq!(Ok(Some("hi")), foo());
}

Current output

error[E0308]: mismatched types
 --> src/main.rs:6:32
  |
6 |     assert_eq!(Ok(Some("hi")), foo());
  |                                ^^^^^ expected `Result<Option<&str>, _>`, found `Result<Option<String>, ()>`
  |
  = note: expected enum `Result<Option<&str>, _>`
             found enum `Result<Option<String>, ()>`
help: try wrapping the expression in `Err`
  |
6 |     assert_eq!(Ok(Some("hi")), Err(foo()));
  |                                ++++     +

Desired output

error[E0308]: mismatched types
 --> src/main.rs:6:32
  |
6 |     assert_eq!(Ok(Some("hi")), foo());
  |                                ^^^^^ expected `Result<Option<&str>, _>`, found `Result<Option<String>, ()>`
  |
  = note: expected enum `Result<Option<&str>, _>`

-- or ideally:

help: try converting &str into String
  |
6 |     assert_eq!(Ok(Some("hi".into()), foo());
  |                            +++++++

Rationale and extra context

No response

Other cases

Rust Version

nightly-2025-08-11

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