This lint warns you when you've called .clone().iter_mut() without saving the result anywhere.
You rarely want to mutate a cloned value without first saving the data somewhere. You can't access the data if it's not stored in a variable, so mutating it would be a noop.
Advantage
Avoids accidental noops
Almost never necessary to use .clone().iter_mut()
.clone().iter_mut() is relatively unused
On Github, only 290 Rust files use it, out of 590M.
Drawbacks
Someone might use .clone().iter_mut() just for convenience when one of the functions in the for loop takes a mutable reference.
E.g. vec.append(&mut x)
Usually a non mutable version exists (e.g. vec.extend)
Example
fn add_one(a : &mut Vec<usize>) {
for x in a.clone().iter_mut() {
// oops, this does nothing
*x += 2;
}
}
Could be written as:
fn add_one(a : &mut Vec<usize>) {
for x in a.iter_mut() {
*x += 2;
}
}
fn added_one(a : &Vec<usize>) -> Vec<usize> {
// no need for it to be mutable
a.clone().iter_mut().map(|&mut x| x + 1).collect()
}
What it does
This lint warns you when you've called
.clone().iter_mut()
without saving the result anywhere.You rarely want to mutate a cloned value without first saving the data somewhere. You can't access the data if it's not stored in a variable, so mutating it would be a noop.
Advantage
.clone().iter_mut()
.clone().iter_mut()
is relatively unusedDrawbacks
.clone().iter_mut()
just for convenience when one of the functions in the for loop takes a mutable reference.vec.append(&mut x)
Example
Could be written as:
Could be written as: