Skip to content

assigning_clones causes infinite recursion when it triggers inside a clone_from() impl #12600

@matthiaskrgr

Description

@matthiaskrgr

Summary

maybe we should skip everything that is either self or anything that is inside a impl of fn clone_from()?
cc @Kobzol @blyxyas @kpreid

Reproducer

I tried this code:

impl<V: Clone> Clone for MaybeReachable<V> {
    fn clone(&self) -> Self {
        match self {
            MaybeReachable::Reachable(x) => MaybeReachable::Reachable(x.clone()),
            MaybeReachable::Unreachable => MaybeReachable::Unreachable,
        }
    }

    fn clone_from(&mut self, source: &Self) {
        match (&mut *self, source) {
            (MaybeReachable::Reachable(x), MaybeReachable::Reachable(y)) => {
                x.clone_from(y);
            }
            //_ => *self = source.clone(), // original
            _ => self.clone_from(source), // BOOM
        }
    }
}

I expected to see this happen:

Instead, this happened:

Version

rustc 1.79.0-nightly (ef4936510 2024-03-30)
binary: rustc
commit-hash: ef493651025db2d5c38225c12ef97fd832c00c4a
commit-date: 2024-03-30
host: x86_64-unknown-linux-gnu
release: 1.79.0-nightly
LLVM version: 18.1.2

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-bugIssue: The suggestion compiles but changes the code to behave in an unintended way

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions