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 could be false positive #6312

Closed
Fullstop000 opened this issue Nov 9, 2020 · 3 comments · Fixed by #7794
Closed

field_reassign_with_default could be false positive #6312

Fullstop000 opened this issue Nov 9, 2020 · 3 comments · Fixed by #7794
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@Fullstop000
Copy link

Fullstop000 commented Nov 9, 2020

I tried this code:

#[derive(Clone, Default)]
pub struct FileNode {
    name: String,
    delay_data_sync: Arc<AtomicBool>,
}

impl Drop for FileNode {
    fn drop(&mut self) {
        self.close()
    }
}

impl FileNode {
    fn new(name: &str) -> Self {
        let mut f = FileNode::default();
        f.name = name.to_owned();
        f
    }
    
    fn close(&self) {}
}

I expected to see this happen:
Clippy reports ok as FileNode implements Drop manually

Instead, this happened:
Clippy complains about field assignment outside of initializer for an instance created with Default::default(). If refactoring the codes as the suggestion clippy provides, rustc just throws the error:

cannot move out of type `FileNode`, which implements the `Drop` trait
move occurs because value has type `Arc<AtomicBool>`, which does not implement the `Copy` trait rustc(E0509)

Meta

  • cargo clippy -V: clippy 0.0.212 (1773f60 2020-11-08)
  • rustc -Vv:
rustc 1.49.0-nightly (1773f60ea 2020-11-08)
binary: rustc
commit-hash: 1773f60ea5d42e86b8fdf78d2fc5221ead222bc1
commit-date: 2020-11-08
host: x86_64-unknown-linux-gnu
release: 1.49.0-nightly
@Fullstop000 Fullstop000 added the C-bug Category: Clippy is not doing the correct thing label Nov 9, 2020
@flip1995
Copy link
Member

flip1995 commented Nov 9, 2020

Can you provide an example on playground? I don't get this error after applying the lint: Playground

@Fullstop000
Copy link
Author

@ebroto
Copy link
Member

ebroto commented Nov 10, 2020

Indeed, the Drop part is important. We hit E0509 because functional record update moves or copies the remaining fields. Since Arc<AtomicBool> does not implement Copy we are moving out of the struct that implements Drop.

To fix this, we should avoid linting if the type implements Drop and has any field which is not Copy.

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 good-first-issue These issues are a good way to get started with Clippy I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
4 participants