1 use consts::{constant_simple, Constant};
4 use syntax::codemap::Span;
5 use utils::{span_lint, snippet, in_macro};
6 use rustc_const_math::ConstInt;
8 /// **What it does:** This lint checks for identity operations, e.g. `x + 0`.
10 /// **Why is this bad?** This code can be removed without changing the meaning. So it just obscures what's going on. Delete it mercilessly.
12 /// **Known problems:** None
14 /// **Example:** `x / 1 + 0 * 1 - 0 | 0`
16 pub IDENTITY_OP, Warn,
17 "using identity operations, e.g. `x + 0` or `y / 1`"
21 pub struct IdentityOp;
23 impl LintPass for IdentityOp {
24 fn get_lints(&self) -> LintArray {
25 lint_array!(IDENTITY_OP)
29 impl LateLintPass for IdentityOp {
30 fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
31 if in_macro(cx, e.span) {
34 if let ExprBinary(ref cmp, ref left, ref right) = e.node {
36 BiAdd | BiBitOr | BiBitXor => {
37 check(cx, left, 0, e.span, right.span);
38 check(cx, right, 0, e.span, left.span);
40 BiShl | BiShr | BiSub => check(cx, right, 0, e.span, left.span),
42 check(cx, left, 1, e.span, right.span);
43 check(cx, right, 1, e.span, left.span);
45 BiDiv => check(cx, right, 1, e.span, left.span),
47 check(cx, left, -1, e.span, right.span);
48 check(cx, right, -1, e.span, left.span);
57 fn check(cx: &LateContext, e: &Expr, m: i8, span: Span, arg: Span) {
58 if let Some(v @ Constant::Int(_)) = constant_simple(e) {
60 0 => v == Constant::Int(ConstInt::Infer(0)),
61 -1 => v == Constant::Int(ConstInt::InferSigned(-1)),
62 1 => v == Constant::Int(ConstInt::Infer(1)),
68 &format!("the operation is ineffective. Consider reducing it to `{}`",
69 snippet(cx, arg, "..")));