1 use rustc_hir::{BinOpKind, Expr, ExprKind};
2 use rustc_lint::{LateContext, LateLintPass};
3 use rustc_session::{declare_lint_pass, declare_tool_lint};
4 use rustc_span::source_map::Span;
6 use crate::consts::{constant_simple, Constant};
7 use crate::utils::span_lint;
10 /// **What it does:** Checks for erasing operations, e.g., `x * 0`.
12 /// **Why is this bad?** The whole expression can be replaced by zero.
13 /// This is most likely not the intended outcome and should probably be
16 /// **Known problems:** None.
27 "using erasing operations, e.g., `x * 0` or `y & 0`"
30 declare_lint_pass!(ErasingOp => [ERASING_OP]);
32 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ErasingOp {
33 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) {
34 if e.span.from_expansion() {
37 if let ExprKind::Binary(ref cmp, ref left, ref right) = e.kind {
39 BinOpKind::Mul | BinOpKind::BitAnd => {
40 check(cx, left, e.span);
41 check(cx, right, e.span);
43 BinOpKind::Div => check(cx, left, e.span),
50 fn check(cx: &LateContext<'_, '_>, e: &Expr<'_>, span: Span) {
51 if let Some(Constant::Int(0)) = constant_simple(cx, cx.tables, e) {
56 "this operation will always return zero. This is likely not the intended outcome",