Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add manual_slice_size_calculation applicable suggestion #10661

Merged
merged 3 commits into from Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 18 additions & 8 deletions clippy_lints/src/manual_slice_size_calculation.rs
@@ -1,5 +1,8 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::{expr_or_init, in_constant};
// 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, std_or_core};
use rustc_errors::Applicability;
use rustc_hir::{BinOpKind, Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty;
Expand Down Expand Up @@ -42,15 +45,22 @@ 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;
let Some(sugg) = std_or_core(cx) else { return };

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!("{sugg}::mem::size_of_val({val_name})"),
Applicability::MachineApplicable,
);
}
}
}
Expand Down
37 changes: 37 additions & 0 deletions tests/ui/manual_slice_size_calculation.fixed
@@ -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 change: 1 addition & 0 deletions tests/ui/manual_slice_size_calculation.rs
@@ -1,3 +1,4 @@
// run-rustfix
#![allow(unused)]
#![warn(clippy::manual_slice_size_calculation)]

Expand Down
35 changes: 12 additions & 23 deletions tests/ui/manual_slice_size_calculation.stderr
@@ -1,51 +1,40 @@
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)`
|
= 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
--> $DIR/manual_slice_size_calculation.rs:13: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
--> $DIR/manual_slice_size_calculation.rs:14: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
--> $DIR/manual_slice_size_calculation.rs:18: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
--> $DIR/manual_slice_size_calculation.rs:19: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
--> $DIR/manual_slice_size_calculation.rs:20: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