Skip to content

FP redundant_clone: Box #10517

Open
@matthiaskrgr

Description

@matthiaskrgr

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedL-suggestionLint: Improving, adding or fixing lint suggestions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions