about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs20
-rw-r--r--tests/ui/mismatched_types/transforming-option-ref-issue-127545.stderr6
2 files changed, 15 insertions, 11 deletions
diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
index faa6d06ccaf..b3b4c5a56fb 100644
--- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
+++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
@@ -1459,7 +1459,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
             return;
         };
 
-        if call_ident.map_or(true, |ident| ident.name != sym::unwrap_or) {
+        let Some(call_ident) = call_ident else {
+            return;
+        };
+        if call_ident.name != sym::unwrap_or {
             return;
         }
 
@@ -1483,14 +1486,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
         if !self.can_coerce(expected_ty, dummy_ty) {
             return;
         }
-        let (provided_snip, applicability) =
-            match self.tcx.sess.source_map().span_to_snippet(provided_expr.span) {
-                Ok(snip) => (snip, Applicability::MachineApplicable),
-                Err(_) => ("/* _ */".to_owned(), Applicability::MaybeIncorrect),
-            };
-        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);
+        err.multipart_suggestion_verbose(
+            msg,
+            vec![
+                (call_ident.span, "map_or".to_owned()),
+                (provided_expr.span.shrink_to_hi(), ", |v| v".to_owned()),
+            ],
+            Applicability::MachineApplicable,
+        );
     }
 
     /// 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 1790fc1249a..ad423f86ef9 100644
--- a/tests/ui/mismatched_types/transforming-option-ref-issue-127545.stderr
+++ b/tests/ui/mismatched_types/transforming-option-ref-issue-127545.stderr
@@ -35,7 +35,7 @@ note: method defined here
 help: use `Option::map_or` to deref inner value of `Option`
    |
 LL |     arg.map_or(&[], |v| v)
-   |         ~~~~~~~~~~~~~~~~~~
+   |         ~~~~~~    +++++++
 
 error[E0308]: mismatched types
   --> $DIR/transforming-option-ref-issue-127545.rs:13:19
@@ -59,7 +59,7 @@ note: method defined here
 help: use `Option::map_or` to deref inner value of `Option`
    |
 LL |     arg.map_or(v, |v| v)
-   |         ~~~~~~~~~~~~~~~~
+   |         ~~~~~~  +++++++
 
 error[E0308]: mismatched types
   --> $DIR/transforming-option-ref-issue-127545.rs:17:19
@@ -83,7 +83,7 @@ note: method defined here
 help: use `Result::map_or` to deref inner value of `Result`
    |
 LL |     arg.map_or(&[], |v| v)
-   |         ~~~~~~~~~~~~~~~~~~
+   |         ~~~~~~    +++++++
 
 error: aborting due to 4 previous errors