Skip to content

FP redundant_clone: Box #10517

Open

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

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 Mar 17, 2023
  2. matthiaskrgr commented on Mar 17, 2023

    @matthiaskrgr
    MemberAuthor

    This seems to be a regression, at least on beta, there is no such warning, hm.

  3. matthiaskrgr commented on Mar 17, 2023

    @matthiaskrgr
    MemberAuthor

    Hmm, nightly-2023-03-15-x86_64-unknown-linux-gnu doesn't have this FP while rustc 1.70.0-nightly (511364e78 2023-03-16) does

  4. matthiaskrgr commented on Mar 17, 2023

    @matthiaskrgr
    MemberAuthor

    Ooh, I bet this broke in rust-lang/rust#108958 cc @clubby789

  5. clubby789 commented on Mar 17, 2023

    @clubby789
    Contributor

    @rustbot claim

  6. clubby789 commented on Mar 17, 2023

    @clubby789
    Contributor

    Hmm. I'm not sure how this specifically could have broken it - that PR pretty much just removes it from match arms, the expression itself hasn't emitted since I removed it in rust-lang/rust#108516. I'll try a bisect


    searched nightlies: from nightly-2023-03-15 to nightly-2023-03-17
    regressed nightly: nightly-2023-03-17
    searched commit range: rust-lang/rust@ab65486...511364e
    regressed commit: rust-lang/rust@511364e

    bisected with cargo-bisect-rustc v0.6.5

    Host triple: x86_64-unknown-linux-gnu
    Reproduce with:

    cargo bisect-rustc 2023-03-15 --end 2023-03-17 --script=./script.sh -vv 
  7. clubby789 commented on Mar 17, 2023

    @clubby789
    Contributor

    cc @cjgillot

    @rustbot release

  8. removed their assignment
    on Mar 17, 2023
  9. matthiaskrgr commented on Mar 17, 2023

    @matthiaskrgr
    MemberAuthor

    Oh my bad then, sorry 😅 this looked like the obvious contender to me since it removed box stuff and I thought maybe it removed a "saveguard" when linting against boxes...

  10. Jarcho commented on Mar 18, 2023

    @Jarcho
    Contributor

    I remember seeing this come up before, but can't find the issue now. This isn't a false positive as the new value isn't used, but the suggestion should be to remove the whole binding.

  11. removed
    I-false-positiveIssue: The lint was triggered on code it shouldn't have
    on Mar 18, 2023
  12. added
    L-suggestionLint: Improving, adding or fixing lint suggestions
    on Mar 22, 2023
  13. ryoqun commented on May 18, 2023

    @ryoqun

    I think this issue's root cause is same as this (see my cross post: #10577 (comment))

    pub fn main() {
        let i: Box<_> = Box::new(1);
        let j = i.clone();
        assert_eq!(*i, 2);
    }

    this is fine with +nightly-2023-03-16-x86_64-unknown-linux-gnu but starts to emit false positives with +nightly-2023-03-17-x86_64-unknown-linux-gnu

    then again, i think this pr is suspicious: rust-lang/rust#108944

  14. added a commit that references this issue on Jun 5, 2023
  15. CommanderStorm commented on Aug 27, 2026

    @CommanderStorm
    Contributor

    Issue sounds like aliasing, not sure.

    @rustbot label +L-nursary

  16. rustbot commented on Aug 27, 2026

    @rustbot
    Collaborator

    Error: Unknown labels: L-nursary

    Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #triagebot on Zulip.

  17. 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-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedL-nurseryLint: Currently in the nursery groupL-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