Skip to content

Commit

Permalink
add a test example of where transmutes_expressible_as_ptr_casts shoul…
Browse files Browse the repository at this point in the history
…d not suggest anything
  • Loading branch information
Ryan1729 committed Aug 9, 2020
1 parent a1ca125 commit 84db238
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
14 changes: 13 additions & 1 deletion tests/ui/transmutes_expressible_as_ptr_casts.fixed
Expand Up @@ -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.
Expand Down Expand Up @@ -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) }
}
14 changes: 13 additions & 1 deletion tests/ui/transmutes_expressible_as_ptr_casts.rs
Expand Up @@ -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.
Expand Down Expand Up @@ -75,3 +75,15 @@ fn main() {
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) }
}

0 comments on commit 84db238

Please sign in to comment.