Warn on unions with no #[repr]
attribute
#8235
Labels
A-lint
Area: New lints
E-medium
Call for participation: Medium difficulty level problem and requires some initial experience.
L-pedantic
Lint: Belongs in the pedantic lint group
What it does
Unions in rust have unspecified layout by default, despite many people thinking that they lay out each type at the start of the union (like C does)
Lint Name
unspecified_layout_union
Category
pedantic
Advantage
If the user intended the union to be
#[repr(C)]
because they are using it to read fields other than the most recently written one, the code becomes defined behaviour.If the user did not intend any specific representation because they are using it in an externally tagged way, the
#[allow(unspecified_layout_union)]
calls this out specifically.Drawbacks
Some unions don't rely on the representation being C-like, and this may be the majority of the unions that have no explicit representation.
This lint, once added, would fire on all of these unions, as it's currently impossible to specify the rust repr explicitly. This might be a source of noise. I originally thought about
suspicious
as a category for this lint, but downgraded it topedantic
because of that.Example
Could be written as:
The text was updated successfully, but these errors were encountered: