]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/identity_op.rs
Fix lines that exceed max width manually
[rust.git] / clippy_lints / src / identity_op.rs
1 use consts::{constant_simple, Constant};
2 use rustc::hir::*;
3 use rustc::lint::*;
4 use rustc_const_math::ConstInt;
5 use syntax::codemap::Span;
6 use utils::{in_macro, snippet, span_lint};
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_lint! {
20     pub IDENTITY_OP,
21     Warn,
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 ExprBinary(ref cmp, ref left, ref right) = e.node {
40             match cmp.node {
41                 BiAdd | BiBitOr | BiBitXor => {
42                     check(cx, left, 0, e.span, right.span);
43                     check(cx, right, 0, e.span, left.span);
44                 },
45                 BiShl | BiShr | BiSub => check(cx, right, 0, e.span, left.span),
46                 BiMul => {
47                     check(cx, left, 1, e.span, right.span);
48                     check(cx, right, 1, e.span, left.span);
49                 },
50                 BiDiv => check(cx, right, 1, e.span, left.span),
51                 BiBitAnd => {
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 fn all_ones(v: &ConstInt) -> bool {
62     match *v {
63         ConstInt::I8(i) => i == !0,
64         ConstInt::I16(i) => i == !0,
65         ConstInt::I32(i) => i == !0,
66         ConstInt::I64(i) => i == !0,
67         ConstInt::I128(i) => i == !0,
68         ConstInt::U8(i) => i == !0,
69         ConstInt::U16(i) => i == !0,
70         ConstInt::U32(i) => i == !0,
71         ConstInt::U64(i) => i == !0,
72         ConstInt::U128(i) => i == !0,
73         _ => false,
74     }
75 }
76
77 #[allow(cast_possible_wrap)]
78 fn check(cx: &LateContext, e: &Expr, m: i8, span: Span, arg: Span) {
79     if let Some(Constant::Int(v)) = constant_simple(cx, e) {
80         if match m {
81             0 => v.to_u128_unchecked() == 0,
82             -1 => all_ones(&v),
83             1 => v.to_u128_unchecked() == 1,
84             _ => unreachable!(),
85         } {
86             span_lint(
87                 cx,
88                 IDENTITY_OP,
89                 span,
90                 &format!(
91                     "the operation is ineffective. Consider reducing it to `{}`",
92                     snippet(cx, arg, "..")
93                 ),
94             );
95         }
96     }
97 }