Skip to content

field_reassign_with_default lint suggest invalid code #6344

@krojew

Description

@krojew

I tried this code:

// in crate X
#[derive(Default)]
struct Foo {
    pub x: i8,
    pub y: i8,
    z: i8
}

// in crate Y:

let mut foo = Foo::default();
foo.x = 1;

I expected to see this happen: no field_reassign_with_default warning

Instead, this happened:

Field assignment outside of initializer for an instance created with Default::default()

Suggestion implies the following:

let foo = Foo {
    x: 1,
    ..Default::default()
};

This doesn't compile, since z is private and Foo cannot be initialized in this way.

Meta

  • cargo clippy -V: clippy 0.0.212 (5404efc 2020-11-11)
  • rustc -Vv:
    rustc 1.49.0-nightly (5404efc28 2020-11-11)
    binary: rustc
    commit-hash: 5404efc28a0cddee103ef6396c48ea71ff9631c8
    commit-date: 2020-11-11
    host: x86_64-pc-windows-msvc
    release: 1.49.0-nightly
    

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions