1 use crate::consts::{constant_simple, Constant};
2 use crate::utils::{in_macro, span_lint};
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc::{declare_tool_lint, lint_array};
6 use syntax::source_map::Span;
8 /// **What it does:** Checks for erasing operations, e.g. `x * 0`.
10 /// **Why is this bad?** The whole expression can be replaced by zero.
11 /// This is most likely not the intended outcome and should probably be
14 /// **Known problems:** None.
22 declare_clippy_lint! {
25 "using erasing operations, e.g. `x * 0` or `y & 0`"
28 #[derive(Copy, Clone)]
31 impl LintPass for ErasingOp {
32 fn get_lints(&self) -> LintArray {
33 lint_array!(ERASING_OP)
37 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ErasingOp {
38 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
42 if let ExprKind::Binary(ref cmp, ref left, ref right) = e.node {
44 BinOpKind::Mul | BinOpKind::BitAnd => {
45 check(cx, left, e.span);
46 check(cx, right, e.span);
48 BinOpKind::Div => check(cx, left, e.span),
55 fn check(cx: &LateContext<'_, '_>, e: &Expr, span: Span) {
56 if let Some(Constant::Int(v)) = constant_simple(cx, cx.tables, e) {
62 "this operation will always return zero. This is likely not the intended outcome",