Skip to content

Commit 9c3c278

Browse files
committed
Add support for `Result<&T, _>'
1 parent 9c1a9e0 commit 9c3c278

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -1451,9 +1451,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14511451
let ty::Adt(callee_adt, _) = callee_ty.peel_refs().kind() else {
14521452
return;
14531453
};
1454-
if !self.tcx.is_diagnostic_item(sym::Option, callee_adt.did()) {
1454+
let adt_name = if self.tcx.is_diagnostic_item(sym::Option, callee_adt.did()) {
1455+
"Option"
1456+
} else if self.tcx.is_diagnostic_item(sym::Result, callee_adt.did()) {
1457+
"Result"
1458+
} else {
14551459
return;
1456-
}
1460+
};
14571461

14581462
if call_ident.map_or(true, |ident| ident.name != sym::unwrap_or) {
14591463
return;
@@ -1484,14 +1488,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14841488
Ok(snip) => (snip, Applicability::MachineApplicable),
14851489
Err(_) => ("/* _ */".to_owned(), Applicability::MaybeIncorrect),
14861490
};
1487-
let sugg = &format!("map_or({provided_snip}, |v| v)");
1488-
err.span_suggestion_verbose(
1489-
error_span,
1490-
"use `Option::map_or` to deref inner value of `Option`",
1491-
sugg,
1492-
applicability,
1493-
);
1494-
return;
1491+
let sugg = format!("map_or({provided_snip}, |v| v)");
1492+
let msg = format!("use `{adt_name}::map_or` to deref inner value of `{adt_name}`");
1493+
err.span_suggestion_verbose(error_span, msg, sugg, applicability);
14951494
}
14961495

14971496
/// Suggest wrapping the block in square brackets instead of curly braces

tests/ui/mismatched_types/transforming-option-ref-issue-127545.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ LL | arg.unwrap_or(&[])
8080
| this argument influences the return type of `unwrap_or`
8181
note: method defined here
8282
--> $SRC_DIR/core/src/result.rs:LL:COL
83+
help: use `Result::map_or` to deref inner value of `Result`
84+
|
85+
LL | arg.map_or(&[], |v| v)
86+
| ~~~~~~~~~~~~~~~~~~
8387

8488
error: aborting due to 4 previous errors
8589

0 commit comments

Comments
 (0)