Skip to content

incorrect "consider annotating ContainsRc<T> with #[derive(Clone)]" #146515

@lcnr

Description

@lcnr
use std::rc::Rc;

#[derive(Clone)]
struct ContainsRc<T> {
    value: Rc<T>,
}

fn clone_me<T>(x: &ContainsRc<T>) -> ContainsRc<T> {
    x.clone()
}

results in

error[E0308]: mismatched types
 --> src/lib.rs:9:5
  |
8 | fn clone_me<T>(x: &ContainsRc<T>) -> ContainsRc<T> {
  |                                      ------------- expected `ContainsRc<T>` because of return type
9 |     x.clone()
  |     ^^^^^^^^^ expected `ContainsRc<T>`, found `&ContainsRc<T>`
  |
  = note: expected struct `ContainsRc<_>`
          found reference `&ContainsRc<_>`
note: `ContainsRc<T>` does not implement `Clone`, so `&ContainsRc<T>` was cloned instead
 --> src/lib.rs:9:5
  |
9 |     x.clone()
  |     ^
  = help: `Clone` is not implemented because the trait bound `T: Clone` is not satisfied
help: consider annotating `ContainsRc<T>` with `#[derive(Clone)]`
  |
4 + #[derive(Clone)]
5 | struct ContainsRc<T> {
  |

We should not suggest adding #[derive(Clone)] if the struct already derives Clone :> the issue is the unnecessary T: Clone constraint added by the derive.

Metadata

Metadata

Assignees

No one assigned

    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