Skip to content

struct initialization incorrectly suggests using assoc function #115992

Closed
@lcnr

Description

@lcnr

Code

struct Foo {
    field: u32,
}

impl Foo {
    fn field(&self) -> u32 {
        self.field
    }

    fn new() -> Foo {
        Foo { field }
    }
}

Current output

error[E0425]: cannot find value `field` in this scope
  --> src/lib.rs:11:15
   |
11 |         Foo { field }
   |               ^^^^^ a field by this name exists in `Self`
   |
help: consider using the associated function
   |
11 |         Foo { self.field }
   |               +++++

We should not suggest using self.field inside of a struct initializer.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.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