Skip to content

False positive for clone_on_copy when the clone is immediately borrowed #1472

Closed
@scurest

Description

@scurest

Consider

extern crate rand;
use rand::Rng;

let r = rand::ChaChaRng::new_unseeded(); // ChaChaRng implements Copy
let x = r.clone().next_u32(); // peek at next u32, without modifying r

This triggers clone_on_copy, suggesting removing the clone call. Taking the suggestion is illegal because r is not mutable. If it were mutable, taking the suggestion would change the behavior of the program.

So it seems like we don't want to lint when the clone is immediately borrowed mutably.

You can also observe a difference (using ref_eq for example) when the clone is borrowed immutably, so perhaps it's better to lint only if the clone is immediately moved from?

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingE-needs-mcveCall for participation: This issue needs a Minimal Complete and Verifiable ExampleI-false-positiveIssue: The lint was triggered on code it shouldn't haveT-middleType: Probably requires verifiying types

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions