Skip to content

No hint about implementing clone on type mismatch when cloning reference #95535

Closed
@Noratrieb

Description

@Noratrieb

Given the following code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=ebe1fe6ca75fd88cba9aa3a77cb625bf

struct NotClone;

fn main() {
    clone_thing(&NotClone);
}

fn clone_thing(nc: &NotClone) -> NotClone {
    nc.clone()
}

The current output is:

error[[E0308]](https://doc.rust-lang.org/nightly/error-index.html#E0308): mismatched types
 --> src/main.rs:8:5
  |
7 | fn clone_thing(nc: &NotClone) -> NotClone {
  |                                  -------- expected `NotClone` because of return type
8 |     nc.clone()
  |     ^^^^^^^^^^ expected struct `NotClone`, found `&NotClone`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error

Ideally the output should look like:

error[[E0308]](https://doc.rust-lang.org/nightly/error-index.html#E0308): mismatched types
 --> src/main.rs:8:5
  |
7 | fn clone_thing(nc: &NotClone) -> NotClone {
  |                                  -------- expected `NotClone` because of return type
8 |     nc.clone()
  |     ^^^^^^^^^^ expected struct `NotClone`, found `&NotClone`
  | note: `NotClone` does not implement clone, consider implementing it.

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-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