add a test example of where transmutes_expressible_as_ptr_casts should not suggest anything

This commit is contained in:
Ryan1729 2020-08-09 00:15:56 -06:00
parent a1ca12581a
commit 84db238fa1
2 changed files with 26 additions and 2 deletions

View File

@ -5,7 +5,7 @@
#![warn(clippy::useless_transmute)]
#![warn(clippy::transmute_ptr_to_ptr)]
use std::mem::transmute;
use std::mem::{size_of, transmute};
// rustc_typeck::check::cast contains documentation about when a cast `e as U` is
// valid, which we quote from below.
@ -75,3 +75,15 @@ fn main() {
fn trigger_do_check_to_emit_error(in_param: &[i32; 1]) -> *const u8 {
unsafe { in_param as *const [i32; 1] as *const u8 }
}
#[repr(C)]
struct Single(u64);
#[repr(C)]
struct Pair(u32, u32);
fn cannot_be_expressed_as_pointer_cast(in_param: Single) -> Pair {
assert_eq!(size_of::<Single>(), size_of::<Pair>());
unsafe { transmute::<Single, Pair>(in_param) }
}

View File

@ -5,7 +5,7 @@
#![warn(clippy::useless_transmute)]
#![warn(clippy::transmute_ptr_to_ptr)]
use std::mem::transmute;
use std::mem::{size_of, transmute};
// rustc_typeck::check::cast contains documentation about when a cast `e as U` is
// valid, which we quote from below.
@ -75,3 +75,15 @@ fn foo(_: usize) -> u8 { 42 }
fn trigger_do_check_to_emit_error(in_param: &[i32; 1]) -> *const u8 {
unsafe { transmute::<&[i32; 1], *const u8>(in_param) }
}
#[repr(C)]
struct Single(u64);
#[repr(C)]
struct Pair(u32, u32);
fn cannot_be_expressed_as_pointer_cast(in_param: Single) -> Pair {
assert_eq!(size_of::<Single>(), size_of::<Pair>());
unsafe { transmute::<Single, Pair>(in_param) }
}