]> git.lizzy.rs Git - rust.git/blob - tests/ui/cast_slice_different_sizes.rs
Auto merge of #8665 - InfRandomness:option_take_on_temporary, r=llogiq
[rust.git] / tests / ui / cast_slice_different_sizes.rs
1 #![allow(clippy::let_unit_value)]
2
3 fn main() {
4     let x: [i32; 3] = [1_i32, 2, 3];
5     let r_x = &x;
6     // Check casting through multiple bindings
7     // Because it's separate, it does not check the cast back to something of the same size
8     let a = r_x as *const [i32];
9     let b = a as *const [u8];
10     let c = b as *const [u32];
11
12     // loses data
13     let loss = r_x as *const [i32] as *const [u8];
14
15     // Cast back to same size but different type loses no data, just type conversion
16     // This is weird code but there's no reason for this lint specifically to fire *twice* on it
17     let restore = r_x as *const [i32] as *const [u8] as *const [u32];
18
19     // Check casting through blocks is detected
20     let loss_block_1 = { r_x as *const [i32] } as *const [u8];
21     let loss_block_2 = {
22         let _ = ();
23         r_x as *const [i32]
24     } as *const [u8];
25
26     // Check that resores of the same size are detected through blocks
27     let restore_block_1 = { r_x as *const [i32] } as *const [u8] as *const [u32];
28     let restore_block_2 = { ({ r_x as *const [i32] }) as *const [u8] } as *const [u32];
29     let restore_block_3 = {
30         let _ = ();
31         ({
32             let _ = ();
33             r_x as *const [i32]
34         }) as *const [u8]
35     } as *const [u32];
36
37     // Check that the result of a long chain of casts is detected
38     let long_chain_loss = r_x as *const [i32] as *const [u32] as *const [u16] as *const [i8] as *const [u8];
39     let long_chain_restore =
40         r_x as *const [i32] as *const [u32] as *const [u16] as *const [i8] as *const [u8] as *const [u32];
41 }