about summary refs log tree commit diff
diff options
context:
space:
mode:
authorxizheyin <xizheyin@smail.nju.edu.cn>2025-06-17 23:48:03 +0800
committerxizheyin <xizheyin@smail.nju.edu.cn>2025-06-17 23:48:47 +0800
commitfd9c1b962ae29ff928c2c6310a227b56b9db19bd (patch)
treed0c0ccc1f36a4f2ae55e96b8368300b1d2a3ab0f
parent02dac5cd78ff36663facc7863e6b4ed4b8df92b2 (diff)
downloadrust-fd9c1b962ae29ff928c2c6310a227b56b9db19bd.tar.gz
rust-fd9c1b962ae29ff928c2c6310a227b56b9db19bd.zip
Find correct span when suggesting using clone
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
-rw-r--r--compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs4
-rw-r--r--tests/ui/typeck/suggestions/suggest-clone-in-macro-issue-139253.stderr8
2 files changed, 11 insertions, 1 deletions
diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
index 7e5f1d97a8b..1154cc26c59 100644
--- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
+++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
@@ -1302,8 +1302,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                 None => ".clone()".to_string(),
             };
 
+            let span = expr.span.find_oldest_ancestor_in_same_ctxt().shrink_to_hi();
+
             diag.span_suggestion_verbose(
-                expr.span.shrink_to_hi(),
+                span,
                 "consider using clone here",
                 suggestion,
                 Applicability::MachineApplicable,
diff --git a/tests/ui/typeck/suggestions/suggest-clone-in-macro-issue-139253.stderr b/tests/ui/typeck/suggestions/suggest-clone-in-macro-issue-139253.stderr
index e615cecb365..59e56f67237 100644
--- a/tests/ui/typeck/suggestions/suggest-clone-in-macro-issue-139253.stderr
+++ b/tests/ui/typeck/suggestions/suggest-clone-in-macro-issue-139253.stderr
@@ -27,6 +27,10 @@ LL |     let c: S = dbg!(field);
    |                ^^^^^^^^^^^ expected `S`, found `&S`
    |
    = note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
+help: consider using clone here
+   |
+LL |     let c: S = dbg!(field).clone();
+   |                           ++++++++
 
 error[E0308]: mismatched types
   --> $DIR/suggest-clone-in-macro-issue-139253.rs:16:16
@@ -35,6 +39,10 @@ LL |     let c: S = dbg!(dbg!(field));
    |                ^^^^^^^^^^^^^^^^^ expected `S`, found `&S`
    |
    = note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
+help: consider using clone here
+   |
+LL |     let c: S = dbg!(dbg!(field)).clone();
+   |                                 ++++++++
 
 error: aborting due to 4 previous errors