1 #![warn(clippy::size_of_in_element_count)]
2 #![allow(clippy::ptr_offset_with_cast)]
3 
4 use std::mem::{size_of, size_of_val};
5 use std::ptr::{copy, copy_nonoverlapping, write_bytes};
6 
main()7 fn main() {
8     const SIZE: usize = 128;
9     const HALF_SIZE: usize = SIZE / 2;
10     const DOUBLE_SIZE: usize = SIZE * 2;
11     let mut x = [2u8; SIZE];
12     let mut y = [2u8; SIZE];
13 
14     // Count expression involving multiplication of size_of (Should trigger the lint)
15     unsafe { copy_nonoverlapping(x.as_ptr(), y.as_mut_ptr(), size_of::<u8>() * SIZE) };
16 
17     // Count expression involving nested multiplications of size_of (Should trigger the lint)
18     unsafe { copy_nonoverlapping(x.as_ptr(), y.as_mut_ptr(), HALF_SIZE * size_of_val(&x[0]) * 2) };
19 
20     // Count expression involving divisions of size_of (Should trigger the lint)
21     unsafe { copy(x.as_ptr(), y.as_mut_ptr(), DOUBLE_SIZE * size_of::<u8>() / 2) };
22 
23     // Count expression involving divisions by size_of (Should not trigger the lint)
24     unsafe { copy(x.as_ptr(), y.as_mut_ptr(), DOUBLE_SIZE / size_of::<u8>()) };
25 
26     // Count expression involving divisions by multiple size_of (Should not trigger the lint)
27     unsafe { copy(x.as_ptr(), y.as_mut_ptr(), DOUBLE_SIZE / (2 * size_of::<u8>())) };
28 
29     // Count expression involving recursive divisions by size_of (Should trigger the lint)
30     unsafe { copy(x.as_ptr(), y.as_mut_ptr(), DOUBLE_SIZE / (2 / size_of::<u8>())) };
31 
32     // No size_of calls (Should not trigger the lint)
33     unsafe { copy(x.as_ptr(), y.as_mut_ptr(), SIZE) };
34 
35     // Different types for pointee and size_of (Should not trigger the lint)
36     unsafe { y.as_mut_ptr().write_bytes(0u8, size_of::<u16>() / 2 * SIZE) };
37 }
38