Skip to content

Commit 203c909

Browse files
committed
Auto merge of #10661 - timvisee:manual-slice-size-calc-suggestion, r=Manishearth
Add `manual_slice_size_calculation` applicable suggestion Continuation of #10659 (comment). This adds applicable suggestions to the `manual_slice_size_calculation` lint: ``` error: manual slice size calculation --> $DIR/manual_slice_size_calculation.rs:11:13 | LL | let _ = s_i32.len() * size_of::<i32>(); // WARNING | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)` | = note: `-D clippy::manual-slice-size-calculation` implied by `-D warnings` ``` changelog: [`manual_slice_size_calculation`]: add machine applicable suggestion
2 parents e57deaa + a2580db commit 203c909

4 files changed

+68
-31
lines changed

clippy_lints/src/manual_slice_size_calculation.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::{expr_or_init, in_constant};
1+
// run-rustfix
2+
use clippy_utils::diagnostics::span_lint_and_sugg;
3+
use clippy_utils::source::snippet_with_context;
4+
use clippy_utils::{expr_or_init, in_constant, std_or_core};
5+
use rustc_errors::Applicability;
36
use rustc_hir::{BinOpKind, Expr, ExprKind};
47
use rustc_lint::{LateContext, LateLintPass};
58
use rustc_middle::ty;
@@ -42,15 +45,22 @@ impl<'tcx> LateLintPass<'tcx> for ManualSliceSizeCalculation {
4245
if !in_constant(cx, expr.hir_id)
4346
&& let ExprKind::Binary(ref op, left, right) = expr.kind
4447
&& BinOpKind::Mul == op.node
45-
&& let Some(_receiver) = simplify(cx, left, right)
48+
&& let Some(receiver) = simplify(cx, left, right)
4649
{
47-
span_lint_and_help(
50+
let ctxt = expr.span.ctxt();
51+
let mut app = Applicability::MachineApplicable;
52+
let val_name = snippet_with_context(cx, receiver.span, ctxt, "slice", &mut app).0;
53+
let Some(sugg) = std_or_core(cx) else { return };
54+
55+
span_lint_and_sugg(
4856
cx,
4957
MANUAL_SLICE_SIZE_CALCULATION,
50-
expr.span,
51-
"manual slice size calculation",
52-
None,
53-
"consider using std::mem::size_of_val instead");
58+
expr.span,
59+
"manual slice size calculation",
60+
"try",
61+
format!("{sugg}::mem::size_of_val({val_name})"),
62+
Applicability::MachineApplicable,
63+
);
5464
}
5565
}
5666
}
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

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,40 @@
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
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
66
|
7-
= help: consider using std::mem::size_of_val instead
87
= note: `-D clippy::manual-slice-size-calculation` implied by `-D warnings`
98

109
error: manual slice size calculation
11-
--> $DIR/manual_slice_size_calculation.rs:12:13
10+
--> $DIR/manual_slice_size_calculation.rs:13:13
1211
|
1312
LL | let _ = size_of::<i32>() * s_i32.len(); // WARNING
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15-
|
16-
= help: consider using std::mem::size_of_val instead
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
1714

1815
error: manual slice size calculation
19-
--> $DIR/manual_slice_size_calculation.rs:13:13
16+
--> $DIR/manual_slice_size_calculation.rs:14:13
2017
|
2118
LL | let _ = size_of::<i32>() * s_i32.len() * 5; // WARNING
22-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
23-
|
24-
= help: consider using std::mem::size_of_val instead
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
2520

2621
error: manual slice size calculation
27-
--> $DIR/manual_slice_size_calculation.rs:17:13
22+
--> $DIR/manual_slice_size_calculation.rs:18:13
2823
|
2924
LL | let _ = len * size_of::<i32>(); // WARNING
30-
| ^^^^^^^^^^^^^^^^^^^^^^
31-
|
32-
= help: consider using std::mem::size_of_val instead
25+
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
3326

3427
error: manual slice size calculation
35-
--> $DIR/manual_slice_size_calculation.rs:18:13
28+
--> $DIR/manual_slice_size_calculation.rs:19:13
3629
|
3730
LL | let _ = s_i32.len() * size; // WARNING
38-
| ^^^^^^^^^^^^^^^^^^
39-
|
40-
= help: consider using std::mem::size_of_val instead
31+
| ^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
4132

4233
error: manual slice size calculation
43-
--> $DIR/manual_slice_size_calculation.rs:19:13
34+
--> $DIR/manual_slice_size_calculation.rs:20:13
4435
|
4536
LL | let _ = len * size; // WARNING
46-
| ^^^^^^^^^^
47-
|
48-
= help: consider using std::mem::size_of_val instead
37+
| ^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
4938

5039
error: aborting due to 6 previous errors
5140

0 commit comments

Comments
 (0)