Ignore manual_slice_size_calculation
in code from macro expansions
This commit is contained in:
parent
203c909d04
commit
2f4f798f9b
@ -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};
|
||||
@ -45,6 +44,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
|
||||
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();
|
||||
@ -55,12 +55,12 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
|
||||
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,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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()
|
||||
|
@ -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();
|
||||
@ -19,6 +23,8 @@ 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()
|
||||
@ -26,6 +32,9 @@ fn main() {
|
||||
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)
|
||||
|
@ -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();
|
||||
@ -19,6 +23,8 @@ 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()
|
||||
@ -26,6 +32,9 @@ fn main() {
|
||||
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)
|
||||
|
@ -1,5 +1,5 @@
|
||||
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)`
|
||||
@ -7,34 +7,40 @@ LL | let _ = s_i32.len() * size_of::<i32>(); // WARNING
|
||||
= 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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user