Skip to content

Commit fd9c1b9

Browse files
committed
Find correct span when suggesting using clone
Signed-off-by: xizheyin <[email protected]>
1 parent 02dac5c commit fd9c1b9

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1302,8 +1302,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13021302
None => ".clone()".to_string(),
13031303
};
13041304

1305+
let span = expr.span.find_oldest_ancestor_in_same_ctxt().shrink_to_hi();
1306+
13051307
diag.span_suggestion_verbose(
1306-
expr.span.shrink_to_hi(),
1308+
span,
13071309
"consider using clone here",
13081310
suggestion,
13091311
Applicability::MachineApplicable,

tests/ui/typeck/suggestions/suggest-clone-in-macro-issue-139253.stderr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ LL | let c: S = dbg!(field);
2727
| ^^^^^^^^^^^ expected `S`, found `&S`
2828
|
2929
= note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
30+
help: consider using clone here
31+
|
32+
LL | let c: S = dbg!(field).clone();
33+
| ++++++++
3034

3135
error[E0308]: mismatched types
3236
--> $DIR/suggest-clone-in-macro-issue-139253.rs:16:16
@@ -35,6 +39,10 @@ LL | let c: S = dbg!(dbg!(field));
3539
| ^^^^^^^^^^^^^^^^^ expected `S`, found `&S`
3640
|
3741
= note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
42+
help: consider using clone here
43+
|
44+
LL | let c: S = dbg!(dbg!(field)).clone();
45+
| ++++++++
3846

3947
error: aborting due to 4 previous errors
4048

0 commit comments

Comments
 (0)