Fix false negatives by using expr_or_init

This commit is contained in:
Michael Schubart 2023-04-07 08:00:53 +09:00
parent b47a322ef1
commit b1c784d31f
3 changed files with 35 additions and 8 deletions

View File

@ -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))
}

View File

@ -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]) {

View File

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