Skip to content

redundant-clone: False positive with moved data #5595

@Jonathas-Conceicao

Description

@Jonathas-Conceicao

I have made the following small example to illustrate a warning of redudant-clone I've been getting:

#[derive(Clone)]
struct NoCopy;

fn foo<F: Fn()>(_: &NoCopy, _: F) {}

fn main() {
    let data = NoCopy;
    foo(&data.clone(), move || {
        let _ = data;
    });
}

Which gives me:

warning: redundant clone
 --> src/main.rs:8:14
  |
8 |     foo(&data.clone(), move || {
  |              ^^^^^^^^ help: remove this
  |
  = note: `#[warn(clippy::redundant_clone)]` on by default
note: cloned value is neither consumed nor mutated
 --> src/main.rs:8:10
  |
8 |     foo(&data.clone(), move || {
  |          ^^^^^^^^^^^^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone

warning: 1 warning emitted

However, removing the clone would result in a error as the data will be moved to the closure while still borrowed as an argument to foo. Am I right to think this is a false positive, or am I messing something to fix this?

$ cargo clippy -V
clippy 0.0.212 (769d12e 2020-05-12)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingE-hardCall for participation: This a hard problem and requires more experience or effort to work onI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions