Use multipart_suggestion to avoid place holder in span_to_snippet

CO-AUTHORED-BY: Esteban Küber <esteban@kuber.com.ar>
This commit is contained in:
Lzu Tao 2024-07-13 01:18:21 +00:00
parent 9c3c278b54
commit bdc9df2478
2 changed files with 15 additions and 11 deletions

View file

@ -1459,7 +1459,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return; 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; return;
} }
@ -1483,14 +1486,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if !self.can_coerce(expected_ty, dummy_ty) { if !self.can_coerce(expected_ty, dummy_ty) {
return; 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}`"); 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 /// Suggest wrapping the block in square brackets instead of curly braces

View file

@ -35,7 +35,7 @@ note: method defined here
help: use `Option::map_or` to deref inner value of `Option` help: use `Option::map_or` to deref inner value of `Option`
| |
LL | arg.map_or(&[], |v| v) LL | arg.map_or(&[], |v| v)
| ~~~~~~~~~~~~~~~~~~ | ~~~~~~ +++++++
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/transforming-option-ref-issue-127545.rs:13:19 --> $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` help: use `Option::map_or` to deref inner value of `Option`
| |
LL | arg.map_or(v, |v| v) LL | arg.map_or(v, |v| v)
| ~~~~~~~~~~~~~~~~ | ~~~~~~ +++++++
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/transforming-option-ref-issue-127545.rs:17:19 --> $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` help: use `Result::map_or` to deref inner value of `Result`
| |
LL | arg.map_or(&[], |v| v) LL | arg.map_or(&[], |v| v)
| ~~~~~~~~~~~~~~~~~~ | ~~~~~~ +++++++
error: aborting due to 4 previous errors error: aborting due to 4 previous errors