]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/identity_op.rs
Auto merge of #3946 - rchaser53:issue-3920, r=flip1995
[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_tool_lint, lint_array};
5 use syntax::source_map::Span;
6
7 use crate::consts::{constant_simple, Constant};
8 use crate::utils::{clip, in_macro, 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 #[derive(Copy, Clone)]
28 pub struct IdentityOp;
29
30 impl LintPass for IdentityOp {
31     fn get_lints(&self) -> LintArray {
32         lint_array!(IDENTITY_OP)
33     }
34
35     fn name(&self) -> &'static str {
36         "IdentityOp"
37     }
38 }
39
40 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityOp {
41     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
42         if in_macro(e.span) {
43             return;
44         }
45         if let ExprKind::Binary(ref cmp, ref left, ref right) = e.node {
46             match cmp.node {
47                 BinOpKind::Add | BinOpKind::BitOr | BinOpKind::BitXor => {
48                     check(cx, left, 0, e.span, right.span);
49                     check(cx, right, 0, e.span, left.span);
50                 },
51                 BinOpKind::Shl | BinOpKind::Shr | BinOpKind::Sub => check(cx, right, 0, e.span, left.span),
52                 BinOpKind::Mul => {
53                     check(cx, left, 1, e.span, right.span);
54                     check(cx, right, 1, e.span, left.span);
55                 },
56                 BinOpKind::Div => check(cx, right, 1, e.span, left.span),
57                 BinOpKind::BitAnd => {
58                     check(cx, left, -1, e.span, right.span);
59                     check(cx, right, -1, e.span, left.span);
60                 },
61                 _ => (),
62             }
63         }
64     }
65 }
66
67 #[allow(clippy::cast_possible_wrap)]
68 fn check(cx: &LateContext<'_, '_>, e: &Expr, m: i8, span: Span, arg: Span) {
69     if let Some(Constant::Int(v)) = constant_simple(cx, cx.tables, e) {
70         let check = match cx.tables.expr_ty(e).sty {
71             ty::Int(ity) => unsext(cx.tcx, -1_i128, ity),
72             ty::Uint(uty) => clip(cx.tcx, !0, uty),
73             _ => return,
74         };
75         if match m {
76             0 => v == 0,
77             -1 => v == check,
78             1 => v == 1,
79             _ => unreachable!(),
80         } {
81             span_lint(
82                 cx,
83                 IDENTITY_OP,
84                 span,
85                 &format!(
86                     "the operation is ineffective. Consider reducing it to `{}`",
87                     snippet(cx, arg, "..")
88                 ),
89             );
90         }
91     }
92 }