Skip to content

Commit b21c9d4

Browse files
committed
Auto merge of rust-lang#11849 - GuillaumeGomez:add-more-transmute_ref_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
2 parents a8b0e5f + 2fa87fb commit b21c9d4

5 files changed

+57
-1
lines changed

tests/ui/transmute_ptr_to_ptr.fixed

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ fn transmute_ptr_to_ptr() {
4343
//~^ ERROR: transmute from a reference to a reference
4444
let _: &GenericParam<f32> = &*(&GenericParam { t: 1u32 } as *const GenericParam<u32> as *const GenericParam<f32>);
4545
//~^ ERROR: transmute from a reference to a reference
46+
let u8_ref: &u8 = &0u8;
47+
let u64_ref: &u64 = unsafe { &*(u8_ref as *const u8 as *const u64) };
48+
//~^ ERROR: transmute from a reference to a reference
4649
}
4750

4851
// these are recommendations for solving the above; if these lint we need to update

tests/ui/transmute_ptr_to_ptr.rs

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ fn transmute_ptr_to_ptr() {
4343
//~^ ERROR: transmute from a reference to a reference
4444
let _: &GenericParam<f32> = std::mem::transmute(&GenericParam { t: 1u32 });
4545
//~^ ERROR: transmute from a reference to a reference
46+
let u8_ref: &u8 = &0u8;
47+
let u64_ref: &u64 = unsafe { std::mem::transmute(u8_ref) };
48+
//~^ ERROR: transmute from a reference to a reference
4649
}
4750

4851
// these are recommendations for solving the above; if these lint we need to update

tests/ui/transmute_ptr_to_ptr.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,11 @@ error: transmute from a reference to a reference
3737
LL | let _: &GenericParam<f32> = std::mem::transmute(&GenericParam { t: 1u32 });
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(&GenericParam { t: 1u32 } as *const GenericParam<u32> as *const GenericParam<f32>)`
3939

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

tests/ui/transmute_ref_to_ref.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//@no-rustfix
2+
3+
#![deny(clippy::transmute_ptr_to_ptr)]
4+
#![allow(dead_code)]
5+
6+
fn main() {
7+
unsafe {
8+
let single_u64: &[u64] = &[0xDEAD_BEEF_DEAD_BEEF];
9+
let bools: &[bool] = unsafe { std::mem::transmute(single_u64) };
10+
//~^ ERROR: transmute from a reference to a reference
11+
let a: &[u32] = &[0x12345678, 0x90ABCDEF, 0xFEDCBA09, 0x87654321];
12+
let b: &[u8] = unsafe { std::mem::transmute(a) };
13+
//~^ ERROR: transmute from a reference to a reference
14+
let bytes = &[1u8, 2u8, 3u8, 4u8] as &[u8];
15+
let alt_slice: &[u32] = unsafe { core::mem::transmute(bytes) };
16+
//~^ ERROR: transmute from a reference to a reference
17+
}
18+
}

tests/ui/transmute_ref_to_ref.stderr

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: transmute from a reference to a reference
2+
--> $DIR/transmute_ref_to_ref.rs:9:39
3+
|
4+
LL | let bools: &[bool] = unsafe { std::mem::transmute(single_u64) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(single_u64 as *const [u64] as *const [bool])`
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/transmute_ref_to_ref.rs:3:9
9+
|
10+
LL | #![deny(clippy::transmute_ptr_to_ptr)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: transmute from a reference to a reference
14+
--> $DIR/transmute_ref_to_ref.rs:12:33
15+
|
16+
LL | let b: &[u8] = unsafe { std::mem::transmute(a) };
17+
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(a as *const [u32] as *const [u8])`
18+
19+
error: transmute from a reference to a reference
20+
--> $DIR/transmute_ref_to_ref.rs:15:42
21+
|
22+
LL | let alt_slice: &[u32] = unsafe { core::mem::transmute(bytes) };
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(bytes as *const [u8] as *const [u32])`
24+
25+
error: aborting due to 3 previous errors
26+

0 commit comments

Comments
 (0)