Skip to content

Commit 5ebc4d3

Browse files
authored
Rollup merge of #85939 - m-ou-se:fix-remove-ref-macro-invocation, r=estebank
Fix suggestion for removing &mut from &mut macro!(). Fixes #85933 Before: (Note the suggestions.) ``` error[E0308]: mismatched types --> src/main.rs:2:21 | 2 | let _: String = &mut format!(""); | ------ ^^^^^^^^^^^^^^^^ | | | | | expected struct `String`, found `&mut String` | | help: consider removing the borrow: `mut format!("")` | expected due to this error[E0308]: mismatched types --> src/main.rs:3:21 | 3 | let _: String = &mut (format!("")); | ------ ^^^^^^^^^^^^^^^^^^ | | | | | expected struct `String`, found `&mut String` | | help: consider removing the borrow: `mut (format!(""))` | expected due to this ``` After: ``` error[E0308]: mismatched types --> src/main.rs:2:21 | 2 | let _: String = &mut format!(""); | ------ ^^^^^^^^^^^^^^^^ | | | | | expected struct `String`, found `&mut String` | | help: consider removing the borrow: `format!("")` | expected due to this error[E0308]: mismatched types --> src/main.rs:3:21 | 3 | let _: String = &mut (format!("")); | ------ ^^^^^^^^^^^^^^^^^^ | | | | | expected struct `String`, found `&mut String` | | help: consider removing the borrow: `format!("")` | expected due to this ```
2 parents ec9e7d5 + ecebb66 commit 5ebc4d3

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

Diff for: compiler/rustc_typeck/src/check/demand.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_span::Span;
1717
use super::method::probe;
1818

1919
use std::fmt;
20+
use std::iter;
2021

2122
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2223
pub fn emit_coerce_suggestions(
@@ -573,12 +574,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
573574
// We have `&T`, check if what was expected was `T`. If so,
574575
// we may want to suggest removing a `&`.
575576
if sm.is_imported(expr.span) {
576-
if let Ok(src) = sm.span_to_snippet(sp) {
577-
if let Some(src) = src.strip_prefix('&') {
577+
// Go through the spans from which this span was expanded,
578+
// and find the one that's pointing inside `sp`.
579+
//
580+
// E.g. for `&format!("")`, where we want the span to the
581+
// `format!()` invocation instead of its expansion.
582+
if let Some(call_span) =
583+
iter::successors(Some(expr.span), |s| s.parent()).find(|&s| sp.contains(s))
584+
{
585+
if let Ok(code) = sm.span_to_snippet(call_span) {
578586
return Some((
579587
sp,
580588
"consider removing the borrow",
581-
src.to_string(),
589+
code,
582590
Applicability::MachineApplicable,
583591
));
584592
}

Diff for: src/test/ui/suggestions/format-borrow.rs

+4
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ fn main() {
33
//~^ ERROR mismatched types
44
let b: String = &format!("b");
55
//~^ ERROR mismatched types
6+
let c: String = &mut format!("c");
7+
//~^ ERROR mismatched types
8+
let d: String = &mut (format!("d"));
9+
//~^ ERROR mismatched types
610
}

Diff for: src/test/ui/suggestions/format-borrow.stderr

+21-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@ LL | let b: String = &format!("b");
1818
| | help: consider removing the borrow: `format!("b")`
1919
| expected due to this
2020

21-
error: aborting due to 2 previous errors
21+
error[E0308]: mismatched types
22+
--> $DIR/format-borrow.rs:6:21
23+
|
24+
LL | let c: String = &mut format!("c");
25+
| ------ ^^^^^^^^^^^^^^^^^
26+
| | |
27+
| | expected struct `String`, found `&mut String`
28+
| | help: consider removing the borrow: `format!("c")`
29+
| expected due to this
30+
31+
error[E0308]: mismatched types
32+
--> $DIR/format-borrow.rs:8:21
33+
|
34+
LL | let d: String = &mut (format!("d"));
35+
| ------ ^^^^^^^^^^^^^^^^^^^
36+
| | |
37+
| | expected struct `String`, found `&mut String`
38+
| | help: consider removing the borrow: `format!("d")`
39+
| expected due to this
40+
41+
error: aborting due to 4 previous errors
2242

2343
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)