Add run-rustfix marker and test file
This commit is contained in:
parent
503fd56a42
commit
b8fee8b504
@ -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};
|
||||
|
37
tests/ui/manual_slice_size_calculation.fixed
Normal file
37
tests/ui/manual_slice_size_calculation.fixed
Normal 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
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
// run-rustfix
|
||||
#![allow(unused)]
|
||||
#![warn(clippy::manual_slice_size_calculation)]
|
||||
|
||||
|
@ -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)`
|
||||
|
Loading…
Reference in New Issue
Block a user