Skip to content

Ignore manual_slice_size_calculation in code from macro expansions #10667

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

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions clippy_lints/src/manual_slice_size_calculation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// 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};
Expand Down Expand Up @@ -45,6 +44,7 @@ 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
&& !expr.span.from_expansion()
&& let Some(receiver) = simplify(cx, left, right)
{
let ctxt = expr.span.ctxt();
Expand All @@ -55,12 +55,12 @@ impl<'tcx> LateLintPass<'tcx> for ManualSliceSizeCalculation {
span_lint_and_sugg(
cx,
MANUAL_SLICE_SIZE_CALCULATION,
expr.span,
"manual slice size calculation",
"try",
format!("{sugg}::mem::size_of_val({val_name})"),
Applicability::MachineApplicable,
);
expr.span,
"manual slice size calculation",
"try",
format!("{sugg}::mem::size_of_val({val_name})"),
app,
);
}
}
}
Expand All @@ -81,9 +81,9 @@ fn simplify_half<'tcx>(
expr1: &'tcx Expr<'tcx>,
expr2: &'tcx Expr<'tcx>,
) -> Option<&'tcx Expr<'tcx>> {
if
if !expr1.span.from_expansion()
// expr1 is `[T1].len()`?
let ExprKind::MethodCall(method_path, receiver, _, _) = expr1.kind
&& let ExprKind::MethodCall(method_path, receiver, _, _) = expr1.kind
&& method_path.ident.name == sym::len
&& let receiver_ty = cx.typeck_results().expr_ty(receiver)
&& let ty::Slice(ty1) = receiver_ty.peel_refs().kind()
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/manual_slice_size_calculation.fixed
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// run-rustfix
// aux-build:proc_macros.rs
#![allow(unused)]
#![warn(clippy::manual_slice_size_calculation)]

extern crate proc_macros;

use core::mem::{align_of, size_of};
use proc_macros::external;

fn main() {
let v_i32 = Vec::<i32>::new();
Expand All @@ -19,13 +23,18 @@ fn main() {
let _ = std::mem::size_of_val(s_i32); // WARNING
let _ = std::mem::size_of_val(s_i32); // WARNING

let _ = std::mem::size_of_val(external!(&[1u64][..]));

// 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

let _ = external!($s_i32.len() * size_of::<i32>());
let _ = external!($s_i32.len()) * size_of::<i32>();

// False negatives:
let _ = 5 * size_of::<i32>() * s_i32.len(); // Ok (MISSED OPPORTUNITY)
let _ = size_of::<i32>() * 5 * s_i32.len(); // Ok (MISSED OPPORTUNITY)
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/manual_slice_size_calculation.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// run-rustfix
// aux-build:proc_macros.rs
#![allow(unused)]
#![warn(clippy::manual_slice_size_calculation)]

extern crate proc_macros;

use core::mem::{align_of, size_of};
use proc_macros::external;

fn main() {
let v_i32 = Vec::<i32>::new();
Expand All @@ -19,13 +23,18 @@ fn main() {
let _ = s_i32.len() * size; // WARNING
let _ = len * size; // WARNING

let _ = external!(&[1u64][..]).len() * size_of::<u64>();

// 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

let _ = external!($s_i32.len() * size_of::<i32>());
let _ = external!($s_i32.len()) * size_of::<i32>();

// False negatives:
let _ = 5 * size_of::<i32>() * s_i32.len(); // Ok (MISSED OPPORTUNITY)
let _ = size_of::<i32>() * 5 * s_i32.len(); // Ok (MISSED OPPORTUNITY)
Expand Down
20 changes: 13 additions & 7 deletions tests/ui/manual_slice_size_calculation.stderr
Original file line number Diff line number Diff line change
@@ -1,40 +1,46 @@
error: manual slice size calculation
--> $DIR/manual_slice_size_calculation.rs:12:13
--> $DIR/manual_slice_size_calculation.rs:16: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`

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

error: manual slice size calculation
--> $DIR/manual_slice_size_calculation.rs:14:13
--> $DIR/manual_slice_size_calculation.rs:18:13
|
LL | let _ = size_of::<i32>() * s_i32.len() * 5; // WARNING
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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:22:13
|
LL | let _ = len * size_of::<i32>(); // WARNING
| ^^^^^^^^^^^^^^^^^^^^^^ 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:23:13
|
LL | let _ = s_i32.len() * size; // WARNING
| ^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`

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

error: aborting due to 6 previous errors
error: manual slice size calculation
--> $DIR/manual_slice_size_calculation.rs:26:13
|
LL | let _ = external!(&[1u64][..]).len() * size_of::<u64>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(external!(&[1u64][..]))`

error: aborting due to 7 previous errors