rust/tests/ui/traits/derive-implicit-bound-on-clone.rs
Esteban Küber 085da0cee4 On E0308 caused by cloning a reference due to missing bounds, account for derive
On type errors where the difference is expecting an owned type and getting a reference, if the expression is a `.clone()` call and the type is annotated with `#[derive(Clone)]`, we now explain implicit bounds and suggest manually implementing `Clone`.

```
error[E0308]: mismatched types
  --> $DIR/derive-implicit-bound-on-clone.rs:10:5
   |
LL | fn clone_me<T, K>(x: &ContainsRc<T, K>) -> ContainsRc<T, K> {
   |                                            ---------------- expected `ContainsRc<T, K>` because of return type
LL |     x.clone()
   |     ^^^^^^^^^ expected `ContainsRc<T, K>`, found `&ContainsRc<T, K>`
   |
   = note: expected struct `ContainsRc<_, _>`
           found reference `&ContainsRc<_, _>`
note: `ContainsRc<T, K>` does not implement `Clone`, so `&ContainsRc<T, K>` was cloned instead
  --> $DIR/derive-implicit-bound-on-clone.rs:10:5
   |
LL |     x.clone()
   |     ^
help: `Clone` is not implemented because the some trait bounds could not be satisfied
  --> $DIR/derive-implicit-bound-on-clone.rs:5:19
   |
LL | #[derive(Clone)]
   |          ----- in this derive macro expansion
LL | struct ContainsRc<T, K> {
   |                   ^  ^ derive introduces an implicit unsatisfied trait bound `K: Clone`
   |                   |
   |                   derive introduces an implicit unsatisfied trait bound `T: Clone`
   = help: consider manually implementing `Clone` to avoid the implict type parameter bounds
```
2026-02-01 18:20:30 +00:00

24 lines
562 B
Rust

// Issue #146515
use std::rc::Rc;
#[derive(Clone)]
struct ContainsRc<T, K> { //~ HELP `Clone` is not implemented
value: Rc<(T, K)>,
}
fn clone_me<T, K>(x: &ContainsRc<T, K>) -> ContainsRc<T, K> {
x.clone() //~ ERROR E0308
//~^ HELP consider manually implementing `Clone`
}
#[derive(Clone)]
struct ContainsRcSingle<T> { //~ HELP `Clone` is not implemented
value: Rc<T>,
}
fn clone_me_single<T>(x: &ContainsRcSingle<T>) -> ContainsRcSingle<T> {
x.clone() //~ ERROR E0308
//~^ HELP consider manually implementing `Clone`
}
fn main() {}