diff options
| author | Lzu Tao <taolzu@gmail.com> | 2024-07-13 01:06:40 +0000 |
|---|---|---|
| committer | Lzu Tao <taolzu@gmail.com> | 2024-07-15 12:54:00 +0000 |
| commit | 9c3c278b545d9e03939e3bfcbebb256758b9a8b9 (patch) | |
| tree | bbf8f6d9d2bde11f953c3eb4b27f147641855db2 | |
| parent | 9c1a9e03d5e001543a21eaff5d616de8cb4e2220 (diff) | |
| download | rust-9c3c278b545d9e03939e3bfcbebb256758b9a8b9.tar.gz rust-9c3c278b545d9e03939e3bfcbebb256758b9a8b9.zip | |
Add support for `Result<&T, _>'
| -rw-r--r-- | compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs | 19 | ||||
| -rw-r--r-- | tests/ui/mismatched_types/transforming-option-ref-issue-127545.stderr | 4 |
2 files changed, 13 insertions, 10 deletions
diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index 15a1a18daa7..faa6d06ccaf 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -1451,9 +1451,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let ty::Adt(callee_adt, _) = callee_ty.peel_refs().kind() else { return; }; - if !self.tcx.is_diagnostic_item(sym::Option, callee_adt.did()) { + let adt_name = if self.tcx.is_diagnostic_item(sym::Option, callee_adt.did()) { + "Option" + } else if self.tcx.is_diagnostic_item(sym::Result, callee_adt.did()) { + "Result" + } else { return; - } + }; if call_ident.map_or(true, |ident| ident.name != sym::unwrap_or) { return; @@ -1484,14 +1488,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { Ok(snip) => (snip, Applicability::MachineApplicable), Err(_) => ("/* _ */".to_owned(), Applicability::MaybeIncorrect), }; - let sugg = &format!("map_or({provided_snip}, |v| v)"); - err.span_suggestion_verbose( - error_span, - "use `Option::map_or` to deref inner value of `Option`", - sugg, - applicability, - ); - return; + let sugg = format!("map_or({provided_snip}, |v| v)"); + let msg = format!("use `{adt_name}::map_or` to deref inner value of `{adt_name}`"); + err.span_suggestion_verbose(error_span, msg, sugg, applicability); } /// Suggest wrapping the block in square brackets instead of curly braces diff --git a/tests/ui/mismatched_types/transforming-option-ref-issue-127545.stderr b/tests/ui/mismatched_types/transforming-option-ref-issue-127545.stderr index 0a6d47339d8..1790fc1249a 100644 --- a/tests/ui/mismatched_types/transforming-option-ref-issue-127545.stderr +++ b/tests/ui/mismatched_types/transforming-option-ref-issue-127545.stderr @@ -80,6 +80,10 @@ LL | arg.unwrap_or(&[]) | this argument influences the return type of `unwrap_or` note: method defined here --> $SRC_DIR/core/src/result.rs:LL:COL +help: use `Result::map_or` to deref inner value of `Result` + | +LL | arg.map_or(&[], |v| v) + | ~~~~~~~~~~~~~~~~~~ error: aborting due to 4 previous errors |
