From b4524f8bf0650113f764b01918ff7a65601e0050 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 2 Jun 2021 19:06:23 +0200 Subject: [PATCH 1/2] Fix suggestion for removing &mut from &mut macro!(). --- compiler/rustc_typeck/src/check/demand.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_typeck/src/check/demand.rs b/compiler/rustc_typeck/src/check/demand.rs index e5fcdcfa743..5b1f48bde0d 100644 --- a/compiler/rustc_typeck/src/check/demand.rs +++ b/compiler/rustc_typeck/src/check/demand.rs @@ -16,6 +16,7 @@ use super::method::probe; use std::fmt; +use std::iter; impl<'a, 'tcx> FnCtxt<'a, 'tcx> { pub fn emit_coerce_suggestions( @@ -577,12 +578,19 @@ pub fn check_ref( // We have `&T`, check if what was expected was `T`. If so, // we may want to suggest removing a `&`. if sm.is_imported(expr.span) { - if let Ok(src) = sm.span_to_snippet(sp) { - if let Some(src) = src.strip_prefix('&') { + // Go through the spans from which this span was expanded, + // and find the one that's pointing inside `sp`. + // + // E.g. for `&format!("")`, where we want the span to the + // `format!()` invocation instead of its expansion. + if let Some(call_span) = + iter::successors(Some(expr.span), |s| s.parent()).find(|&s| sp.contains(s)) + { + if let Ok(code) = sm.span_to_snippet(call_span) { return Some(( sp, "consider removing the borrow", - src.to_string(), + code, Applicability::MachineApplicable, )); } From ecebb669d5fa442b903a3a17f72cbf2268a5a080 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Wed, 2 Jun 2021 19:06:45 +0200 Subject: [PATCH 2/2] Add test for removing &mut for &mut format!(). --- src/test/ui/suggestions/format-borrow.rs | 4 ++++ src/test/ui/suggestions/format-borrow.stderr | 22 +++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/test/ui/suggestions/format-borrow.rs b/src/test/ui/suggestions/format-borrow.rs index 63930e7f787..599a79fc08a 100644 --- a/src/test/ui/suggestions/format-borrow.rs +++ b/src/test/ui/suggestions/format-borrow.rs @@ -3,4 +3,8 @@ fn main() { //~^ ERROR mismatched types let b: String = &format!("b"); //~^ ERROR mismatched types + let c: String = &mut format!("c"); + //~^ ERROR mismatched types + let d: String = &mut (format!("d")); + //~^ ERROR mismatched types } diff --git a/src/test/ui/suggestions/format-borrow.stderr b/src/test/ui/suggestions/format-borrow.stderr index 05d8fcd3ed6..0881b024712 100644 --- a/src/test/ui/suggestions/format-borrow.stderr +++ b/src/test/ui/suggestions/format-borrow.stderr @@ -18,6 +18,26 @@ LL | let b: String = &format!("b"); | | help: consider removing the borrow: `format!("b")` | expected due to this -error: aborting due to 2 previous errors +error[E0308]: mismatched types + --> $DIR/format-borrow.rs:6:21 + | +LL | let c: String = &mut format!("c"); + | ------ ^^^^^^^^^^^^^^^^^ + | | | + | | expected struct `String`, found `&mut String` + | | help: consider removing the borrow: `format!("c")` + | expected due to this + +error[E0308]: mismatched types + --> $DIR/format-borrow.rs:8:21 + | +LL | let d: String = &mut (format!("d")); + | ------ ^^^^^^^^^^^^^^^^^^^ + | | | + | | expected struct `String`, found `&mut String` + | | help: consider removing the borrow: `format!("d")` + | expected due to this + +error: aborting due to 4 previous errors For more information about this error, try `rustc --explain E0308`.