Suggest applicable expression for manual slice size calculation lint

This commit is contained in:
timvisee 2023-04-17 20:25:42 +02:00
parent e57deaace5
commit 503fd56a42
No known key found for this signature in database
GPG Key ID: B8DB720BC383E172
2 changed files with 21 additions and 24 deletions

View File

@ -1,5 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_context;
use clippy_utils::{expr_or_init, in_constant};
use rustc_errors::Applicability;
use rustc_hir::{BinOpKind, Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty;
@ -42,15 +44,21 @@ impl<'tcx> LateLintPass<'tcx> for ManualSliceSizeCalculation {
if !in_constant(cx, expr.hir_id)
&& let ExprKind::Binary(ref op, left, right) = expr.kind
&& BinOpKind::Mul == op.node
&& let Some(_receiver) = simplify(cx, left, right)
&& let Some(receiver) = simplify(cx, left, right)
{
span_lint_and_help(
let ctxt = expr.span.ctxt();
let mut app = Applicability::MachineApplicable;
let val_name = snippet_with_context(cx, receiver.span, ctxt, "slice", &mut app).0;
span_lint_and_sugg(
cx,
MANUAL_SLICE_SIZE_CALCULATION,
expr.span,
"manual slice size calculation",
None,
"consider using std::mem::size_of_val instead");
expr.span,
"manual slice size calculation",
"try",
format!("std::mem::size_of_val({val_name})"),
Applicability::MachineApplicable,
);
}
}
}

View File

@ -2,50 +2,39 @@ 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)`
|
= help: consider using std::mem::size_of_val instead
= note: `-D clippy::manual-slice-size-calculation` implied by `-D warnings`
error: manual slice size calculation
--> $DIR/manual_slice_size_calculation.rs:12:13
|
LL | let _ = size_of::<i32>() * s_i32.len(); // WARNING
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider using std::mem::size_of_val instead
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
error: manual slice size calculation
--> $DIR/manual_slice_size_calculation.rs:13:13
|
LL | let _ = size_of::<i32>() * s_i32.len() * 5; // WARNING
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider using std::mem::size_of_val instead
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
error: manual slice size calculation
--> $DIR/manual_slice_size_calculation.rs:17:13
|
LL | let _ = len * size_of::<i32>(); // WARNING
| ^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider using std::mem::size_of_val instead
| ^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
error: manual slice size calculation
--> $DIR/manual_slice_size_calculation.rs:18:13
|
LL | let _ = s_i32.len() * size; // WARNING
| ^^^^^^^^^^^^^^^^^^
|
= help: consider using std::mem::size_of_val instead
| ^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
error: manual slice size calculation
--> $DIR/manual_slice_size_calculation.rs:19:13
|
LL | let _ = len * size; // WARNING
| ^^^^^^^^^^
|
= help: consider using std::mem::size_of_val instead
| ^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
error: aborting due to 6 previous errors