rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

Fix handling of `Deref` in `assigning_clones`

Open Kobzol opened this issue 1 year ago • 15 comments

The assigning_clones lint had a special case for producing a bit nicer code for mutable references:

fn clone_function_lhs_mut_ref(mut_thing: &mut HasCloneFrom, ref_thing: &HasCloneFrom) {
    *mut_thing = Clone::clone(ref_thing);
}
//v
fn clone_function_lhs_mut_ref(mut_thing: &mut HasCloneFrom, ref_thing: &HasCloneFrom) {
    Clone::clone_from(mut_thing, ref_thing);
}

However, this could break when combined with Deref.

This PR removes the special case, so that the generated code should work more generally. Later we can improve the detection of Deref and put the special case back in a way that does not break code.

Fixes: https://github.com/rust-lang/rust-clippy/issues/12437

r? @blyxyas

changelog: [assigning_clones]: change applicability to Unspecified and fix a problem with Deref.

Kobzol avatar Mar 12 '24 16:03 Kobzol