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