Add run-rustfix marker and test file

This commit is contained in:
timvisee 2023-04-17 20:43:16 +02:00
parent 503fd56a42
commit b8fee8b504
No known key found for this signature in database
GPG Key ID: B8DB720BC383E172
4 changed files with 45 additions and 6 deletions

View File

@ -1,3 +1,4 @@
// run-rustfix
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_context;
use clippy_utils::{expr_or_init, in_constant};

View File

@ -0,0 +1,37 @@
// run-rustfix
#![allow(unused)]
#![warn(clippy::manual_slice_size_calculation)]
use core::mem::{align_of, size_of};
fn main() {
let v_i32 = Vec::<i32>::new();
let s_i32 = v_i32.as_slice();
// 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 len = s_i32.len();
let size = size_of::<i32>();
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); // WARNING
// True negatives:
let _ = size_of::<i32>() + s_i32.len(); // Ok, not a multiplication
let _ = size_of::<i32>() * s_i32.partition_point(|_| true); // Ok, not len()
let _ = size_of::<i32>() * v_i32.len(); // Ok, not a slice
let _ = align_of::<i32>() * s_i32.len(); // Ok, not size_of()
let _ = size_of::<u32>() * s_i32.len(); // Ok, different types
// False negatives:
let _ = 5 * size_of::<i32>() * s_i32.len(); // Ok (MISSED OPPORTUNITY)
let _ = size_of::<i32>() * 5 * s_i32.len(); // Ok (MISSED OPPORTUNITY)
}
const fn _const(s_i32: &[i32]) {
// True negative:
let _ = s_i32.len() * size_of::<i32>(); // Ok, can't use size_of_val in const
}

View File

@ -1,3 +1,4 @@
// run-rustfix
#![allow(unused)]
#![warn(clippy::manual_slice_size_calculation)]

View File

@ -1,5 +1,5 @@
error: manual slice size calculation
--> $DIR/manual_slice_size_calculation.rs:11:13
--> $DIR/manual_slice_size_calculation.rs:12:13
|
LL | let _ = s_i32.len() * size_of::<i32>(); // WARNING
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
@ -7,31 +7,31 @@ LL | let _ = s_i32.len() * size_of::<i32>(); // WARNING
= note: `-D clippy::manual-slice-size-calculation` implied by `-D warnings`
error: manual slice size calculation
--> $DIR/manual_slice_size_calculation.rs:12:13
--> $DIR/manual_slice_size_calculation.rs:13:13
|
LL | let _ = size_of::<i32>() * s_i32.len(); // WARNING
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
error: manual slice size calculation
--> $DIR/manual_slice_size_calculation.rs:13:13
--> $DIR/manual_slice_size_calculation.rs:14: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
--> $DIR/manual_slice_size_calculation.rs:17:13
--> $DIR/manual_slice_size_calculation.rs:18:13
|
LL | let _ = len * size_of::<i32>(); // WARNING
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
error: manual slice size calculation
--> $DIR/manual_slice_size_calculation.rs:18:13
--> $DIR/manual_slice_size_calculation.rs:19:13
|
LL | let _ = s_i32.len() * size; // WARNING
| ^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
error: manual slice size calculation
--> $DIR/manual_slice_size_calculation.rs:19:13
--> $DIR/manual_slice_size_calculation.rs:20:13
|
LL | let _ = len * size; // WARNING
| ^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`