Skip to content

Commit 0786375

Browse files
committed
Suggest clone in user-write-code instead of inside macro
Signed-off-by: xizheyin <[email protected]>
1 parent ed44c0e commit 0786375

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1302,8 +1302,9 @@ 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();
13051306
diag.span_suggestion_verbose(
1306-
expr.span.shrink_to_hi(),
1307+
span,
13071308
"consider using clone here",
13081309
suggestion,
13091310
Applicability::MachineApplicable,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Make sure we don't suggest clone not in dbg macro
2+
3+
#[derive(Clone, Debug)]
4+
struct Point<T> {
5+
v: T,
6+
}
7+
8+
#[derive(Clone, Debug)]
9+
struct S;
10+
11+
fn foo(_: S) {}
12+
13+
fn test1() {
14+
let s = &S;
15+
foo(dbg!(s)); //~ ERROR mismatched types
16+
}
17+
18+
fn main() {
19+
let a: Point<u8> = dbg!(Point { v: 42 });
20+
let b: Point<u8> = dbg!(&a); //~ ERROR mismatched types [E0308]
21+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/suggest-clone-in-macro-issue-139253.rs:15:9
3+
|
4+
LL | foo(dbg!(s));
5+
| ^^^^^^^ expected `S`, found `&S`
6+
|
7+
= note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
8+
help: consider using clone here
9+
|
10+
LL | foo(dbg!(s).clone());
11+
| ++++++++
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/suggest-clone-in-macro-issue-139253.rs:20:24
15+
|
16+
LL | let b: Point<u8> = dbg!(&a);
17+
| ^^^^^^^^ expected `Point<u8>`, found `&Point<u8>`
18+
|
19+
= note: expected struct `Point<_>`
20+
found reference `&Point<_>`
21+
= note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
22+
help: consider using clone here
23+
|
24+
LL | let b: Point<u8> = dbg!(&a).clone();
25+
| ++++++++
26+
27+
error: aborting due to 2 previous errors
28+
29+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)