rust-lang / rust-lang/rust

Incorrect suggestion to derive `Clone` on `Vec` directly

Open
#134,471 1 comment 0 reactions 1 assignee View on GitHub

@cyrgani is already working on this.

Since Dec 20, 2024.

A-diagnostics A-suggestion-diagnostics D-invalid-suggestion T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
pub struct NotClone {}

pub fn foo(v: &Vec<NotClone>) {
    let _v = v.clone();
}
Current output
warning: call to `.clone()` on a reference in this situation does nothing
 --> src/lib.rs:4:15
  |
4 |     let _v = v.clone();
  |               ^^^^^^^^
  |
  = note: the type `Vec<NotClone>` does not implement `Clone`, so calling `clone` on `&Vec<NotClone>` copies the reference, which does not do anything and can be removed
  = note: `#[warn(noop_method_call)]` on by default
help: remove this redundant call
  |
4 -     let _v = v.clone();
4 +     let _v = v;
  |
help: if you meant to clone `Vec<NotClone>`, implement `Clone` for it
 --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:397:1
  |
39+ #[derive(Clone)]
39| pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global> {
  |
Desired output
warning: call to `.clone()` on a reference in this situation does nothing
 --> src/lib.rs:4:15
  |
4 |     let _v = v.clone();
  |               ^^^^^^^^
  |
  = note: the type `Vec<NotClone>` does not implement `Clone`, so calling `clone` on `&Vec<NotClone>` copies the reference, which does not do anything and can be removed
  = note: `#[warn(noop_method_call)]` on by default
help: remove this redundant call
  |
4 -     let _v = v.clone();
4 +     let _v = v;
  |
help: if you meant to clone `Vec<NotClone>`, implement `Clone` for it
 --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:397:1
  |
1+ #[derive(Clone)]
2| pub struct NotClone {}
  |
Rationale and extra context

Encountered this while investigating around #134467. Obviously the derive is placed at the wrong place.

Other cases

Rust Version
1.85.0-nightly

(2024-12-17 a4cb3c831823d9baa56c)
Anything else?

No response

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.