Auto merge of #8289 - jubnzv:unspecified-layout-union, r=camsteffen

Add `default_union_representation` lint

Closes #8235

changelog: Added a new lint  [`default_union_representation`]
This commit is contained in:
bors 2022-01-29 10:58:16 +00:00
commit 7ceffdee9b
7 changed files with 236 additions and 0 deletions

View file

@ -0,0 +1,78 @@
#![feature(transparent_unions)]
#![warn(clippy::default_union_representation)]
union NoAttribute {
a: i32,
b: u32,
}
#[repr(C)]
union ReprC {
a: i32,
b: u32,
}
#[repr(packed)]
union ReprPacked {
a: i32,
b: u32,
}
#[repr(C, packed)]
union ReprCPacked {
a: i32,
b: u32,
}
#[repr(C, align(32))]
union ReprCAlign {
a: i32,
b: u32,
}
#[repr(align(32))]
union ReprAlign {
a: i32,
b: u32,
}
union SingleZST {
f0: (),
}
union ZSTsAndField1 {
f0: u32,
f1: (),
f2: (),
f3: (),
}
union ZSTsAndField2 {
f0: (),
f1: (),
f2: u32,
f3: (),
}
union ZSTAndTwoFields {
f0: u32,
f1: u64,
f2: (),
}
#[repr(C)]
union CZSTAndTwoFields {
f0: u32,
f1: u64,
f2: (),
}
#[repr(transparent)]
union ReprTransparent {
a: i32,
}
#[repr(transparent)]
union ReprTransparentZST {
a: i32,
b: (),
}
fn main() {}

View file

@ -0,0 +1,48 @@
error: this union has the default representation
--> $DIR/default_union_representation.rs:4:1
|
LL | / union NoAttribute {
LL | | a: i32,
LL | | b: u32,
LL | | }
| |_^
|
= note: `-D clippy::default-union-representation` implied by `-D warnings`
= help: consider annotating `NoAttribute` with `#[repr(C)]` to explicitly specify memory layout
error: this union has the default representation
--> $DIR/default_union_representation.rs:16:1
|
LL | / union ReprPacked {
LL | | a: i32,
LL | | b: u32,
LL | | }
| |_^
|
= help: consider annotating `ReprPacked` with `#[repr(C)]` to explicitly specify memory layout
error: this union has the default representation
--> $DIR/default_union_representation.rs:34:1
|
LL | / union ReprAlign {
LL | | a: i32,
LL | | b: u32,
LL | | }
| |_^
|
= help: consider annotating `ReprAlign` with `#[repr(C)]` to explicitly specify memory layout
error: this union has the default representation
--> $DIR/default_union_representation.rs:54:1
|
LL | / union ZSTAndTwoFields {
LL | | f0: u32,
LL | | f1: u64,
LL | | f2: (),
LL | | }
| |_^
|
= help: consider annotating `ZSTAndTwoFields` with `#[repr(C)]` to explicitly specify memory layout
error: aborting due to 4 previous errors