Skip to content

Clippy suggests broken code when modifying union fields behind ManuallyDrop #14386

Open
@abgros

Description

@abgros

Summary

Modifying a union field that is behind ManuallyDrop incorrectly triggers the "immediately dereferencing a reference" lint, causing Clippy to suggest broken code.

Reproducer

use std::mem::ManuallyDrop;

struct Data {
	num: u64,
}

union DataWithPadding {
	data: ManuallyDrop<Data>,
	with_padding: [u8; size_of::<Data>()],
}

fn main() {
	let mut a = DataWithPadding {
		with_padding: [0; size_of::<DataWithPadding>()],
	};
	unsafe {
		(*(&raw mut a.data)).num = 42;
		// Clippy suggests `(a.data).num = 42;`, which doesn't work

		println!("{:?}", a.with_padding);
	}
}

Version

rustc 1.87.0-nightly (8c392966a 2025-03-01)
binary: rustc
commit-hash: 8c392966a013fd8a09e6b78b3c8d6e442bc278e1
commit-date: 2025-03-01
host: x86_64-pc-windows-msvc
release: 1.87.0-nightly
LLVM version: 20.1.0

Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions