Skip to content

Commit 47c9ed6

Browse files
committed
Fix checking transmutes with adjusted types
1 parent 911e105 commit 47c9ed6

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

clippy_lints/src/transmute/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,8 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
415415
// And see https://github.com/rust-lang/rust/issues/51911 for dereferencing raw pointers.
416416
let const_context = in_constant(cx, e.hir_id);
417417

418-
let from_ty = cx.typeck_results().expr_ty(arg);
418+
let from_ty = cx.typeck_results().expr_ty_adjusted(arg);
419+
// Adjustments for `to_ty` happen after the call to `transmute`, so don't use them.
419420
let to_ty = cx.typeck_results().expr_ty(e);
420421

421422
// If useless_transmute is triggered, the other lints can be skipped.

tests/ui/transmute_undefined_repr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,7 @@ fn main() {
106106

107107
let _: MaybeUninit<Ty2<u32, u32>> = transmute(value::<Ty2<u32, u32>>()); // Ok
108108
let _: Ty2<u32, u32> = transmute(value::<MaybeUninit<Ty2<u32, u32>>>()); // Ok
109+
110+
let _: Ty<&[u32]> = transmute::<&[u32], _>(value::<&Vec<u32>>()); // Ok
109111
}
110112
}

tests/ui/transmutes_expressible_as_ptr_casts.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ error: transmute from a reference to a pointer
3434
LL | let _array_ptr_transmute = unsafe { transmute::<&[i32; 4], *const [i32; 4]>(array_ref) };
3535
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `array_ref as *const [i32; 4]`
3636

37-
error: transmute from `fn(usize) -> u8 {main::foo}` to `*const usize` which could be expressed as a pointer cast instead
37+
error: transmute from `fn(usize) -> u8` to `*const usize` which could be expressed as a pointer cast instead
3838
--> $DIR/transmutes_expressible_as_ptr_casts.rs:48:41
3939
|
4040
LL | let _usize_ptr_transmute = unsafe { transmute::<fn(usize) -> u8, *const usize>(foo) };
4141
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `foo as *const usize`
4242

43-
error: transmute from `fn(usize) -> u8 {main::foo}` to `usize` which could be expressed as a pointer cast instead
43+
error: transmute from `fn(usize) -> u8` to `usize` which could be expressed as a pointer cast instead
4444
--> $DIR/transmutes_expressible_as_ptr_casts.rs:52:49
4545
|
4646
LL | let _usize_from_fn_ptr_transmute = unsafe { transmute::<fn(usize) -> u8, usize>(foo) };

0 commit comments

Comments
 (0)