Skip to content

Bad suggestion for clone in field init shorthand #108470

Closed
@ehuss

Description

@ehuss

Code

struct Foo {
    t: Thing
}

#[derive(Clone)]
struct Thing;

fn main() {
    let t = &Thing;
    let f = Foo {
        t
    };
}

Current output

error[E0308]: mismatched types
  --> src/main.rs:11:9
   |
11 |         t
   |         ^ expected `Thing`, found `&Thing`
   |
help: consider using clone here
   |
11 |         t.clone()
   |          ++++++++

Desired output

error[E0308]: mismatched types
  --> src/main.rs:11:9
   |
11 |         t
   |         ^ expected `Thing`, found `&Thing`
   |
help: consider using clone here
   |
11 |         t: t.clone()
   |         ++  ++++++++

Rationale and extra context

The suggestion is not compatible with struct field init shorthand syntax. The suggestion should include the field name when rewriting.

Other cases

No response

Anything else?

rustc 1.69.0-nightly (c5c7d2b37 2023-02-24)
binary: rustc
commit-hash: c5c7d2b37780dac1092e75f12ab97dd56c30861d
commit-date: 2023-02-24
host: aarch64-apple-darwin
release: 1.69.0-nightly
LLVM version: 15.0.7

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