Skip to content

Commit 89bd5bf

Browse files
committed
Add a suggestion for the ok_or method
1 parent 3a6bf35 commit 89bd5bf

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
@@ -1151,6 +1151,7 @@ symbols! {
11511151
offset,
11521152
offset_of,
11531153
offset_of_enum,
1154+
ok_or,
11541155
ok_or_else,
11551156
omit_gdb_pretty_printer_section,
11561157
on,

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

+13
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,19 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
11581158
);
11591159
}
11601160

1161+
if path_segment.ident.name == sym::ok_or && is_diagnostic_item(sym::Option, next_ty) {
1162+
err.span_suggestion(
1163+
path_segment.ident.span,
1164+
format!(
1165+
"`?` expected `{}` for `Err` variant but found `{:?}`. Did you meant to use `ok_or_else`?",
1166+
self_ty,
1167+
get_e_type(prev_ty).unwrap().to_string(),
1168+
),
1169+
"ok_or_else",
1170+
Applicability::MachineApplicable,
1171+
);
1172+
}
1173+
11611174
prev_ty = next_ty;
11621175

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

0 commit comments

Comments
 (0)