Do not trigger [size_of_in_element_count] for u8

Counting in bytes for a pointer to `u8` is legitimate and must not
trigger the lint. Also, this prevents linting the
`{std,core}::ptr::write_bytes` as it manipulates bytes.
This commit is contained in:
Samuel Tardieu 2025-01-17 00:17:13 +01:00
parent 8d0c0eb917
commit bbd58d19d4
5 changed files with 85 additions and 109 deletions

View file

@ -8,11 +8,11 @@ fn main() {
const SIZE: usize = 128;
const HALF_SIZE: usize = SIZE / 2;
const DOUBLE_SIZE: usize = SIZE * 2;
let mut x = [2u8; SIZE];
let mut y = [2u8; SIZE];
let mut x = [2u16; SIZE];
let mut y = [2u16; SIZE];
// Count expression involving multiplication of size_of (Should trigger the lint)
unsafe { copy_nonoverlapping(x.as_ptr(), y.as_mut_ptr(), size_of::<u8>() * SIZE) };
unsafe { copy_nonoverlapping(x.as_ptr(), y.as_mut_ptr(), size_of::<u16>() * SIZE) };
//~^ ERROR: found a count of bytes instead of a count of elements of `T`
// Count expression involving nested multiplications of size_of (Should trigger the lint)
@ -20,22 +20,19 @@ fn main() {
//~^ ERROR: found a count of bytes instead of a count of elements of `T`
// Count expression involving divisions of size_of (Should trigger the lint)
unsafe { copy(x.as_ptr(), y.as_mut_ptr(), DOUBLE_SIZE * size_of::<u8>() / 2) };
unsafe { copy(x.as_ptr(), y.as_mut_ptr(), DOUBLE_SIZE * size_of::<u16>() / 2) };
//~^ ERROR: found a count of bytes instead of a count of elements of `T`
// Count expression involving divisions by size_of (Should not trigger the lint)
unsafe { copy(x.as_ptr(), y.as_mut_ptr(), DOUBLE_SIZE / size_of::<u8>()) };
unsafe { copy(x.as_ptr(), y.as_mut_ptr(), DOUBLE_SIZE / size_of::<u16>()) };
// Count expression involving divisions by multiple size_of (Should not trigger the lint)
unsafe { copy(x.as_ptr(), y.as_mut_ptr(), DOUBLE_SIZE / (2 * size_of::<u8>())) };
unsafe { copy(x.as_ptr(), y.as_mut_ptr(), DOUBLE_SIZE / (2 * size_of::<u16>())) };
// Count expression involving recursive divisions by size_of (Should trigger the lint)
unsafe { copy(x.as_ptr(), y.as_mut_ptr(), DOUBLE_SIZE / (2 / size_of::<u8>())) };
unsafe { copy(x.as_ptr(), y.as_mut_ptr(), DOUBLE_SIZE / (2 / size_of::<u16>())) };
//~^ ERROR: found a count of bytes instead of a count of elements of `T`
// No size_of calls (Should not trigger the lint)
unsafe { copy(x.as_ptr(), y.as_mut_ptr(), SIZE) };
// Different types for pointee and size_of (Should not trigger the lint)
unsafe { y.as_mut_ptr().write_bytes(0u8, size_of::<u16>() / 2 * SIZE) };
}

View file

@ -1,8 +1,8 @@
error: found a count of bytes instead of a count of elements of `T`
--> tests/ui/size_of_in_element_count/expressions.rs:15:62
|
LL | unsafe { copy_nonoverlapping(x.as_ptr(), y.as_mut_ptr(), size_of::<u8>() * SIZE) };
| ^^^^^^^^^^^^^^^^^^^^^^
LL | unsafe { copy_nonoverlapping(x.as_ptr(), y.as_mut_ptr(), size_of::<u16>() * SIZE) };
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= help: use a count of elements instead of a count of bytes, it already gets multiplied by the size of the type
= note: `-D clippy::size-of-in-element-count` implied by `-D warnings`
@ -19,16 +19,16 @@ LL | unsafe { copy_nonoverlapping(x.as_ptr(), y.as_mut_ptr(), HALF_SIZE * si
error: found a count of bytes instead of a count of elements of `T`
--> tests/ui/size_of_in_element_count/expressions.rs:23:47
|
LL | unsafe { copy(x.as_ptr(), y.as_mut_ptr(), DOUBLE_SIZE * size_of::<u8>() / 2) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | unsafe { copy(x.as_ptr(), y.as_mut_ptr(), DOUBLE_SIZE * size_of::<u16>() / 2) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: use a count of elements instead of a count of bytes, it already gets multiplied by the size of the type
error: found a count of bytes instead of a count of elements of `T`
--> tests/ui/size_of_in_element_count/expressions.rs:33:47
|
LL | unsafe { copy(x.as_ptr(), y.as_mut_ptr(), DOUBLE_SIZE / (2 / size_of::<u8>())) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | unsafe { copy(x.as_ptr(), y.as_mut_ptr(), DOUBLE_SIZE / (2 / size_of::<u16>())) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: use a count of elements instead of a count of bytes, it already gets multiplied by the size of the type

View file

@ -11,57 +11,52 @@ fn main() {
const SIZE: usize = 128;
const HALF_SIZE: usize = SIZE / 2;
const DOUBLE_SIZE: usize = SIZE * 2;
let mut x = [2u8; SIZE];
let mut y = [2u8; SIZE];
let mut x = [2u16; SIZE];
let mut y = [2u16; SIZE];
// Count is size_of (Should trigger the lint)
unsafe { copy_nonoverlapping::<u8>(x.as_ptr(), y.as_mut_ptr(), size_of::<u8>()) };
unsafe { copy_nonoverlapping::<u16>(x.as_ptr(), y.as_mut_ptr(), size_of::<u16>()) };
//~^ ERROR: found a count of bytes instead of a count of elements of `T`
unsafe { copy_nonoverlapping(x.as_ptr(), y.as_mut_ptr(), size_of_val(&x[0])) };
//~^ ERROR: found a count of bytes instead of a count of elements of `T`
unsafe { x.as_ptr().copy_to(y.as_mut_ptr(), size_of::<u8>()) };
unsafe { x.as_ptr().copy_to(y.as_mut_ptr(), size_of::<u16>()) };
//~^ ERROR: found a count of bytes instead of a count of elements of `T`
unsafe { x.as_ptr().copy_to_nonoverlapping(y.as_mut_ptr(), size_of::<u8>()) };
unsafe { x.as_ptr().copy_to_nonoverlapping(y.as_mut_ptr(), size_of::<u16>()) };
//~^ ERROR: found a count of bytes instead of a count of elements of `T`
unsafe { y.as_mut_ptr().copy_from(x.as_ptr(), size_of::<u8>()) };
unsafe { y.as_mut_ptr().copy_from(x.as_ptr(), size_of::<u16>()) };
//~^ ERROR: found a count of bytes instead of a count of elements of `T`
unsafe { y.as_mut_ptr().copy_from_nonoverlapping(x.as_ptr(), size_of::<u8>()) };
unsafe { y.as_mut_ptr().copy_from_nonoverlapping(x.as_ptr(), size_of::<u16>()) };
//~^ ERROR: found a count of bytes instead of a count of elements of `T`
unsafe { copy(x.as_ptr(), y.as_mut_ptr(), size_of::<u8>()) };
unsafe { copy(x.as_ptr(), y.as_mut_ptr(), size_of::<u16>()) };
//~^ ERROR: found a count of bytes instead of a count of elements of `T`
unsafe { copy(x.as_ptr(), y.as_mut_ptr(), size_of_val(&x[0])) };
//~^ ERROR: found a count of bytes instead of a count of elements of `T`
unsafe { y.as_mut_ptr().write_bytes(0u8, size_of::<u8>() * SIZE) };
//~^ ERROR: found a count of bytes instead of a count of elements of `T`
unsafe { write_bytes(y.as_mut_ptr(), 0u8, size_of::<u8>() * SIZE) };
unsafe { swap_nonoverlapping(y.as_mut_ptr(), x.as_mut_ptr(), size_of::<u16>() * SIZE) };
//~^ ERROR: found a count of bytes instead of a count of elements of `T`
unsafe { swap_nonoverlapping(y.as_mut_ptr(), x.as_mut_ptr(), size_of::<u8>() * SIZE) };
slice_from_raw_parts_mut(y.as_mut_ptr(), size_of::<u16>() * SIZE);
//~^ ERROR: found a count of bytes instead of a count of elements of `T`
slice_from_raw_parts(y.as_ptr(), size_of::<u16>() * SIZE);
//~^ ERROR: found a count of bytes instead of a count of elements of `T`
slice_from_raw_parts_mut(y.as_mut_ptr(), size_of::<u8>() * SIZE);
unsafe { from_raw_parts_mut(y.as_mut_ptr(), size_of::<u16>() * SIZE) };
//~^ ERROR: found a count of bytes instead of a count of elements of `T`
slice_from_raw_parts(y.as_ptr(), size_of::<u8>() * SIZE);
unsafe { from_raw_parts(y.as_ptr(), size_of::<u16>() * SIZE) };
//~^ ERROR: found a count of bytes instead of a count of elements of `T`
unsafe { from_raw_parts_mut(y.as_mut_ptr(), size_of::<u8>() * SIZE) };
unsafe { y.as_mut_ptr().sub(size_of::<u16>()) };
//~^ ERROR: found a count of bytes instead of a count of elements of `T`
unsafe { from_raw_parts(y.as_ptr(), size_of::<u8>() * SIZE) };
y.as_ptr().wrapping_sub(size_of::<u16>());
//~^ ERROR: found a count of bytes instead of a count of elements of `T`
unsafe { y.as_mut_ptr().sub(size_of::<u8>()) };
unsafe { y.as_ptr().add(size_of::<u16>()) };
//~^ ERROR: found a count of bytes instead of a count of elements of `T`
y.as_ptr().wrapping_sub(size_of::<u8>());
y.as_mut_ptr().wrapping_add(size_of::<u16>());
//~^ ERROR: found a count of bytes instead of a count of elements of `T`
unsafe { y.as_ptr().add(size_of::<u8>()) };
unsafe { y.as_ptr().offset(size_of::<u16>() as isize) };
//~^ ERROR: found a count of bytes instead of a count of elements of `T`
y.as_mut_ptr().wrapping_add(size_of::<u8>());
//~^ ERROR: found a count of bytes instead of a count of elements of `T`
unsafe { y.as_ptr().offset(size_of::<u8>() as isize) };
//~^ ERROR: found a count of bytes instead of a count of elements of `T`
y.as_mut_ptr().wrapping_offset(size_of::<u8>() as isize);
y.as_mut_ptr().wrapping_offset(size_of::<u16>() as isize);
//~^ ERROR: found a count of bytes instead of a count of elements of `T`
}

View file

@ -1,8 +1,8 @@
error: found a count of bytes instead of a count of elements of `T`
--> tests/ui/size_of_in_element_count/functions.rs:18:68
--> tests/ui/size_of_in_element_count/functions.rs:18:69
|
LL | unsafe { copy_nonoverlapping::<u8>(x.as_ptr(), y.as_mut_ptr(), size_of::<u8>()) };
| ^^^^^^^^^^^^^^^
LL | unsafe { copy_nonoverlapping::<u16>(x.as_ptr(), y.as_mut_ptr(), size_of::<u16>()) };
| ^^^^^^^^^^^^^^^^
|
= help: use a count of elements instead of a count of bytes, it already gets multiplied by the size of the type
= note: `-D clippy::size-of-in-element-count` implied by `-D warnings`
@ -19,40 +19,40 @@ LL | unsafe { copy_nonoverlapping(x.as_ptr(), y.as_mut_ptr(), size_of_val(&x
error: found a count of bytes instead of a count of elements of `T`
--> tests/ui/size_of_in_element_count/functions.rs:23:49
|
LL | unsafe { x.as_ptr().copy_to(y.as_mut_ptr(), size_of::<u8>()) };
| ^^^^^^^^^^^^^^^
LL | unsafe { x.as_ptr().copy_to(y.as_mut_ptr(), size_of::<u16>()) };
| ^^^^^^^^^^^^^^^^
|
= help: use a count of elements instead of a count of bytes, it already gets multiplied by the size of the type
error: found a count of bytes instead of a count of elements of `T`
--> tests/ui/size_of_in_element_count/functions.rs:25:64
|
LL | unsafe { x.as_ptr().copy_to_nonoverlapping(y.as_mut_ptr(), size_of::<u8>()) };
| ^^^^^^^^^^^^^^^
LL | unsafe { x.as_ptr().copy_to_nonoverlapping(y.as_mut_ptr(), size_of::<u16>()) };
| ^^^^^^^^^^^^^^^^
|
= help: use a count of elements instead of a count of bytes, it already gets multiplied by the size of the type
error: found a count of bytes instead of a count of elements of `T`
--> tests/ui/size_of_in_element_count/functions.rs:27:51
|
LL | unsafe { y.as_mut_ptr().copy_from(x.as_ptr(), size_of::<u8>()) };
| ^^^^^^^^^^^^^^^
LL | unsafe { y.as_mut_ptr().copy_from(x.as_ptr(), size_of::<u16>()) };
| ^^^^^^^^^^^^^^^^
|
= help: use a count of elements instead of a count of bytes, it already gets multiplied by the size of the type
error: found a count of bytes instead of a count of elements of `T`
--> tests/ui/size_of_in_element_count/functions.rs:29:66
|
LL | unsafe { y.as_mut_ptr().copy_from_nonoverlapping(x.as_ptr(), size_of::<u8>()) };
| ^^^^^^^^^^^^^^^
LL | unsafe { y.as_mut_ptr().copy_from_nonoverlapping(x.as_ptr(), size_of::<u16>()) };
| ^^^^^^^^^^^^^^^^
|
= help: use a count of elements instead of a count of bytes, it already gets multiplied by the size of the type
error: found a count of bytes instead of a count of elements of `T`
--> tests/ui/size_of_in_element_count/functions.rs:32:47
|
LL | unsafe { copy(x.as_ptr(), y.as_mut_ptr(), size_of::<u8>()) };
| ^^^^^^^^^^^^^^^
LL | unsafe { copy(x.as_ptr(), y.as_mut_ptr(), size_of::<u16>()) };
| ^^^^^^^^^^^^^^^^
|
= help: use a count of elements instead of a count of bytes, it already gets multiplied by the size of the type
@ -65,108 +65,92 @@ LL | unsafe { copy(x.as_ptr(), y.as_mut_ptr(), size_of_val(&x[0])) };
= help: use a count of elements instead of a count of bytes, it already gets multiplied by the size of the type
error: found a count of bytes instead of a count of elements of `T`
--> tests/ui/size_of_in_element_count/functions.rs:37:46
--> tests/ui/size_of_in_element_count/functions.rs:37:66
|
LL | unsafe { y.as_mut_ptr().write_bytes(0u8, size_of::<u8>() * SIZE) };
| ^^^^^^^^^^^^^^^^^^^^^^
LL | unsafe { swap_nonoverlapping(y.as_mut_ptr(), x.as_mut_ptr(), size_of::<u16>() * SIZE) };
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= help: use a count of elements instead of a count of bytes, it already gets multiplied by the size of the type
error: found a count of bytes instead of a count of elements of `T`
--> tests/ui/size_of_in_element_count/functions.rs:39:47
--> tests/ui/size_of_in_element_count/functions.rs:40:46
|
LL | unsafe { write_bytes(y.as_mut_ptr(), 0u8, size_of::<u8>() * SIZE) };
| ^^^^^^^^^^^^^^^^^^^^^^
LL | slice_from_raw_parts_mut(y.as_mut_ptr(), size_of::<u16>() * SIZE);
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= help: use a count of elements instead of a count of bytes, it already gets multiplied by the size of the type
error: found a count of bytes instead of a count of elements of `T`
--> tests/ui/size_of_in_element_count/functions.rs:42:66
--> tests/ui/size_of_in_element_count/functions.rs:42:38
|
LL | unsafe { swap_nonoverlapping(y.as_mut_ptr(), x.as_mut_ptr(), size_of::<u8>() * SIZE) };
| ^^^^^^^^^^^^^^^^^^^^^^
LL | slice_from_raw_parts(y.as_ptr(), size_of::<u16>() * SIZE);
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= help: use a count of elements instead of a count of bytes, it already gets multiplied by the size of the type
error: found a count of bytes instead of a count of elements of `T`
--> tests/ui/size_of_in_element_count/functions.rs:45:46
--> tests/ui/size_of_in_element_count/functions.rs:45:49
|
LL | slice_from_raw_parts_mut(y.as_mut_ptr(), size_of::<u8>() * SIZE);
| ^^^^^^^^^^^^^^^^^^^^^^
LL | unsafe { from_raw_parts_mut(y.as_mut_ptr(), size_of::<u16>() * SIZE) };
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= help: use a count of elements instead of a count of bytes, it already gets multiplied by the size of the type
error: found a count of bytes instead of a count of elements of `T`
--> tests/ui/size_of_in_element_count/functions.rs:47:38
--> tests/ui/size_of_in_element_count/functions.rs:47:41
|
LL | slice_from_raw_parts(y.as_ptr(), size_of::<u8>() * SIZE);
| ^^^^^^^^^^^^^^^^^^^^^^
LL | unsafe { from_raw_parts(y.as_ptr(), size_of::<u16>() * SIZE) };
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= help: use a count of elements instead of a count of bytes, it already gets multiplied by the size of the type
error: found a count of bytes instead of a count of elements of `T`
--> tests/ui/size_of_in_element_count/functions.rs:50:49
--> tests/ui/size_of_in_element_count/functions.rs:50:33
|
LL | unsafe { from_raw_parts_mut(y.as_mut_ptr(), size_of::<u8>() * SIZE) };
| ^^^^^^^^^^^^^^^^^^^^^^
LL | unsafe { y.as_mut_ptr().sub(size_of::<u16>()) };
| ^^^^^^^^^^^^^^^^
|
= help: use a count of elements instead of a count of bytes, it already gets multiplied by the size of the type
error: found a count of bytes instead of a count of elements of `T`
--> tests/ui/size_of_in_element_count/functions.rs:52:41
--> tests/ui/size_of_in_element_count/functions.rs:52:29
|
LL | unsafe { from_raw_parts(y.as_ptr(), size_of::<u8>() * SIZE) };
| ^^^^^^^^^^^^^^^^^^^^^^
LL | y.as_ptr().wrapping_sub(size_of::<u16>());
| ^^^^^^^^^^^^^^^^
|
= help: use a count of elements instead of a count of bytes, it already gets multiplied by the size of the type
error: found a count of bytes instead of a count of elements of `T`
--> tests/ui/size_of_in_element_count/functions.rs:55:33
--> tests/ui/size_of_in_element_count/functions.rs:54:29
|
LL | unsafe { y.as_mut_ptr().sub(size_of::<u8>()) };
| ^^^^^^^^^^^^^^^
LL | unsafe { y.as_ptr().add(size_of::<u16>()) };
| ^^^^^^^^^^^^^^^^
|
= help: use a count of elements instead of a count of bytes, it already gets multiplied by the size of the type
error: found a count of bytes instead of a count of elements of `T`
--> tests/ui/size_of_in_element_count/functions.rs:57:29
--> tests/ui/size_of_in_element_count/functions.rs:56:33
|
LL | y.as_ptr().wrapping_sub(size_of::<u8>());
| ^^^^^^^^^^^^^^^
LL | y.as_mut_ptr().wrapping_add(size_of::<u16>());
| ^^^^^^^^^^^^^^^^
|
= help: use a count of elements instead of a count of bytes, it already gets multiplied by the size of the type
error: found a count of bytes instead of a count of elements of `T`
--> tests/ui/size_of_in_element_count/functions.rs:59:29
--> tests/ui/size_of_in_element_count/functions.rs:58:32
|
LL | unsafe { y.as_ptr().add(size_of::<u8>()) };
| ^^^^^^^^^^^^^^^
LL | unsafe { y.as_ptr().offset(size_of::<u16>() as isize) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: use a count of elements instead of a count of bytes, it already gets multiplied by the size of the type
error: found a count of bytes instead of a count of elements of `T`
--> tests/ui/size_of_in_element_count/functions.rs:61:33
--> tests/ui/size_of_in_element_count/functions.rs:60:36
|
LL | y.as_mut_ptr().wrapping_add(size_of::<u8>());
| ^^^^^^^^^^^^^^^
LL | y.as_mut_ptr().wrapping_offset(size_of::<u16>() as isize);
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: use a count of elements instead of a count of bytes, it already gets multiplied by the size of the type
error: found a count of bytes instead of a count of elements of `T`
--> tests/ui/size_of_in_element_count/functions.rs:63:32
|
LL | unsafe { y.as_ptr().offset(size_of::<u8>() as isize) };
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: use a count of elements instead of a count of bytes, it already gets multiplied by the size of the type
error: found a count of bytes instead of a count of elements of `T`
--> tests/ui/size_of_in_element_count/functions.rs:65:36
|
LL | y.as_mut_ptr().wrapping_offset(size_of::<u8>() as isize);
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: use a count of elements instead of a count of bytes, it already gets multiplied by the size of the type
error: aborting due to 21 previous errors
error: aborting due to 19 previous errors