1 use clippy_utils::consts::{constant_simple, Constant};
2 use clippy_utils::diagnostics::span_lint;
3 use rustc_hir::{BinOpKind, Expr, ExprKind};
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_session::{declare_lint_pass, declare_tool_lint};
6 use rustc_span::source_map::Span;
9 /// **What it does:** Checks for erasing operations, e.g., `x * 0`.
11 /// **Why is this bad?** The whole expression can be replaced by zero.
12 /// This is most likely not the intended outcome and should probably be
15 /// **Known problems:** None.
26 "using erasing operations, e.g., `x * 0` or `y & 0`"
29 declare_lint_pass!(ErasingOp => [ERASING_OP]);
31 impl<'tcx> LateLintPass<'tcx> for ErasingOp {
32 fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
33 if e.span.from_expansion() {
36 if let ExprKind::Binary(ref cmp, left, right) = e.kind {
38 BinOpKind::Mul | BinOpKind::BitAnd => {
39 check(cx, left, e.span);
40 check(cx, right, e.span);
42 BinOpKind::Div => check(cx, left, e.span),
49 fn check(cx: &LateContext<'_>, e: &Expr<'_>, span: Span) {
50 if let Some(Constant::Int(0)) = constant_simple(cx, cx.typeck_results(), e) {
55 "this operation will always return zero. This is likely not the intended outcome",