about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Schubart <michael@schubart.net>2023-04-07 08:00:53 +0900
committerMichael Schubart <michael@schubart.net>2023-04-07 08:00:53 +0900
commitb1c784d31f3361b3093466d61b8e62c997b1d086 (patch)
tree09ed36a6ae4885a3aff6556d37f59adfbbe9de76
parentb47a322ef11e541231b18c67dfe133dac8764b11 (diff)
downloadrust-b1c784d31f3361b3093466d61b8e62c997b1d086.tar.gz
rust-b1c784d31f3361b3093466d61b8e62c997b1d086.zip
Fix false negatives by using `expr_or_init`
-rw-r--r--clippy_lints/src/manual_slice_size_calculation.rs5
-rw-r--r--tests/ui/manual_slice_size_calculation.rs12
-rw-r--r--tests/ui/manual_slice_size_calculation.stderr26
3 files changed, 35 insertions, 8 deletions
diff --git a/clippy_lints/src/manual_slice_size_calculation.rs b/clippy_lints/src/manual_slice_size_calculation.rs
index 2659f347778..92ee79453a3 100644
--- a/clippy_lints/src/manual_slice_size_calculation.rs
+++ b/clippy_lints/src/manual_slice_size_calculation.rs
@@ -1,5 +1,5 @@
 use clippy_utils::diagnostics::span_lint_and_help;
-use clippy_utils::in_constant;
+use clippy_utils::{expr_or_init, in_constant};
 use rustc_hir::{BinOpKind, Expr, ExprKind};
 use rustc_lint::{LateContext, LateLintPass};
 use rustc_middle::ty;
@@ -60,6 +60,9 @@ fn simplify<'tcx>(
     expr1: &'tcx Expr<'tcx>,
     expr2: &'tcx Expr<'tcx>,
 ) -> Option<&'tcx Expr<'tcx>> {
+    let expr1 = expr_or_init(cx, expr1);
+    let expr2 = expr_or_init(cx, expr2);
+
     simplify_half(cx, expr1, expr2).or_else(|| simplify_half(cx, expr2, expr1))
 }
 
diff --git a/tests/ui/manual_slice_size_calculation.rs b/tests/ui/manual_slice_size_calculation.rs
index 2cb8c4c2ad3..5082f931f3c 100644
--- a/tests/ui/manual_slice_size_calculation.rs
+++ b/tests/ui/manual_slice_size_calculation.rs
@@ -12,6 +12,12 @@ fn main() {
     let _ = size_of::<i32>() * s_i32.len(); // WARNING
     let _ = size_of::<i32>() * s_i32.len() * 5; // WARNING
 
+    let len = s_i32.len();
+    let size = size_of::<i32>();
+    let _ = len * size_of::<i32>(); // WARNING
+    let _ = s_i32.len() * size; // WARNING
+    let _ = len * size; // 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()
@@ -22,12 +28,6 @@ fn main() {
     // False negatives:
     let _ = 5 * size_of::<i32>() * s_i32.len(); // Ok (MISSED OPPORTUNITY)
     let _ = size_of::<i32>() * 5 * s_i32.len(); // Ok (MISSED OPPORTUNITY)
-
-    let len = s_i32.len();
-    let size = size_of::<i32>();
-    let _ = len * size_of::<i32>(); // Ok (MISSED OPPORTUNITY)
-    let _ = s_i32.len() * size; // Ok (MISSED OPPORTUNITY)
-    let _ = len * size; // Ok (MISSED OPPORTUNITY)
 }
 
 const fn _const(s_i32: &[i32]) {
diff --git a/tests/ui/manual_slice_size_calculation.stderr b/tests/ui/manual_slice_size_calculation.stderr
index 33de9fad4d3..4a24fc60a0f 100644
--- a/tests/ui/manual_slice_size_calculation.stderr
+++ b/tests/ui/manual_slice_size_calculation.stderr
@@ -23,5 +23,29 @@ LL |     let _ = size_of::<i32>() * s_i32.len() * 5; // WARNING
    |
    = help: consider using std::mem::size_of_value instead
 
-error: aborting due to 3 previous errors
+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_value instead
+
+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_value instead
+
+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_value instead
+
+error: aborting due to 6 previous errors