mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-30 18:53:39 +00:00
Suggest applicable expression for manual slice size calculation lint
This commit is contained in:
parent
e57deaace5
commit
503fd56a42
@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user