Skip to content

Commit 834d146

Browse files
committed
Add a suggestion for the ok_or method
1 parent a58ec8f commit 834d146

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,7 @@ symbols! {
11541154
offset_of,
11551155
offset_of_enum,
11561156
offset_of_nested,
1157+
ok_or,
11571158
ok_or_else,
11581159
omit_gdb_pretty_printer_section,
11591160
on,

compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,19 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
11551155
);
11561156
}
11571157

1158+
if path_segment.ident.name == sym::ok_or && is_diagnostic_item(sym::Option, next_ty) {
1159+
err.span_suggestion(
1160+
path_segment.ident.span,
1161+
format!(
1162+
"`?` expected `{}` for `Err` variant but found `{:?}`. Use the `ok_or_else` method to pass a closure",
1163+
self_ty,
1164+
get_e_type(prev_ty).unwrap().to_string(),
1165+
),
1166+
"ok_or_else",
1167+
Applicability::MachineApplicable,
1168+
);
1169+
}
1170+
11581171
prev_ty = next_ty;
11591172

11601173
if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind

0 commit comments

Comments
 (0)