]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/identity_op.rs
Merge pull request #3049 from mikerite/fix-2799
[rust.git] / clippy_lints / src / identity_op.rs
1 use crate::consts::{constant_simple, Constant};
2 use rustc::hir::*;
3 use rustc::lint::*;
4 use rustc::{declare_lint, lint_array};
5 use syntax::codemap::Span;
6 use crate::utils::{in_macro, snippet, span_lint, unsext, clip};
7 use rustc::ty;
8
9 /// **What it does:** Checks for identity operations, e.g. `x + 0`.
10 ///
11 /// **Why is this bad?** This code can be removed without changing the
12 /// meaning. So it just obscures what's going on. Delete it mercilessly.
13 ///
14 /// **Known problems:** None.
15 ///
16 /// **Example:**
17 /// ```rust
18 /// x / 1 + 0 * 1 - 0 | 0
19 /// ```
20 declare_clippy_lint! {
21     pub IDENTITY_OP,
22     complexity,
23     "using identity operations, e.g. `x + 0` or `y / 1`"
24 }
25
26 #[derive(Copy, Clone)]
27 pub struct IdentityOp;
28
29 impl LintPass for IdentityOp {
30     fn get_lints(&self) -> LintArray {
31         lint_array!(IDENTITY_OP)
32     }
33 }
34
35 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityOp {
36     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
37         if in_macro(e.span) {
38             return;
39         }
40         if let ExprKind::Binary(ref cmp, ref left, ref right) = e.node {
41             match cmp.node {
42                 BinOpKind::Add | BinOpKind::BitOr | BinOpKind::BitXor => {
43                     check(cx, left, 0, e.span, right.span);
44                     check(cx, right, 0, e.span, left.span);
45                 },
46                 BinOpKind::Shl | BinOpKind::Shr | BinOpKind::Sub => check(cx, right, 0, e.span, left.span),
47                 BinOpKind::Mul => {
48                     check(cx, left, 1, e.span, right.span);
49                     check(cx, right, 1, e.span, left.span);
50                 },
51                 BinOpKind::Div => check(cx, right, 1, e.span, left.span),
52                 BinOpKind::BitAnd => {
53                     check(cx, left, -1, e.span, right.span);
54                     check(cx, right, -1, e.span, left.span);
55                 },
56                 _ => (),
57             }
58         }
59     }
60 }
61
62 #[allow(cast_possible_wrap)]
63 fn check(cx: &LateContext<'_, '_>, e: &Expr, m: i8, span: Span, arg: Span) {
64     if let Some(Constant::Int(v)) = constant_simple(cx, cx.tables, e) {
65         let check = match cx.tables.expr_ty(e).sty {
66             ty::TyInt(ity) => unsext(cx.tcx, -1_i128, ity),
67             ty::TyUint(uty) => clip(cx.tcx, !0, uty),
68             _ => return,
69         };
70         if match m {
71             0 => v == 0,
72             -1 => v == check,
73             1 => v == 1,
74             _ => unreachable!(),
75         } {
76             span_lint(
77                 cx,
78                 IDENTITY_OP,
79                 span,
80                 &format!(
81                     "the operation is ineffective. Consider reducing it to `{}`",
82                     snippet(cx, arg, "..")
83                 ),
84             );
85         }
86     }
87 }