Skip to content

Commit 4931cab

Browse files
committed
Auto merge of #8425 - Jarcho:transmute_8417, r=giraffate
Fix `transmute_undefined_repr` with single field `#[repr(C)]` structs Fixes: #8417 The description has also been made more precise. changelog: Fix `transmute_undefined_repr` with single field `#[repr(C)]` structs changelog: Move `transmute_undefined_repr` back to `correctness`
2 parents 41c7004 + 662df33 commit 4931cab

File tree

6 files changed

+14
-7
lines changed

6 files changed

+14
-7
lines changed

clippy_lints/src/lib.register_all.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
277277
LintId::of(transmute::TRANSMUTE_INT_TO_FLOAT),
278278
LintId::of(transmute::TRANSMUTE_NUM_TO_BYTES),
279279
LintId::of(transmute::TRANSMUTE_PTR_TO_REF),
280+
LintId::of(transmute::TRANSMUTE_UNDEFINED_REPR),
280281
LintId::of(transmute::UNSOUND_COLLECTION_TRANSMUTE),
281282
LintId::of(transmute::WRONG_TRANSMUTE),
282283
LintId::of(transmuting_null::TRANSMUTING_NULL),

clippy_lints/src/lib.register_correctness.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ store.register_group(true, "clippy::correctness", Some("clippy_correctness"), ve
5858
LintId::of(size_of_in_element_count::SIZE_OF_IN_ELEMENT_COUNT),
5959
LintId::of(swap::ALMOST_SWAPPED),
6060
LintId::of(to_string_in_display::TO_STRING_IN_DISPLAY),
61+
LintId::of(transmute::TRANSMUTE_UNDEFINED_REPR),
6162
LintId::of(transmute::UNSOUND_COLLECTION_TRANSMUTE),
6263
LintId::of(transmute::WRONG_TRANSMUTE),
6364
LintId::of(transmuting_null::TRANSMUTING_NULL),

clippy_lints/src/lib.register_nursery.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ store.register_group(true, "clippy::nursery", Some("clippy_nursery"), vec![
2626
LintId::of(strings::STRING_LIT_AS_BYTES),
2727
LintId::of(suspicious_operation_groupings::SUSPICIOUS_OPERATION_GROUPINGS),
2828
LintId::of(trailing_empty_array::TRAILING_EMPTY_ARRAY),
29-
LintId::of(transmute::TRANSMUTE_UNDEFINED_REPR),
3029
LintId::of(transmute::USELESS_TRANSMUTE),
3130
LintId::of(use_self::USE_SELF),
3231
])

clippy_lints/src/transmute/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,8 @@ declare_clippy_lint! {
358358

359359
declare_clippy_lint! {
360360
/// ### What it does
361-
/// Checks for transmutes either to or from a type which does not have a defined representation.
361+
/// Checks for transmutes between types which do not have a representation defined relative to
362+
/// each other.
362363
///
363364
/// ### Why is this bad?
364365
/// The results of such a transmute are not defined.
@@ -376,7 +377,7 @@ declare_clippy_lint! {
376377
/// ```
377378
#[clippy::version = "1.60.0"]
378379
pub TRANSMUTE_UNDEFINED_REPR,
379-
nursery,
380+
correctness,
380381
"transmute to or from a type with an undefined representation"
381382
}
382383

clippy_lints/src/transmute/transmute_undefined_repr.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,6 @@ fn reduce_ty<'tcx>(cx: &LateContext<'tcx>, mut ty: Ty<'tcx>) -> ReducedTy<'tcx>
255255
ReducedTy::UnorderedFields(ty)
256256
},
257257
ty::Adt(def, substs) if def.is_struct() => {
258-
if def.repr.inhibit_struct_field_reordering_opt() {
259-
return ReducedTy::OrderedFields(ty);
260-
}
261258
let mut iter = def
262259
.non_enum_variant()
263260
.fields
@@ -270,7 +267,11 @@ fn reduce_ty<'tcx>(cx: &LateContext<'tcx>, mut ty: Ty<'tcx>) -> ReducedTy<'tcx>
270267
ty = sized_ty;
271268
continue;
272269
}
273-
ReducedTy::UnorderedFields(ty)
270+
if def.repr.inhibit_struct_field_reordering_opt() {
271+
ReducedTy::OrderedFields(ty)
272+
} else {
273+
ReducedTy::UnorderedFields(ty)
274+
}
274275
},
275276
ty::Ref(..) | ty::RawPtr(_) => ReducedTy::Ref(ty),
276277
_ => ReducedTy::Other(ty),

tests/ui/transmute_undefined_repr.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,9 @@ fn main() {
4040

4141
let _: Ty<[u8; 8]> = core::mem::transmute(value::<Ty2<u32, i32>>()); // Ok, transmute to byte array
4242
let _: Ty2<u32, i32> = core::mem::transmute(value::<Ty<[u8; 8]>>()); // Ok, transmute from byte array
43+
44+
// issue #8417
45+
let _: Ty2C<Ty2<u32, i32>, ()> = core::mem::transmute(value::<Ty2<u32, i32>>()); // Ok, Ty2 types are the same
46+
let _: Ty2<u32, i32> = core::mem::transmute(value::<Ty2C<Ty2<u32, i32>, ()>>()); // Ok, Ty2 types are the same
4347
}
4448
}

0 commit comments

Comments
 (0)