Skip to content

Add a suggestion for the ok_or method #119854

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,7 @@ symbols! {
offset_of,
offset_of_enum,
offset_of_nested,
ok_or,
ok_or_else,
omit_gdb_pretty_printer_section,
on,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,19 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
);
}

if path_segment.ident.name == sym::ok_or && is_diagnostic_item(sym::Option, next_ty) {
err.span_suggestion(
path_segment.ident.span,
format!(
"`?` expected `{}` for `Err` variant but found `{:?}`. Use the `ok_or_else` method to pass a closure",
self_ty,
get_e_type(prev_ty).unwrap().to_string(),
),
"ok_or_else",
Applicability::MachineApplicable,
);
}

prev_ty = next_ty;

if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/closures/closure-ok-or.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// run-rustfix

// issue #119765

fn main() -> Result<(), bool> {
None.ok_or_else(|| true)?
//~^ ERROR `?` couldn't convert the error to `bool` [E0277]
}
8 changes: 8 additions & 0 deletions tests/ui/closures/closure-ok-or.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// run-rustfix

// issue #119765

fn main() -> Result<(), bool> {
None.ok_or(|| true)?
//~^ ERROR `?` couldn't convert the error to `bool` [E0277]
}
23 changes: 23 additions & 0 deletions tests/ui/closures/closure-ok-or.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error[E0277]: `?` couldn't convert the error to `bool`
--> $DIR/closure-ok-or.rs:6:24
|
LL | fn main() -> Result<(), bool> {
| ---------------- expected `bool` because of this
LL | None.ok_or(|| true)?
| --------------^ the trait `From<{closure@$DIR/closure-ok-or.rs:6:16: 6:18}>` is not implemented for `bool`
| |
| this can't be annotated with `?` because it has type `Result<_, {closure@$DIR/closure-ok-or.rs:6:16: 6:18}>`
|
= 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@$DIR/closure-ok-or.rs:6:16: 6:18}>>`
help: `?` expected `bool` for `Err` variant but found `"{closure@$DIR/closure-ok-or.rs:6:16: 6:18}"`. Use the `ok_or_else` method to pass a closure
|
LL | None.ok_or_else(|| true)?
| ~~~~~~~~~~

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0277`.