rust-lang / rust-lang/rust-clippy

Cargo clone_on_copy false positive or bad suggestion

Open
#7,414 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Lint name: clone_on_copy

I tried this code:

    #[test]
    fn copy_on_clone_false_positive() {
        let mut empty = [];
        let mut array = [0, 1];
        assert_eq!(
            (&mut empty[..], &mut array.clone()[..]),
            array.split_at_mut(0)
        );
    }
$ cargo clippy --all-targets

I expected to see this happen: No warning.

Instead, this happened: This warning:

warning: using `clone` on type `[i32; 2]` which implements the `Copy` trait
   --> src/file.rs:470:35
    |
470 |             (&mut empty[..], &mut array.clone()[..]),
    |                                   ^^^^^^^^^^^^^ help: try removing the `clone` call: `array`
    |
    = note: `#[warn(clippy::clone_on_copy)]` on by default
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy

warning: 1 warning emitted

When I apply the suggested fix:

    #[test]
    fn copy_on_clone_false_positive() {
        let mut empty = [];
        let mut array = [0, 1];
        assert_eq!(
            (&mut empty[..], &mut array[..]),
            array.split_at_mut(0)
        );
    }

It no longer compiles:

error[E0499]: cannot borrow `array` as mutable more than once at a time
   --> src/file.rs:469:54
    |
469 |         assert_eq!((&mut empty[..], &mut array[..]), array.split_at_mut(0));
    |         ---------------------------------------------^^^^^------------------
    |         |                                |           |
    |         |                                |           second mutable borrow occurs here
    |         |                                first mutable borrow occurs here
    |         first borrow later used here
Meta
  • cargo clippy -V: clippy 0.1.52 (9bc8c42b 2021-05-09)
  • rustc -Vv:
rustc 1.52.1 (9bc8c42bb 2021-05-09)
binary: rustc
commit-hash: 9bc8c42bb2f19e745a63f3445f1ac248fb015e53
commit-date: 2021-05-09
host: x86_64-apple-darwin
release: 1.52.1
LLVM version: 12.0.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the clone_on_copy lint implementation and reproduce the example with cargo clippy --all-targets. Check why removing clone creates the reported mutable-borrow error, then add a regression test covering this case and confirm the diagnostic no longer suggests the invalid change.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.