Skip to content

Commit b8fee8b

Browse files
committed
Add run-rustfix marker and test file
1 parent 503fd56 commit b8fee8b

4 files changed

+45
-6
lines changed

clippy_lints/src/manual_slice_size_calculation.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// run-rustfix
12
use clippy_utils::diagnostics::span_lint_and_sugg;
23
use clippy_utils::source::snippet_with_context;
34
use clippy_utils::{expr_or_init, in_constant};
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// run-rustfix
2+
#![allow(unused)]
3+
#![warn(clippy::manual_slice_size_calculation)]
4+
5+
use core::mem::{align_of, size_of};
6+
7+
fn main() {
8+
let v_i32 = Vec::<i32>::new();
9+
let s_i32 = v_i32.as_slice();
10+
11+
// True positives:
12+
let _ = std::mem::size_of_val(s_i32); // WARNING
13+
let _ = std::mem::size_of_val(s_i32); // WARNING
14+
let _ = std::mem::size_of_val(s_i32) * 5; // WARNING
15+
16+
let len = s_i32.len();
17+
let size = size_of::<i32>();
18+
let _ = std::mem::size_of_val(s_i32); // WARNING
19+
let _ = std::mem::size_of_val(s_i32); // WARNING
20+
let _ = std::mem::size_of_val(s_i32); // WARNING
21+
22+
// True negatives:
23+
let _ = size_of::<i32>() + s_i32.len(); // Ok, not a multiplication
24+
let _ = size_of::<i32>() * s_i32.partition_point(|_| true); // Ok, not len()
25+
let _ = size_of::<i32>() * v_i32.len(); // Ok, not a slice
26+
let _ = align_of::<i32>() * s_i32.len(); // Ok, not size_of()
27+
let _ = size_of::<u32>() * s_i32.len(); // Ok, different types
28+
29+
// False negatives:
30+
let _ = 5 * size_of::<i32>() * s_i32.len(); // Ok (MISSED OPPORTUNITY)
31+
let _ = size_of::<i32>() * 5 * s_i32.len(); // Ok (MISSED OPPORTUNITY)
32+
}
33+
34+
const fn _const(s_i32: &[i32]) {
35+
// True negative:
36+
let _ = s_i32.len() * size_of::<i32>(); // Ok, can't use size_of_val in const
37+
}

tests/ui/manual_slice_size_calculation.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// run-rustfix
12
#![allow(unused)]
23
#![warn(clippy::manual_slice_size_calculation)]
34

tests/ui/manual_slice_size_calculation.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
error: manual slice size calculation
2-
--> $DIR/manual_slice_size_calculation.rs:11:13
2+
--> $DIR/manual_slice_size_calculation.rs:12:13
33
|
44
LL | let _ = s_i32.len() * size_of::<i32>(); // WARNING
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
66
|
77
= note: `-D clippy::manual-slice-size-calculation` implied by `-D warnings`
88

99
error: manual slice size calculation
10-
--> $DIR/manual_slice_size_calculation.rs:12:13
10+
--> $DIR/manual_slice_size_calculation.rs:13:13
1111
|
1212
LL | let _ = size_of::<i32>() * s_i32.len(); // WARNING
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
1414

1515
error: manual slice size calculation
16-
--> $DIR/manual_slice_size_calculation.rs:13:13
16+
--> $DIR/manual_slice_size_calculation.rs:14:13
1717
|
1818
LL | let _ = size_of::<i32>() * s_i32.len() * 5; // WARNING
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
2020

2121
error: manual slice size calculation
22-
--> $DIR/manual_slice_size_calculation.rs:17:13
22+
--> $DIR/manual_slice_size_calculation.rs:18:13
2323
|
2424
LL | let _ = len * size_of::<i32>(); // WARNING
2525
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
2626

2727
error: manual slice size calculation
28-
--> $DIR/manual_slice_size_calculation.rs:18:13
28+
--> $DIR/manual_slice_size_calculation.rs:19:13
2929
|
3030
LL | let _ = s_i32.len() * size; // WARNING
3131
| ^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
3232

3333
error: manual slice size calculation
34-
--> $DIR/manual_slice_size_calculation.rs:19:13
34+
--> $DIR/manual_slice_size_calculation.rs:20:13
3535
|
3636
LL | let _ = len * size; // WARNING
3737
| ^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`

0 commit comments

Comments
 (0)