Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 3de70a4

Browse files
committed
Move ErasingOp into Operators lint pass
1 parent 0adb3c0 commit 3de70a4

File tree

7 files changed

+81
-82
lines changed

7 files changed

+81
-82
lines changed

clippy_lints/src/erasing_op.rs

Lines changed: 0 additions & 77 deletions
This file was deleted.

clippy_lints/src/lib.register_all.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
6161
LintId::of(enum_clike::ENUM_CLIKE_UNPORTABLE_VARIANT),
6262
LintId::of(enum_variants::ENUM_VARIANT_NAMES),
6363
LintId::of(enum_variants::MODULE_INCEPTION),
64-
LintId::of(erasing_op::ERASING_OP),
6564
LintId::of(escape::BOXED_LOCAL),
6665
LintId::of(eta_reduction::REDUNDANT_CLOSURE),
6766
LintId::of(explicit_write::EXPLICIT_WRITE),
@@ -257,6 +256,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
257256
LintId::of(operators::DOUBLE_COMPARISONS),
258257
LintId::of(operators::DURATION_SUBSEC),
259258
LintId::of(operators::EQ_OP),
259+
LintId::of(operators::ERASING_OP),
260260
LintId::of(operators::INEFFECTIVE_BIT_MASK),
261261
LintId::of(operators::MISREFACTORED_ASSIGN_OP),
262262
LintId::of(operators::OP_REF),

clippy_lints/src/lib.register_correctness.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ store.register_group(true, "clippy::correctness", Some("clippy_correctness"), ve
2121
LintId::of(drop_forget_ref::FORGET_REF),
2222
LintId::of(drop_forget_ref::UNDROPPED_MANUALLY_DROPS),
2323
LintId::of(enum_clike::ENUM_CLIKE_UNPORTABLE_VARIANT),
24-
LintId::of(erasing_op::ERASING_OP),
2524
LintId::of(format_impl::RECURSIVE_FORMAT_IMPL),
2625
LintId::of(formatting::POSSIBLE_MISSING_COMMA),
2726
LintId::of(functions::NOT_UNSAFE_PTR_ARG_DEREF),
@@ -50,6 +49,7 @@ store.register_group(true, "clippy::correctness", Some("clippy_correctness"), ve
5049
LintId::of(operators::ABSURD_EXTREME_COMPARISONS),
5150
LintId::of(operators::BAD_BIT_MASK),
5251
LintId::of(operators::EQ_OP),
52+
LintId::of(operators::ERASING_OP),
5353
LintId::of(operators::INEFFECTIVE_BIT_MASK),
5454
LintId::of(option_env_unwrap::OPTION_ENV_UNWRAP),
5555
LintId::of(ptr::INVALID_NULL_PTR_USAGE),

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ store.register_lints(&[
141141
enum_variants::MODULE_INCEPTION,
142142
enum_variants::MODULE_NAME_REPETITIONS,
143143
equatable_if_let::EQUATABLE_IF_LET,
144-
erasing_op::ERASING_OP,
145144
escape::BOXED_LOCAL,
146145
eta_reduction::REDUNDANT_CLOSURE,
147146
eta_reduction::REDUNDANT_CLOSURE_FOR_METHOD_CALLS,
@@ -431,6 +430,7 @@ store.register_lints(&[
431430
operators::DOUBLE_COMPARISONS,
432431
operators::DURATION_SUBSEC,
433432
operators::EQ_OP,
433+
operators::ERASING_OP,
434434
operators::FLOAT_ARITHMETIC,
435435
operators::INEFFECTIVE_BIT_MASK,
436436
operators::INTEGER_ARITHMETIC,

clippy_lints/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ mod entry;
220220
mod enum_clike;
221221
mod enum_variants;
222222
mod equatable_if_let;
223-
mod erasing_op;
224223
mod escape;
225224
mod eta_reduction;
226225
mod excessive_bools;
@@ -585,7 +584,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
585584
store.register_late_pass(|| Box::new(misc::MiscLints));
586585
store.register_late_pass(|| Box::new(eta_reduction::EtaReduction));
587586
store.register_late_pass(|| Box::new(identity_op::IdentityOp));
588-
store.register_late_pass(|| Box::new(erasing_op::ErasingOp));
589587
store.register_late_pass(|| Box::new(mut_mut::MutMut));
590588
store.register_late_pass(|| Box::new(mut_reference::UnnecessaryMutPassed));
591589
store.register_late_pass(|| Box::new(len_zero::LenZero));
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use clippy_utils::consts::{constant_simple, Constant};
2+
use clippy_utils::diagnostics::span_lint;
3+
use clippy_utils::ty::same_type_and_consts;
4+
5+
use rustc_hir::{BinOpKind, Expr};
6+
use rustc_lint::LateContext;
7+
use rustc_middle::ty::TypeckResults;
8+
9+
use super::ERASING_OP;
10+
11+
pub(super) fn check<'tcx>(
12+
cx: &LateContext<'tcx>,
13+
e: &'tcx Expr<'_>,
14+
op: BinOpKind,
15+
left: &'tcx Expr<'_>,
16+
right: &'tcx Expr<'_>,
17+
) {
18+
let tck = cx.typeck_results();
19+
match op {
20+
BinOpKind::Mul | BinOpKind::BitAnd => {
21+
check_op(cx, tck, left, right, e);
22+
check_op(cx, tck, right, left, e);
23+
},
24+
BinOpKind::Div => check_op(cx, tck, left, right, e),
25+
_ => (),
26+
}
27+
}
28+
29+
fn different_types(tck: &TypeckResults<'_>, input: &Expr<'_>, output: &Expr<'_>) -> bool {
30+
let input_ty = tck.expr_ty(input).peel_refs();
31+
let output_ty = tck.expr_ty(output).peel_refs();
32+
!same_type_and_consts(input_ty, output_ty)
33+
}
34+
35+
fn check_op<'tcx>(
36+
cx: &LateContext<'tcx>,
37+
tck: &TypeckResults<'tcx>,
38+
op: &Expr<'tcx>,
39+
other: &Expr<'tcx>,
40+
parent: &Expr<'tcx>,
41+
) {
42+
if constant_simple(cx, tck, op) == Some(Constant::Int(0)) {
43+
if different_types(tck, other, parent) {
44+
return;
45+
}
46+
span_lint(
47+
cx,
48+
ERASING_OP,
49+
parent.span,
50+
"this operation will always return zero. This is likely not the intended outcome",
51+
);
52+
}
53+
}

clippy_lints/src/operators/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod bit_mask;
88
mod double_comparison;
99
mod duration_subsec;
1010
mod eq_op;
11+
mod erasing_op;
1112
mod misrefactored_assign_op;
1213
mod numeric_arithmetic;
1314
mod op_ref;
@@ -360,6 +361,28 @@ declare_clippy_lint! {
360361
"taking a reference to satisfy the type constraints on `==`"
361362
}
362363

364+
declare_clippy_lint! {
365+
/// ### What it does
366+
/// Checks for erasing operations, e.g., `x * 0`.
367+
///
368+
/// ### Why is this bad?
369+
/// The whole expression can be replaced by zero.
370+
/// This is most likely not the intended outcome and should probably be
371+
/// corrected
372+
///
373+
/// ### Example
374+
/// ```rust
375+
/// let x = 1;
376+
/// 0 / x;
377+
/// 0 * x;
378+
/// x & 0;
379+
/// ```
380+
#[clippy::version = "pre 1.29.0"]
381+
pub ERASING_OP,
382+
correctness,
383+
"using erasing operations, e.g., `x * 0` or `y & 0`"
384+
}
385+
363386
pub struct Operators {
364387
arithmetic_context: numeric_arithmetic::Context,
365388
verbose_bit_mask_threshold: u64,
@@ -377,6 +400,7 @@ impl_lint_pass!(Operators => [
377400
DURATION_SUBSEC,
378401
EQ_OP,
379402
OP_REF,
403+
ERASING_OP,
380404
]);
381405
impl Operators {
382406
pub fn new(verbose_bit_mask_threshold: u64) -> Self {
@@ -397,6 +421,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
397421
eq_op::check(cx, e, op.node, lhs, rhs);
398422
op_ref::check(cx, e, op.node, lhs, rhs);
399423
}
424+
erasing_op::check(cx, e, op.node, lhs, rhs);
400425
}
401426
self.arithmetic_context.check_binary(cx, e, op.node, lhs, rhs);
402427
bit_mask::check(cx, e, op.node, lhs, rhs);

0 commit comments

Comments
 (0)