Skip to content

explicit_auto_deref interaction with ManuallyDrop #11474

@talagrand

Description

@talagrand

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

Metadata

Metadata

Assignees

No one assigned

    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