deref_addrof can suggest adding UB #13275
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
Summary
Reading/writing a place requires that place to be based on an aligned pointer, but taking the reference of a place only requires the referenced place to be aligned. These facts together mean that
{*&(*ptr).0}
is a semantically weaker operation than{(*ptr).0}
, butclippy::deref_addrof
will suggest changing the former to the latter, which potentially introduces UB to a program.This might be considered reasonable (i.e. not a false positive), as semantically relying on this is unreasonably subtle. However, the lint also still fires when using
&raw
, which should not be happening when the place is potentially based on a misaligned pointer.Note: the lint is still useful and should be fired for immediately dereferencing a pointer (
*&raw
) when the place is not based on a (potentially misaligned) pointer, as it is still an operational no-op when the place is based on an aligned pointer (or reference), and removing such allows*&raw
to semantically indicate lowering the implied alignment of a place.Lint Name
clippy::deref_addrof
Reproducer
I tried this code:
I saw this happen:
I expected to see this happen:
Applying the warnings' suggested fix should not introduce UB.
Version
Additional Labels
@rustbot label +I-suggestion-causes-ub
:cheeky:
The text was updated successfully, but these errors were encountered: