You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
transmute_undefined_repr: no lint when transmuting into a single-field struct without #[repr(transparent)] #17869
transmute_undefined_repr doesn't fire on a transmute between a type and a single-field struct that has the default representation. A common case is &str to &Wrapper where struct Wrapper(str).
The layout of a repr(Rust) struct isn't guaranteed, even when it has only one field. The Reference says the default representation gives no guarantees about data layout. The Nomicon says only #[repr(C)] or #[repr(transparent)] give a layout you can rely on. So this transmute relies on how the compiler currently lays the type out, which is what this lint is meant to catch.
The cause is reduce_ty: a struct whose only non-zero-sized field is T reduces to T before the repr is checked. def.repr().inhibit_struct_field_reordering() is only checked for structs with more than one non-zero-sized field. Because of this, &str -> &Wrapper is analysed as &str -> &str.
This seems to be deliberate. #8503 described one-field structs as intentionally not linted, and #8547 made one-element tuples behave the same way. I'd like to suggest changing that: only treat a single-field struct as its field when it is #[repr(transparent)], or #[repr(C)] with that field first. In every other case, report an undefined layout as the lint does today. The lint is in nursery, so this seems like the right place to be strict, but I understand it may create a lot of new warnings in existing code (see #8496).
This came up in a real crate. A #[repr(transparent)] was missing on one of many otherwise identical wrapper types whose from_borrowed_unchecked transmutes &str -> &Self (ruma/ruma#2625). Nothing in clippy pointed it out.
Activity
bhirahatees-periyasamy commentedon Oct 13, 2026
@rustbot claim
bhirahatees-periyasamy commentedon Oct 13, 2026
@rustbot release-assignment