Skip to content

Commit 73b48e5

Browse files
committed
Move ErasingOp into Operators lint pass
1 parent 727ebf6 commit 73b48e5

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
@@ -60,7 +60,6 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
6060
LintId::of(enum_clike::ENUM_CLIKE_UNPORTABLE_VARIANT),
6161
LintId::of(enum_variants::ENUM_VARIANT_NAMES),
6262
LintId::of(enum_variants::MODULE_INCEPTION),
63-
LintId::of(erasing_op::ERASING_OP),
6463
LintId::of(escape::BOXED_LOCAL),
6564
LintId::of(eta_reduction::REDUNDANT_CLOSURE),
6665
LintId::of(explicit_write::EXPLICIT_WRITE),
@@ -252,6 +251,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
252251
LintId::of(operators::DOUBLE_COMPARISONS),
253252
LintId::of(operators::DURATION_SUBSEC),
254253
LintId::of(operators::EQ_OP),
254+
LintId::of(operators::ERASING_OP),
255255
LintId::of(operators::INEFFECTIVE_BIT_MASK),
256256
LintId::of(operators::MISREFACTORED_ASSIGN_OP),
257257
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
@@ -138,7 +138,6 @@ store.register_lints(&[
138138
enum_variants::MODULE_INCEPTION,
139139
enum_variants::MODULE_NAME_REPETITIONS,
140140
equatable_if_let::EQUATABLE_IF_LET,
141-
erasing_op::ERASING_OP,
142141
escape::BOXED_LOCAL,
143142
eta_reduction::REDUNDANT_CLOSURE,
144143
eta_reduction::REDUNDANT_CLOSURE_FOR_METHOD_CALLS,
@@ -424,6 +423,7 @@ store.register_lints(&[
424423
operators::DOUBLE_COMPARISONS,
425424
operators::DURATION_SUBSEC,
426425
operators::EQ_OP,
426+
operators::ERASING_OP,
427427
operators::FLOAT_ARITHMETIC,
428428
operators::INEFFECTIVE_BIT_MASK,
429429
operators::INTEGER_ARITHMETIC,

clippy_lints/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ mod entry;
218218
mod enum_clike;
219219
mod enum_variants;
220220
mod equatable_if_let;
221-
mod erasing_op;
222221
mod escape;
223222
mod eta_reduction;
224223
mod excessive_bools;
@@ -547,7 +546,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
547546
store.register_late_pass(|| Box::new(misc::MiscLints));
548547
store.register_late_pass(|| Box::new(eta_reduction::EtaReduction));
549548
store.register_late_pass(|| Box::new(identity_op::IdentityOp));
550-
store.register_late_pass(|| Box::new(erasing_op::ErasingOp));
551549
store.register_late_pass(|| Box::new(mut_mut::MutMut));
552550
store.register_late_pass(|| Box::new(mut_reference::UnnecessaryMutPassed));
553551
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;
@@ -362,6 +363,28 @@ declare_clippy_lint! {
362363
"taking a reference to satisfy the type constraints on `==`"
363364
}
364365

366+
declare_clippy_lint! {
367+
/// ### What it does
368+
/// Checks for erasing operations, e.g., `x * 0`.
369+
///
370+
/// ### Why is this bad?
371+
/// The whole expression can be replaced by zero.
372+
/// This is most likely not the intended outcome and should probably be
373+
/// corrected
374+
///
375+
/// ### Example
376+
/// ```rust
377+
/// let x = 1;
378+
/// 0 / x;
379+
/// 0 * x;
380+
/// x & 0;
381+
/// ```
382+
#[clippy::version = "pre 1.29.0"]
383+
pub ERASING_OP,
384+
correctness,
385+
"using erasing operations, e.g., `x * 0` or `y & 0`"
386+
}
387+
365388
pub struct Operators {
366389
arithmetic_context: numeric_arithmetic::Context,
367390
verbose_bit_mask_threshold: u64,
@@ -379,6 +402,7 @@ impl_lint_pass!(Operators => [
379402
DURATION_SUBSEC,
380403
EQ_OP,
381404
OP_REF,
405+
ERASING_OP,
382406
]);
383407
impl Operators {
384408
pub fn new(verbose_bit_mask_threshold: u64) -> Self {
@@ -399,6 +423,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
399423
eq_op::check(cx, e, op.node, lhs, rhs);
400424
op_ref::check(cx, e, op.node, lhs, rhs);
401425
}
426+
erasing_op::check(cx, e, op.node, lhs, rhs);
402427
}
403428
self.arithmetic_context.check_binary(cx, e, op.node, lhs, rhs);
404429
bit_mask::check(cx, e, op.node, lhs, rhs);

0 commit comments

Comments
 (0)