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