Clippy lints / Restriction
Clippy lint · Restriction · checked with Clippy 0.1.99
clippy::default_union_representation: this union has the default representation
What it meansDisplays a warning when a union is declared with the default representation (without a #[repr(C)] attribute). It is in the restriction group and off by default.
What you see
warning: this union has the default representation --> main.rs:1:1 | 1 | / union Foo { 2 | | a: i32, 3 | | b: u32, 4 | | } | |_^ | = help: consider annotating `Foo` with `#[repr(C)]` to explicitly specify memory layout = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#default_union_representation = note: requested on the command line with `-W clippy::default-union-representation`
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
Unions in Rust have unspecified layout by default, despite many people thinking that they lay out each field at the start of the union (like C does). That is, there are no guarantees about the offset of the fields for unions with multiple non-ZST fields without an explicitly specified layout. These cases may lead to undefined behavior in unsafe blocks.
Lints that forbid language features for specific codebases (no unwrap, no indexing...). Off by default; pick them one by one, never the whole group.
How to fix it
Clippy does not fix this one automatically. Change the code by hand:
Flagged
union Foo {
a: i32,
b: u32,
}
fn main() {
let _x: u32 = unsafe {
Foo { a: 0_i32 }.b // Undefined behavior: `b` is allowed to be padding
};
}
Write this instead
#[repr(C)]
union Foo {
a: i32,
b: u32,
}
fn main() {
let _x: u32 = unsafe {
Foo { a: 0_i32 }.b // Now defined behavior, this is just an i32 -> u32 transmute
};
}
Turn it off, or on
It is off by default. To use it, set it to warn or deny:
Whole crate (Cargo.toml)
[lints.clippy]
default_union_representation = "warn"In code
#![warn(clippy::default_union_representation)]One run
cargo clippy -- -W clippy::default-union-representationThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
What it does
Displays a warning when a union is declared with the default representation (without a #[repr(C)] attribute).
Why restrict this?
Unions in Rust have unspecified layout by default, despite many people thinking that they lay out each field at the start of the union (like C does). That is, there are no guarantees about the offset of the fields for unions with multiple non-ZST fields without an explicitly specified layout. These cases may lead to undefined behavior in unsafe blocks.
Example
union Foo {
a: i32,
b: u32,
}
fn main() {
let _x: u32 = unsafe {
Foo { a: 0_i32 }.b // Undefined behavior: `b` is allowed to be padding
};
}
Use instead:
#[repr(C)]
union Foo {
a: i32,
b: u32,
}
fn main() {
let _x: u32 = unsafe {
Foo { a: 0_i32 }.b // Now defined behavior, this is just an i32 -> u32 transmute
};
}
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- unwrap_usedChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on Options
- module_name_repetitionsDetects public item names that are prefixed or suffixed by the containing public module’s name
- expect_usedChecks for .expect() or .expect_err() calls on Results and .expect() call on Options
- dbg_macroChecks for usage of the dbg! macro
- decimal_literal_representationWarns if there is a better representation for a numeric literal
- default_numeric_fallbackdefault numeric fallback might occur
- definition_in_module_rootChecks for definitions (structs, functions, traits, etc.) in mod.rs files
- deref_by_slicingslicing when dereferencing would work
- disallowed_script_identsidentifier `счётчик` has a Unicode script that is not allowed by configuration: Cyrillic
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::unwrap_usedChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on OptionsRead it next →Did this answer your question?