Fix lint manual_slice_size_calculation when the slice is ref more than once

This commit is contained in:
coekjan 2024-10-02 23:51:52 +08:00
parent 398be8c842
commit 2080634d33
No known key found for this signature in database
GPG key ID: E2B19491B838BB2B
4 changed files with 35 additions and 13 deletions

View file

@ -10,11 +10,15 @@ use proc_macros::external;
fn main() {
let v_i32 = Vec::<i32>::new();
let s_i32 = v_i32.as_slice();
let s_i32_ref = &s_i32;
let s_i32_ref_ref = &s_i32_ref;
// True positives:
let _ = std::mem::size_of_val(s_i32); // WARNING
let _ = std::mem::size_of_val(s_i32); // WARNING
let _ = std::mem::size_of_val(s_i32) * 5; // WARNING
let _ = std::mem::size_of_val(*s_i32_ref); // WARNING
let _ = std::mem::size_of_val(**s_i32_ref_ref); // WARNING
let len = s_i32.len();
let size = size_of::<i32>();

View file

@ -10,11 +10,15 @@ use proc_macros::external;
fn main() {
let v_i32 = Vec::<i32>::new();
let s_i32 = v_i32.as_slice();
let s_i32_ref = &s_i32;
let s_i32_ref_ref = &s_i32_ref;
// True positives:
let _ = s_i32.len() * size_of::<i32>(); // WARNING
let _ = size_of::<i32>() * s_i32.len(); // WARNING
let _ = size_of::<i32>() * s_i32.len() * 5; // WARNING
let _ = size_of::<i32>() * s_i32_ref.len(); // WARNING
let _ = size_of::<i32>() * s_i32_ref_ref.len(); // WARNING
let len = s_i32.len();
let size = size_of::<i32>();

View file

@ -1,5 +1,5 @@
error: manual slice size calculation
--> tests/ui/manual_slice_size_calculation.rs:15:13
--> tests/ui/manual_slice_size_calculation.rs:17:13
|
LL | let _ = s_i32.len() * size_of::<i32>(); // WARNING
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
@ -8,40 +8,52 @@ LL | let _ = s_i32.len() * size_of::<i32>(); // WARNING
= help: to override `-D warnings` add `#[allow(clippy::manual_slice_size_calculation)]`
error: manual slice size calculation
--> tests/ui/manual_slice_size_calculation.rs:16:13
--> tests/ui/manual_slice_size_calculation.rs:18:13
|
LL | let _ = size_of::<i32>() * s_i32.len(); // WARNING
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
error: manual slice size calculation
--> tests/ui/manual_slice_size_calculation.rs:17:13
--> tests/ui/manual_slice_size_calculation.rs:19:13
|
LL | let _ = size_of::<i32>() * s_i32.len() * 5; // WARNING
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
error: manual slice size calculation
--> tests/ui/manual_slice_size_calculation.rs:20:13
|
LL | let _ = size_of::<i32>() * s_i32_ref.len(); // WARNING
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(*s_i32_ref)`
error: manual slice size calculation
--> tests/ui/manual_slice_size_calculation.rs:21:13
|
LL | let _ = size_of::<i32>() * s_i32_ref_ref.len(); // WARNING
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(**s_i32_ref_ref)`
error: manual slice size calculation
--> tests/ui/manual_slice_size_calculation.rs:25:13
|
LL | let _ = len * size_of::<i32>(); // WARNING
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
error: manual slice size calculation
--> tests/ui/manual_slice_size_calculation.rs:22:13
--> tests/ui/manual_slice_size_calculation.rs:26:13
|
LL | let _ = s_i32.len() * size; // WARNING
| ^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
error: manual slice size calculation
--> tests/ui/manual_slice_size_calculation.rs:23:13
--> tests/ui/manual_slice_size_calculation.rs:27:13
|
LL | let _ = len * size; // WARNING
| ^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
error: manual slice size calculation
--> tests/ui/manual_slice_size_calculation.rs:25:13
--> tests/ui/manual_slice_size_calculation.rs:29:13
|
LL | let _ = external!(&[1u64][..]).len() * size_of::<u64>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(external!(&[1u64][..]))`
error: aborting due to 7 previous errors
error: aborting due to 9 previous errors