2 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
3 use rustc::{declare_tool_lint, lint_array};
4 use syntax::source_map::Span;
6 use crate::consts::{constant_simple, Constant};
7 use crate::utils::{in_macro, 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 #[derive(Copy, Clone)]
33 impl LintPass for ErasingOp {
34 fn get_lints(&self) -> LintArray {
35 lint_array!(ERASING_OP)
38 fn name(&self) -> &'static str {
43 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ErasingOp {
44 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
48 if let ExprKind::Binary(ref cmp, ref left, ref right) = e.node {
50 BinOpKind::Mul | BinOpKind::BitAnd => {
51 check(cx, left, e.span);
52 check(cx, right, e.span);
54 BinOpKind::Div => check(cx, left, e.span),
61 fn check(cx: &LateContext<'_, '_>, e: &Expr, span: Span) {
62 if let Some(Constant::Int(v)) = constant_simple(cx, cx.tables, e) {
68 "this operation will always return zero. This is likely not the intended outcome",