Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

field_reassign_with_default lint suggest invalid code #6344

Closed
krojew opened this issue Nov 18, 2020 · 0 comments · Fixed by #6375
Closed

field_reassign_with_default lint suggest invalid code #6344

krojew opened this issue Nov 18, 2020 · 0 comments · Fixed by #6375
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@krojew
Copy link

krojew commented Nov 18, 2020

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
    
@krojew krojew added the C-bug Category: Clippy is not doing the correct thing label Nov 18, 2020
@phansch phansch added the I-false-positive Issue: The lint was triggered on code it shouldn't have label Dec 18, 2020
@phansch phansch self-assigned this Dec 18, 2020
@bors bors closed this as completed in 3661848 Dec 27, 2020
flip1995 pushed a commit to flip1995/rust-clippy that referenced this issue Jan 2, 2021
…=flip1995

Reassign default private

changelog: fix field_reassign_with_default false positive

* Fix rust-lang#6344
* Fix assumption that `field: Default::default()` is the same as `..Default::default()`
* Cleanup some redundant logic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants