rust-clippy
rust-clippy copied to clipboard
Use of a = b.clone() instead of a.clone_from(&b)
What it does
Catches the places where clone_from
could be used instead
Lint Name
clone_from_candidate
Category
pedantic
Advantage
From the docs
-
a.clone_from(&b)
is equivalent toa = b.clone()
in functionality, but can be overridden to reuse the resources of a to avoid unnecessary allocations.
Drawbacks
I can't think of any
Example
let mut a = "a".to_owned();
let b = "b".to_owned();
a = b.clone();
drop(b);
Could be written as:
let mut a = "a".to_owned();
let b = "b".to_owned();
a.clone_from(&b);
drop(b);
the snippet of code provided doesn't exactly work, but using b.clone_into(&mut a);
instead of a.clone_from(b);
does.
It would be good if the lint could check if the type has a provided fn clone_from
, if it's using the default from Clone
the suggestion wouldn't change anything
the snippet of code provided doesn't exactly work, but using
b.clone_into(&mut a);
instead ofa.clone_from(b);
does.
Fixed it
It would be good if the lint could check if the type has a provided
fn clone_from
, if it's using the default fromClone
the suggestion wouldn't change anything
There's no downsides to it and an upside so I'd say this is always more preferable