rust-lang / rust-lang/rust-clippy
FP redundant_clone: x.clone(); that clones & consumes
Open
Nobody has claimed this yet.
C-bug
I-false-positive
I-suggestion-causes-error
L-nursery
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the redundant_clone lint and reproduce the reported cargo clippy --fix command with the Rust example in this issue. Check that removing the suggested clone does not move a non-Copy value still used by hash(&a), then add a regression test and verify the fixed command no longer produces E0382.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100