Skip to content

Commit 0e2314d

Browse files
committed
Move ErasingOp into Operators lint pass
1 parent 3e332e6 commit 0e2314d

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),
@@ -250,6 +249,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
250249
LintId::of(operators::DOUBLE_COMPARISONS),
251250
LintId::of(operators::DURATION_SUBSEC),
252251
LintId::of(operators::EQ_OP),
252+
LintId::of(operators::ERASING_OP),
253253
LintId::of(operators::INEFFECTIVE_BIT_MASK),
254254
LintId::of(operators::MISREFACTORED_ASSIGN_OP),
255255
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
@@ -137,7 +137,6 @@ store.register_lints(&[
137137
enum_variants::MODULE_INCEPTION,
138138
enum_variants::MODULE_NAME_REPETITIONS,
139139
equatable_if_let::EQUATABLE_IF_LET,
140-
erasing_op::ERASING_OP,
141140
escape::BOXED_LOCAL,
142141
eta_reduction::REDUNDANT_CLOSURE,
143142
eta_reduction::REDUNDANT_CLOSURE_FOR_METHOD_CALLS,
@@ -419,6 +418,7 @@ store.register_lints(&[
419418
operators::DOUBLE_COMPARISONS,
420419
operators::DURATION_SUBSEC,
421420
operators::EQ_OP,
421+
operators::ERASING_OP,
422422
operators::FLOAT_ARITHMETIC,
423423
operators::INEFFECTIVE_BIT_MASK,
424424
operators::INTEGER_ARITHMETIC,

clippy_lints/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ mod entry;
217217
mod enum_clike;
218218
mod enum_variants;
219219
mod equatable_if_let;
220-
mod erasing_op;
221220
mod escape;
222221
mod eta_reduction;
223222
mod excessive_bools;
@@ -550,7 +549,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
550549
store.register_late_pass(|| Box::new(misc::MiscLints));
551550
store.register_late_pass(|| Box::new(eta_reduction::EtaReduction));
552551
store.register_late_pass(|| Box::new(identity_op::IdentityOp));
553-
store.register_late_pass(|| Box::new(erasing_op::ErasingOp));
554552
store.register_late_pass(|| Box::new(mut_mut::MutMut));
555553
store.register_late_pass(|| Box::new(mut_reference::UnnecessaryMutPassed));
556554
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
@@ -9,6 +9,7 @@ mod bit_mask;
99
mod double_comparison;
1010
mod duration_subsec;
1111
mod eq_op;
12+
mod erasing_op;
1213
mod misrefactored_assign_op;
1314
mod op_ref;
1415
mod verbose_bit_mask;
@@ -354,6 +355,28 @@ declare_clippy_lint! {
354355
"taking a reference to satisfy the type constraints on `==`"
355356
}
356357

358+
declare_clippy_lint! {
359+
/// ### What it does
360+
/// Checks for erasing operations, e.g., `x * 0`.
361+
///
362+
/// ### Why is this bad?
363+
/// The whole expression can be replaced by zero.
364+
/// This is most likely not the intended outcome and should probably be
365+
/// corrected
366+
///
367+
/// ### Example
368+
/// ```rust
369+
/// let x = 1;
370+
/// 0 / x;
371+
/// 0 * x;
372+
/// x & 0;
373+
/// ```
374+
#[clippy::version = "pre 1.29.0"]
375+
pub ERASING_OP,
376+
correctness,
377+
"using erasing operations, e.g., `x * 0` or `y & 0`"
378+
}
379+
357380
pub struct Operators {
358381
arithmetic_context: arithmetic::Context,
359382
verbose_bit_mask_threshold: u64,
@@ -371,6 +394,7 @@ impl_lint_pass!(Operators => [
371394
DURATION_SUBSEC,
372395
EQ_OP,
373396
OP_REF,
397+
ERASING_OP,
374398
]);
375399
impl Operators {
376400
pub fn new(verbose_bit_mask_threshold: u64) -> Self {
@@ -391,6 +415,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
391415
eq_op::check(cx, e, op.node, lhs, rhs);
392416
op_ref::check(cx, e, op.node, lhs, rhs);
393417
}
418+
erasing_op::check(cx, e, op.node, lhs, rhs);
394419
}
395420
self.arithmetic_context.check_binary(cx, e, op.node, lhs, rhs);
396421
bit_mask::check(cx, e, op.node, lhs, rhs);

0 commit comments

Comments
 (0)