Skip to content

FP redundant_clone: x.clone(); that clones & consumes #9898

Open

Description

@matthiaskrgr

Summary

.

Lint Name

redundant_clone

Reproducer

I tried this code:

// run-pass
#![allow(unused_must_use)]
#![allow(unused_imports)]
// pretty-expanded FIXME #23616
#![allow(deprecated)]

use std::hash::{Hash, SipHasher};

// testing multiple separate deriving attributes
#[derive(PartialEq)]
#[derive(Clone)]
#[derive(Hash)]
struct Foo {
    bar: usize,
    baz: isize
}

fn hash<T: Hash>(_t: &T) {}

pub fn main() {
    let a = Foo {bar: 4, baz: -3};

    a == a;    // check for PartialEq impl w/o testing its correctness
    a.clone(); // check for Clone impl w/o testing its correctness
    hash(&a);  // check for Hash impl w/o testing its correctness
}

I saw this happen:
cargo clippy --fix -- -Aclippy::all -Wclippy::redundant_clone

    Checking clpy v0.1.0 (/tmp/clpy)
warning: failed to automatically apply fixes suggested by rustc to crate `clpy`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0382]: borrow of moved value: `a`
  --> src/main.rs:25:10
   |
21 |     let a = Foo {bar: 4, baz: -3};
   |         - move occurs because `a` has type `Foo`, which does not implement the `Copy` trait
...
24 |     a; // check for Clone impl w/o testing its correctness
   |     - value moved here
25 |     hash(&a);  // check for Hash impl w/o testing its correctness
   |          ^^ value borrowed here after move

error: aborting due to previous error

For more information about this error, try `rustc --explain E0382`.
Original diagnostics will follow.

warning: redundant clone
  --> src/main.rs:24:6
   |
24 |     a.clone(); // check for Clone impl w/o testing its correctness
   |      ^^^^^^^^ help: remove this
   |
note: cloned value is neither consumed nor mutated
  --> src/main.rs:24:5
   |
24 |     a.clone(); // check for Clone impl w/o testing its correctness
   |     ^^^^^^^^^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone
   = note: requested on the command line with `-W clippy::redundant-clone`

warning: `clpy` (bin "clpy") generated 1 warning (run `cargo fix --bin "clpy"` to apply 1 suggestion)
warning: `clpy` (bin "clpy" test) generated 1 warning (1 duplicate)
    Finished dev [unoptimized + debuginfo] target(s) in 1.21s

I expected to see this happen:

Version

rustc 1.67.0-nightly (c5d82ed7a 2022-11-19)
binary: rustc
commit-hash: c5d82ed7a4ad94a538bb87e5016e7d5ce0bd434b
commit-date: 2022-11-19
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4

Additional Labels

No response

Activity

  1. added
    C-bugCategory: Clippy is not doing the correct thing
    I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied
    I-false-positiveIssue: The lint was triggered on code it shouldn't have
    on Nov 20, 2022
  2. KisaragiEffective commented on Nov 23, 2022

    @KisaragiEffective
    Contributor

    You should consider use static_assetions::assert_impl_all in above example, like this:

    assert_impl_all!(Foo: PartialEq, Clone, Hash);
  3. changed the title [-]FP redundant_clone[/-] [+]FP redundant_clone: x.clone(); that clones & consumes[/+] on Nov 28, 2022
  4. matthiaskrgr commented on Nov 28, 2022

    @matthiaskrgr
    MemberAuthor

    Much simple example:

        let arg: String = "".to_owned();
        arg.to_string();
        format!("{:?}", arg);

    in line 2 we would clone and directly consume the cloned value (because of the ;) but he lint does not recognize that if we removed JUST the clone here, we would instead consume the entire struct and prevent futher uses (line 3)

  5. CommanderStorm commented on Aug 27, 2026

    @CommanderStorm
    Contributor

    @rustbot label +L-nursery

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedL-nurseryLint: Currently in the nursery group

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions