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

explicit_auto_deref interaction with ManuallyDrop #11474

Closed
talagrand opened this issue Sep 8, 2023 · 0 comments · Fixed by #11477
Closed

explicit_auto_deref interaction with ManuallyDrop #11474

talagrand opened this issue Sep 8, 2023 · 0 comments · Fixed by #11477
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

@talagrand
Copy link
Contributor

Summary

Explicit derefs are recommended when using ManuallyDrop types, but the lint does not factor this in when recommending to auto deref.

Lint Name

explicit_auto_deref

Reproducer

Sample repro:

pub struct Variant {
    pub anonymous: Variant0,
}
pub union Variant0 {
    pub anonymous: std::mem::ManuallyDrop<Variant00>,
}

pub struct Variant00 {
    pub anonymous: Variant000,
}

pub union Variant000 {
    pub val: i32,
}

fn main() {
    unsafe {
        let mut p = core::mem::zeroed::<Variant>();
        (*p.anonymous.anonymous).anonymous.val = 1;
    }
}

Warning produced:

warning: deref which would be done by auto-deref
  --> src\main.rs:19:9
   |
19 |         (*p.anonymous.anonymous).anonymous.val = 1;
   |         ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `p.anonymous.anonymous`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#explicit_auto_deref
   = note: `#[warn(clippy::explicit_auto_deref)]` on by default

When attempting to fix this we get:

error: not automatically applying `DerefMut` on `ManuallyDrop` union field
  --> src\main.rs:19:9
   |
19 |         p.anonymous.anonymous.anonymous.val = 1;
   |         ^^^^^^^^^^^^^^^^^^^^^
   |
   = help: writing to this reference calls the destructor for the old value
   = help: add an explicit `*` if that is desired, or call `ptr::write` to not run the destructor

So it seems like this case should be ignored by the rule, given the recommendation to explicitly deref.

Version

rustc 1.70.0-dev
binary: rustc
commit-hash: 23b6f0bea6
commit-date: 2023-06-19
host: x86_64-pc-windows-msvc
release: 1.70.0-dev
LLVM version: 16.0.2

Additional Labels

No response

@talagrand talagrand 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 Sep 8, 2023
@bors bors closed this as completed in 98363cb Sep 11, 2023
jonboh pushed a commit to jonboh/rust-clippy that referenced this issue Sep 12, 2023
Auto deref does not apply on union field

changelog: [`explicit_auto_deref`]: do not suggest propose to auto-dereference an union field

Fix rust-lang#11474
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.

1 participant