Skip to content

Commit

Permalink
Use multipart_suggestion to avoid place holder in span_to_snippet
Browse files Browse the repository at this point in the history
CO-AUTHORED-BY: @estebank
  • Loading branch information
tesuji committed Jul 13, 2024
1 parent fca3073 commit 3d85271
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
20 changes: 12 additions & 8 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 3d85271

Please sign in to comment.