1 use clippy_utils::consts::{constant_simple, Constant};
2 use clippy_utils::diagnostics::span_lint;
3 use clippy_utils::ty::same_type_and_consts;
5 use rustc_hir::{BinOpKind, Expr, ExprKind};
6 use rustc_lint::{LateContext, LateLintPass};
7 use rustc_middle::ty::TypeckResults;
8 use rustc_session::{declare_lint_pass, declare_tool_lint};
10 declare_clippy_lint! {
12 /// Checks for erasing operations, e.g., `x * 0`.
14 /// ### Why is this bad?
15 /// The whole expression can be replaced by zero.
16 /// This is most likely not the intended outcome and should probably be
26 #[clippy::version = "pre 1.29.0"]
29 "using erasing operations, e.g., `x * 0` or `y & 0`"
32 declare_lint_pass!(ErasingOp => [ERASING_OP]);
34 impl<'tcx> LateLintPass<'tcx> for ErasingOp {
35 fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
36 if e.span.from_expansion() {
39 if let ExprKind::Binary(ref cmp, left, right) = e.kind {
40 let tck = cx.typeck_results();
42 BinOpKind::Mul | BinOpKind::BitAnd => {
43 check(cx, tck, left, right, e);
44 check(cx, tck, right, left, e);
46 BinOpKind::Div => check(cx, tck, left, right, e),
53 fn different_types(tck: &TypeckResults<'_>, input: &Expr<'_>, output: &Expr<'_>) -> bool {
54 let input_ty = tck.expr_ty(input).peel_refs();
55 let output_ty = tck.expr_ty(output).peel_refs();
56 !same_type_and_consts(input_ty, output_ty)
60 cx: &LateContext<'tcx>,
61 tck: &TypeckResults<'tcx>,
66 if constant_simple(cx, tck, op) == Some(Constant::Int(0)) {
67 if different_types(tck, other, parent) {
74 "this operation will always return zero. This is likely not the intended outcome",