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 on struct with #[non_exhaustive] or private fields is false positive #6559

Closed
bryanburgers opened this issue Jan 7, 2021 · 3 comments
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

@bryanburgers
Copy link

bryanburgers commented Jan 7, 2021

Lint name: field_reassign_with_default

Given a library that contains a struct with a #[non_exhaustive] attribute on it:

#[derive(Default)]
#[non_exhaustive]
pub struct TestStruct {
    pub one: usize,
    pub two: usize,
}

Code like this causes a clippy warning:

fn main() {
    let mut t = the_lib::TestStruct::default();
    t.one = 1;
    t.two = 2;
    println!("{} {}", t.one, t.two);
}

Clippy would prefer to see the following code, which is a compiler error.

fn main() {
    let t = the_lib::TestStruct {
        one: 1,
        two: 2,
        ..the_lib::TestStruct::default()
    };
    println!("{} {}", t.one, t.two);
}
cannot create non-exhaustive struct using struct expression

Note that due to the rules of #[non_exhaustive] application, this is only an error when the struct is defined in a different crate (which is why I'm not able to provide a playground link).

The attribute #[non_exhaustive], when attached to a struct or the variant of an enum, will prevent code outside of the crate defining it from constructing said struct or variant.

Rust 1.40.0 release notes

Meta

  • cargo clippy -V: clippy 0.0.212 (e1884a8e 2020-12-29)
  • rustc -Vv:
    rustc 1.49.0 (e1884a8e3 2020-12-29)
    binary: rustc
    commit-hash: e1884a8e3c3e813aada8254edfa120e85bf5ffca
    commit-date: 2020-12-29
    host: x86_64-apple-darwin
    release: 1.49.0
    
@bryanburgers bryanburgers added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jan 7, 2021
@bryanburgers
Copy link
Author

Hmm... actually this affects structs within the same crate that have private fields, as well.

mod the_mod {
    #[derive(Default)]
    pub struct OtherTestStruct {
        pub one: usize,
        pub two: usize,
        three: usize,
    }
}

fn main() {
    // Clippy warning
    let mut t = the_mod::OtherTestStruct::default();
    t.one = 1;
    t.two = 2;
    println!("{} {}", t.one, t.two);

    // Compiler error
    /*
    let t = the_mod::OtherTestStruct {
        one: 1,
        two: 2,
        ..the_mod::OtherTestStruct::default()
    };
    println!("{} {}", t.one, t.two);
    */
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=993abe21260563a8c5a12c4383a54d11
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=93692333032b262ba980aaaa311726d6

@bryanburgers
Copy link
Author

Relevant rust issue: rust-lang/rust#70564
Related clippy issue: #6312

@bryanburgers bryanburgers changed the title field_reassign_with_default on non_exhaustive struct is false positive field_reassign_with_default on struct with #[non_exhaustive] or private fields is false positive Jan 7, 2021
lovasoa added a commit to lovasoa/dezoomify-rs that referenced this issue Jan 29, 2021
@Manishearth
Copy link
Member

Fixed in #6375

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

No branches or pull requests

2 participants