Static rustc_lint_defs::builtin::UNALIGNED_REFERENCES
source · pub static UNALIGNED_REFERENCES: &Lint
Expand description
The unaligned_references
lint detects unaligned references to fields
of packed structs.
Example
ⓘ
#[repr(packed)]
pub struct Foo {
field1: u64,
field2: u8,
}
fn main() {
unsafe {
let foo = Foo { field1: 0, field2: 0 };
let _ = &foo.field1;
println!("{}", foo.field1); // An implicit `&` is added here, triggering the lint.
}
}
{{produces}}
Explanation
Creating a reference to an insufficiently aligned packed field is undefined behavior and
should be disallowed. Using an unsafe
block does not change anything about this. Instead,
the code should do a copy of the data in the packed field or use raw pointers and unaligned
accesses. See issue #82523 for more information.