Skip to content

Commit 8099f19

Browse files
committed
Auto merge of rust-lang#10667 - Alexendoo:manual-slice-size-calculation-macros, r=llogiq
Ignore `manual_slice_size_calculation` in code from macro expansions changelog: none, assuming same release as rust-lang#10659
2 parents 203c909 + 2f4f798 commit 8099f19

4 files changed

+40
-16
lines changed

clippy_lints/src/manual_slice_size_calculation.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// run-rustfix
21
use clippy_utils::diagnostics::span_lint_and_sugg;
32
use clippy_utils::source::snippet_with_context;
43
use clippy_utils::{expr_or_init, in_constant, std_or_core};
@@ -45,6 +44,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualSliceSizeCalculation {
4544
if !in_constant(cx, expr.hir_id)
4645
&& let ExprKind::Binary(ref op, left, right) = expr.kind
4746
&& BinOpKind::Mul == op.node
47+
&& !expr.span.from_expansion()
4848
&& let Some(receiver) = simplify(cx, left, right)
4949
{
5050
let ctxt = expr.span.ctxt();
@@ -55,12 +55,12 @@ impl<'tcx> LateLintPass<'tcx> for ManualSliceSizeCalculation {
5555
span_lint_and_sugg(
5656
cx,
5757
MANUAL_SLICE_SIZE_CALCULATION,
58-
expr.span,
59-
"manual slice size calculation",
60-
"try",
61-
format!("{sugg}::mem::size_of_val({val_name})"),
62-
Applicability::MachineApplicable,
63-
);
58+
expr.span,
59+
"manual slice size calculation",
60+
"try",
61+
format!("{sugg}::mem::size_of_val({val_name})"),
62+
app,
63+
);
6464
}
6565
}
6666
}
@@ -81,9 +81,9 @@ fn simplify_half<'tcx>(
8181
expr1: &'tcx Expr<'tcx>,
8282
expr2: &'tcx Expr<'tcx>,
8383
) -> Option<&'tcx Expr<'tcx>> {
84-
if
84+
if !expr1.span.from_expansion()
8585
// expr1 is `[T1].len()`?
86-
let ExprKind::MethodCall(method_path, receiver, _, _) = expr1.kind
86+
&& let ExprKind::MethodCall(method_path, receiver, _, _) = expr1.kind
8787
&& method_path.ident.name == sym::len
8888
&& let receiver_ty = cx.typeck_results().expr_ty(receiver)
8989
&& let ty::Slice(ty1) = receiver_ty.peel_refs().kind()

tests/ui/manual_slice_size_calculation.fixed

+9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
// run-rustfix
2+
// aux-build:proc_macros.rs
23
#![allow(unused)]
34
#![warn(clippy::manual_slice_size_calculation)]
45

6+
extern crate proc_macros;
7+
58
use core::mem::{align_of, size_of};
9+
use proc_macros::external;
610

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

26+
let _ = std::mem::size_of_val(external!(&[1u64][..]));
27+
2228
// True negatives:
2329
let _ = size_of::<i32>() + s_i32.len(); // Ok, not a multiplication
2430
let _ = size_of::<i32>() * s_i32.partition_point(|_| true); // Ok, not len()
2531
let _ = size_of::<i32>() * v_i32.len(); // Ok, not a slice
2632
let _ = align_of::<i32>() * s_i32.len(); // Ok, not size_of()
2733
let _ = size_of::<u32>() * s_i32.len(); // Ok, different types
2834

35+
let _ = external!($s_i32.len() * size_of::<i32>());
36+
let _ = external!($s_i32.len()) * size_of::<i32>();
37+
2938
// False negatives:
3039
let _ = 5 * size_of::<i32>() * s_i32.len(); // Ok (MISSED OPPORTUNITY)
3140
let _ = size_of::<i32>() * 5 * s_i32.len(); // Ok (MISSED OPPORTUNITY)

tests/ui/manual_slice_size_calculation.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
// run-rustfix
2+
// aux-build:proc_macros.rs
23
#![allow(unused)]
34
#![warn(clippy::manual_slice_size_calculation)]
45

6+
extern crate proc_macros;
7+
58
use core::mem::{align_of, size_of};
9+
use proc_macros::external;
610

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

26+
let _ = external!(&[1u64][..]).len() * size_of::<u64>();
27+
2228
// True negatives:
2329
let _ = size_of::<i32>() + s_i32.len(); // Ok, not a multiplication
2430
let _ = size_of::<i32>() * s_i32.partition_point(|_| true); // Ok, not len()
2531
let _ = size_of::<i32>() * v_i32.len(); // Ok, not a slice
2632
let _ = align_of::<i32>() * s_i32.len(); // Ok, not size_of()
2733
let _ = size_of::<u32>() * s_i32.len(); // Ok, different types
2834

35+
let _ = external!($s_i32.len() * size_of::<i32>());
36+
let _ = external!($s_i32.len()) * size_of::<i32>();
37+
2938
// False negatives:
3039
let _ = 5 * size_of::<i32>() * s_i32.len(); // Ok (MISSED OPPORTUNITY)
3140
let _ = size_of::<i32>() * 5 * s_i32.len(); // Ok (MISSED OPPORTUNITY)
+13-7
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,46 @@
11
error: manual slice size calculation
2-
--> $DIR/manual_slice_size_calculation.rs:12:13
2+
--> $DIR/manual_slice_size_calculation.rs:16:13
33
|
44
LL | let _ = s_i32.len() * size_of::<i32>(); // WARNING
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
66
|
77
= note: `-D clippy::manual-slice-size-calculation` implied by `-D warnings`
88

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

1515
error: manual slice size calculation
16-
--> $DIR/manual_slice_size_calculation.rs:14:13
16+
--> $DIR/manual_slice_size_calculation.rs:18:13
1717
|
1818
LL | let _ = size_of::<i32>() * s_i32.len() * 5; // WARNING
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
2020

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

2727
error: manual slice size calculation
28-
--> $DIR/manual_slice_size_calculation.rs:19:13
28+
--> $DIR/manual_slice_size_calculation.rs:23:13
2929
|
3030
LL | let _ = s_i32.len() * size; // WARNING
3131
| ^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(s_i32)`
3232

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

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

0 commit comments

Comments
 (0)