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