Skip to content

Add clearer error message when mistakenly using ok_or instead of ok_or_else and equivalent method #119765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
StealthyKamereon opened this issue Jan 9, 2024 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@StealthyKamereon
Copy link

StealthyKamereon commented Jan 9, 2024

Code

fn main() -> Result<(), bool> {
    None.ok_or(|| true)?
}

Current output

error[E0277]: `?` couldn't convert the error to `bool`
 --> src/main.rs:3:24
  |
2 | fn main() -> Result<(), bool> {
  |              ---------------- expected `bool` because of this
3 |     None.ok_or(|| true)?
  |                        ^ the trait `From<{closure@src/main.rs:3:16: 3:18}>` is not implemented for `bool`
  |
  = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
  = help: the following other types implement trait `FromResidual<R>`:
            <Result<T, F> as FromResidual<Yeet<E>>>
            <Result<T, F> as FromResidual<Result<Infallible, E>>>
  = note: required for `Result<(), bool>` to implement `FromResidual<Result<Infallible, {closure@src/main.rs:3:16: 3:18}>>`

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

Desired output

`?` expected `bool` for `Err` variant but found `{closure@src/main.rs:3:16: 3:18}`.
Did you meant to use `ok_or_else` ?

Rationale and extra context

We could use the fact that the found type is a closure to detect the user probably wanted to use the *_else variant of the method.

Other cases

No response

Rust Version

rustc 1.77.0-nightly (ca663b06c 2024-01-08)
binary: rustc
commit-hash: ca663b06c5492ac2dde5e53cd11579fa8e4d68bd
commit-date: 2024-01-08
host: x86_64-pc-windows-msvc
release: 1.77.0-nightly
LLVM version: 17.0.6

Anything else?

No response

@StealthyKamereon StealthyKamereon added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 9, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 9, 2024
@fmease fmease removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 10, 2024
@obeis obeis removed their assignment May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants