Skip to content

Commit b4524f8

Browse files
committed
Fix suggestion for removing &mut from &mut macro!().
1 parent 716394d commit b4524f8

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

compiler/rustc_typeck/src/check/demand.rs

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

1818
use std::fmt;
19+
use std::iter;
1920

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

0 commit comments

Comments
 (0)