Skip to content

Commit

Permalink
Auto merge of rust-lang#11849 - GuillaumeGomez:add-more-transmute_ref…
Browse files Browse the repository at this point in the history
…_to_ref-tests, r=blyxyas

Add tests for issues rust-lang#10285, rust-lang#10286, rust-lang#10289, rust-lang#10287

Fixes rust-lang#10285.
Fixes rust-lang#10286.
Fixes rust-lang#10289.
Fixes rust-lang#10287.

This PR simply adds tests for the listed issues as they're already implemented so we can close them.

r? `@blyxyas`

changelog:none
  • Loading branch information
bors committed Nov 22, 2023
2 parents a8b0e5f + 2fa87fb commit b21c9d4
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
3 changes: 3 additions & 0 deletions tests/ui/transmute_ptr_to_ptr.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ fn transmute_ptr_to_ptr() {
//~^ ERROR: transmute from a reference to a reference
let _: &GenericParam<f32> = &*(&GenericParam { t: 1u32 } as *const GenericParam<u32> as *const GenericParam<f32>);
//~^ ERROR: transmute from a reference to a reference
let u8_ref: &u8 = &0u8;
let u64_ref: &u64 = unsafe { &*(u8_ref as *const u8 as *const u64) };
//~^ ERROR: transmute from a reference to a reference
}

// these are recommendations for solving the above; if these lint we need to update
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/transmute_ptr_to_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ fn transmute_ptr_to_ptr() {
//~^ ERROR: transmute from a reference to a reference
let _: &GenericParam<f32> = std::mem::transmute(&GenericParam { t: 1u32 });
//~^ ERROR: transmute from a reference to a reference
let u8_ref: &u8 = &0u8;
let u64_ref: &u64 = unsafe { std::mem::transmute(u8_ref) };
//~^ ERROR: transmute from a reference to a reference
}

// these are recommendations for solving the above; if these lint we need to update
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/transmute_ptr_to_ptr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,11 @@ error: transmute from a reference to a reference
LL | let _: &GenericParam<f32> = std::mem::transmute(&GenericParam { t: 1u32 });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(&GenericParam { t: 1u32 } as *const GenericParam<u32> as *const GenericParam<f32>)`

error: aborting due to 6 previous errors
error: transmute from a reference to a reference
--> $DIR/transmute_ptr_to_ptr.rs:47:38
|
LL | let u64_ref: &u64 = unsafe { std::mem::transmute(u8_ref) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(u8_ref as *const u8 as *const u64)`

error: aborting due to 7 previous errors

18 changes: 18 additions & 0 deletions tests/ui/transmute_ref_to_ref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//@no-rustfix

#![deny(clippy::transmute_ptr_to_ptr)]
#![allow(dead_code)]

fn main() {
unsafe {
let single_u64: &[u64] = &[0xDEAD_BEEF_DEAD_BEEF];
let bools: &[bool] = unsafe { std::mem::transmute(single_u64) };
//~^ ERROR: transmute from a reference to a reference
let a: &[u32] = &[0x12345678, 0x90ABCDEF, 0xFEDCBA09, 0x87654321];
let b: &[u8] = unsafe { std::mem::transmute(a) };
//~^ ERROR: transmute from a reference to a reference
let bytes = &[1u8, 2u8, 3u8, 4u8] as &[u8];
let alt_slice: &[u32] = unsafe { core::mem::transmute(bytes) };
//~^ ERROR: transmute from a reference to a reference
}
}
26 changes: 26 additions & 0 deletions tests/ui/transmute_ref_to_ref.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
error: transmute from a reference to a reference
--> $DIR/transmute_ref_to_ref.rs:9:39
|
LL | let bools: &[bool] = unsafe { std::mem::transmute(single_u64) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(single_u64 as *const [u64] as *const [bool])`
|
note: the lint level is defined here
--> $DIR/transmute_ref_to_ref.rs:3:9
|
LL | #![deny(clippy::transmute_ptr_to_ptr)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: transmute from a reference to a reference
--> $DIR/transmute_ref_to_ref.rs:12:33
|
LL | let b: &[u8] = unsafe { std::mem::transmute(a) };
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(a as *const [u32] as *const [u8])`

error: transmute from a reference to a reference
--> $DIR/transmute_ref_to_ref.rs:15:42
|
LL | let alt_slice: &[u32] = unsafe { core::mem::transmute(bytes) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(bytes as *const [u8] as *const [u32])`

error: aborting due to 3 previous errors

0 comments on commit b21c9d4

Please sign in to comment.