Skip to content

Commit bdc9df2

Browse files
tesujiestebank
andcommitted
Use multipart_suggestion to avoid place holder in span_to_snippet
CO-AUTHORED-BY: Esteban Küber <[email protected]>
1 parent 9c3c278 commit bdc9df2

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -1459,7 +1459,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14591459
return;
14601460
};
14611461

1462-
if call_ident.map_or(true, |ident| ident.name != sym::unwrap_or) {
1462+
let Some(call_ident) = call_ident else {
1463+
return;
1464+
};
1465+
if call_ident.name != sym::unwrap_or {
14631466
return;
14641467
}
14651468

@@ -1483,14 +1486,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14831486
if !self.can_coerce(expected_ty, dummy_ty) {
14841487
return;
14851488
}
1486-
let (provided_snip, applicability) =
1487-
match self.tcx.sess.source_map().span_to_snippet(provided_expr.span) {
1488-
Ok(snip) => (snip, Applicability::MachineApplicable),
1489-
Err(_) => ("/* _ */".to_owned(), Applicability::MaybeIncorrect),
1490-
};
1491-
let sugg = format!("map_or({provided_snip}, |v| v)");
14921489
let msg = format!("use `{adt_name}::map_or` to deref inner value of `{adt_name}`");
1493-
err.span_suggestion_verbose(error_span, msg, sugg, applicability);
1490+
err.multipart_suggestion_verbose(
1491+
msg,
1492+
vec![
1493+
(call_ident.span, "map_or".to_owned()),
1494+
(provided_expr.span.shrink_to_hi(), ", |v| v".to_owned()),
1495+
],
1496+
Applicability::MachineApplicable,
1497+
);
14941498
}
14951499

14961500
/// Suggest wrapping the block in square brackets instead of curly braces

tests/ui/mismatched_types/transforming-option-ref-issue-127545.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ note: method defined here
3535
help: use `Option::map_or` to deref inner value of `Option`
3636
|
3737
LL | arg.map_or(&[], |v| v)
38-
| ~~~~~~~~~~~~~~~~~~
38+
| ~~~~~~ +++++++
3939

4040
error[E0308]: mismatched types
4141
--> $DIR/transforming-option-ref-issue-127545.rs:13:19
@@ -59,7 +59,7 @@ note: method defined here
5959
help: use `Option::map_or` to deref inner value of `Option`
6060
|
6161
LL | arg.map_or(v, |v| v)
62-
| ~~~~~~~~~~~~~~~~
62+
| ~~~~~~ +++++++
6363

6464
error[E0308]: mismatched types
6565
--> $DIR/transforming-option-ref-issue-127545.rs:17:19
@@ -83,7 +83,7 @@ note: method defined here
8383
help: use `Result::map_or` to deref inner value of `Result`
8484
|
8585
LL | arg.map_or(&[], |v| v)
86-
| ~~~~~~~~~~~~~~~~~~
86+
| ~~~~~~ +++++++
8787

8888
error: aborting due to 4 previous errors
8989

0 commit comments

Comments
 (0)