Skip to content

Incorrect suggestion to derive Clone on Vec directly #134471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
cyrgani opened this issue Dec 18, 2024 · 1 comment
Open

Incorrect suggestion to derive Clone on Vec directly #134471

cyrgani opened this issue Dec 18, 2024 · 1 comment
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@cyrgani
Copy link
Contributor

cyrgani commented Dec 18, 2024

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

@cyrgani cyrgani added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 18, 2024
@jieyouxu jieyouxu added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Dec 18, 2024
@cyrgani
Copy link
Contributor Author

cyrgani commented Dec 20, 2024

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants