rust-lang / rust-lang/rust-clippy
FP redundant_clone: Box
Open
Nobody has claimed this yet.
C-bug
I-suggestion-causes-error
L-nursery
L-suggestion
- 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:
pub fn main() {
let i: Box<_> = Box::new(1);
let j = i.clone();
assert_eq!(*i, 2);
}
I saw this happen:
warning: redundant clone
--> unique-decl-init-copy.rs:3:14
|
3 | let j = i.clone();
| ^^^^^^^^ help: remove this
|
note: cloned value is neither consumed nor mutated
--> unique-decl-init-copy.rs:3:13
|
3 | let j = i.clone();
| ^^^^^^^^^
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone
= note: `#[warn(clippy::redundant_clone)]` on by defaul
But
pub fn main() {
let i: Box<_> = Box::new(1);
let j = i;
assert_eq!(*i, 2);
}
does not compile
warning: unused variable: `j`
--> unique-decl-init-copy.rs:3:9
|
3 | let j = i;
| ^ help: if this is intentional, prefix it with an underscore: `_j`
|
= note: `#[warn(unused_variables)]` on by default
error[E0382]: borrow of moved value: `i`
--> unique-decl-init-copy.rs:4:5
|
2 | let i: Box<_> = Box::new(1);
| - move occurs because `i` has type `std::boxed::Box<i32>`, which does not implement the `Copy` trait
3 | let j = i;
| - value moved here
4 | assert_eq!(*i, 2);
| ^^^^^^^^^^^^^^^^^ value borrowed here after move
|
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider cloning the value if the performance cost is acceptable
|
3 | let j = i.clone();
| ++++++++
error: aborting due to previous error; 1 warning emitted
Version
rustc 1.70.0-nightly (511364e78 2023-03-16)
binary: rustc
commit-hash: 511364e7874dba9649a264100407e4bffe7b5425
commit-date: 2023-03-16
host: x86_64-unknown-linux-gnu
release: 1.70.0-nightly
LLVM version: 15.0.7
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 by locating the redundant_clone lint implementation and its tests in the Clippy repository, then run the supplied Box reproducer. Trace why the lint treats the clone as redundant even though the original value is used afterward, and add coverage showing the warning is not emitted for this case.
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
- 65/100