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