From bdc9df247841e259c24e95fd81754fb61e332c65 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sat, 13 Jul 2024 01:18:21 +0000 Subject: [PATCH] Use multipart_suggestion to avoid place holder in span_to_snippet MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CO-AUTHORED-BY: Esteban Küber --- .../src/fn_ctxt/suggestions.rs | 20 +++++++++++-------- ...ransforming-option-ref-issue-127545.stderr | 6 +++--- 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 @@ pub(crate) fn suggest_deref_unwrap_or( 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 @@ pub(crate) fn suggest_deref_unwrap_or( 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