Skip to content

Incorrect suggestion to derive Clone on Vec directly #134471

Open
@cyrgani

Description

@cyrgani

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

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions