-// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
+use rustc_hir::{BinOpKind, Expr, ExprKind};
+use rustc_lint::{LateContext, LateLintPass};
+use rustc_session::{declare_lint_pass, declare_tool_lint};
+use rustc_span::source_map::Span;
use crate::consts::{constant_simple, Constant};
-use crate::rustc::hir::*;
-use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
-use crate::rustc::{declare_tool_lint, lint_array};
-use crate::syntax::source_map::Span;
-use crate::utils::{in_macro, span_lint};
+use crate::utils::span_lint;
-/// **What it does:** Checks for erasing operations, e.g. `x * 0`.
-///
-/// **Why is this bad?** The whole expression can be replaced by zero.
-/// This is most likely not the intended outcome and should probably be
-/// corrected
-///
-/// **Known problems:** None.
-///
-/// **Example:**
-/// ```rust
-/// 0 / x; 0 * x; x & 0
-/// ```
declare_clippy_lint! {
+ /// **What it does:** Checks for erasing operations, e.g., `x * 0`.
+ ///
+ /// **Why is this bad?** The whole expression can be replaced by zero.
+ /// This is most likely not the intended outcome and should probably be
+ /// corrected
+ ///
+ /// **Known problems:** None.
+ ///
+ /// **Example:**
+ /// ```rust
+ /// let x = 1;
+ /// 0 / x;
+ /// 0 * x;
+ /// x & 0;
+ /// ```
pub ERASING_OP,
correctness,
- "using erasing operations, e.g. `x * 0` or `y & 0`"
+ "using erasing operations, e.g., `x * 0` or `y & 0`"
}
-#[derive(Copy, Clone)]
-pub struct ErasingOp;
+declare_lint_pass!(ErasingOp => [ERASING_OP]);
-impl LintPass for ErasingOp {
- fn get_lints(&self) -> LintArray {
- lint_array!(ERASING_OP)
- }
-}
-
-impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ErasingOp {
- fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
- if in_macro(e.span) {
+impl<'tcx> LateLintPass<'tcx> for ErasingOp {
+ fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
+ if e.span.from_expansion() {
return;
}
- if let ExprKind::Binary(ref cmp, ref left, ref right) = e.node {
+ if let ExprKind::Binary(ref cmp, ref left, ref right) = e.kind {
match cmp.node {
BinOpKind::Mul | BinOpKind::BitAnd => {
check(cx, left, e.span);
}
}
-fn check(cx: &LateContext<'_, '_>, e: &Expr, span: Span) {
- if let Some(Constant::Int(v)) = constant_simple(cx, cx.tables, e) {
- if v == 0 {
- span_lint(
- cx,
- ERASING_OP,
- span,
- "this operation will always return zero. This is likely not the intended outcome",
- );
- }
+fn check(cx: &LateContext<'_>, e: &Expr<'_>, span: Span) {
+ if let Some(Constant::Int(0)) = constant_simple(cx, cx.typeck_results(), e) {
+ span_lint(
+ cx,
+ ERASING_OP,
+ span,
+ "this operation will always return zero. This is likely not the intended outcome",
+ );
}
}