rust/tests/ui/moves/assignment-of-clone-call-on-ref-due-to-missing-bound.rs
Esteban Küber a754fcaa43 On "this .clone() is on the reference", provide more info
When encountering a case where `let x: T = (val: &T).clone();` and
`T: !Clone`, already mention that the reference is being cloned. We now
also suggest `#[derive(Clone)]` not only on `T` but also on type
parameters to satisfy blanket implementations.

```
error[E0308]: mismatched types
  --> $DIR/assignment-of-clone-call-on-ref-due-to-missing-bound.rs:17:39
   |
LL |             let mut x: HashSet<Day> = v.clone();
   |                        ------------   ^^^^^^^^^ expected `HashSet<Day>`, found `&HashSet<Day>`
   |                        |
   |                        expected due to this
   |
   = note: expected struct `HashSet<Day>`
           found reference `&HashSet<Day>`
note: `HashSet<Day>` does not implement `Clone`, so `&HashSet<Day>` was cloned instead
  --> $DIR/assignment-of-clone-call-on-ref-due-to-missing-bound.rs:17:39
   |
LL |             let mut x: HashSet<Day> = v.clone();
   |                                       ^
   = help: `Clone` is not implemented because the trait bound `Day: Clone` is not satisfied
help: consider annotating `Day` with `#[derive(Clone)]`
   |
LL + #[derive(Clone)]
LL | enum Day {
   |
```

Case taken from # #41825.
2023-12-04 21:54:33 +00:00

30 lines
557 B
Rust

// run-rustfix
#![allow(unused_variables, dead_code)]
use std::collections::BTreeMap;
use std::collections::HashSet;
#[derive(Debug,Eq,PartialEq,Hash)]
enum Day {
Mon,
}
struct Class {
days: BTreeMap<u32, HashSet<Day>>
}
impl Class {
fn do_stuff(&self) {
for (_, v) in &self.days {
let mut x: HashSet<Day> = v.clone(); //~ ERROR
let y: Vec<Day> = x.drain().collect();
println!("{:?}", x);
}
}
}
fn fail() {
let c = Class { days: BTreeMap::new() };
c.do_stuff();
}
fn main() {}