]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/identity_op.rs
Add 'src/tools/clippy/' from commit 'd2708873ef711ec8ab45df1e984ecf24a96cd369'
[rust.git] / src / tools / clippy / clippy_lints / src / identity_op.rs
1 use rustc_hir::{BinOpKind, Expr, ExprKind};
2 use rustc_lint::{LateContext, LateLintPass};
3 use rustc_middle::ty;
4 use rustc_session::{declare_lint_pass, declare_tool_lint};
5 use rustc_span::source_map::Span;
6
7 use crate::consts::{constant_simple, Constant};
8 use crate::utils::{clip, 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     /// # let x = 1;
21     /// x / 1 + 0 * 1 - 0 | 0;
22     /// ```
23     pub IDENTITY_OP,
24     complexity,
25     "using identity operations, e.g., `x + 0` or `y / 1`"
26 }
27
28 declare_lint_pass!(IdentityOp => [IDENTITY_OP]);
29
30 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityOp {
31     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) {
32         if e.span.from_expansion() {
33             return;
34         }
35         if let ExprKind::Binary(ref cmp, ref left, ref right) = e.kind {
36             match cmp.node {
37                 BinOpKind::Add | BinOpKind::BitOr | BinOpKind::BitXor => {
38                     check(cx, left, 0, e.span, right.span);
39                     check(cx, right, 0, e.span, left.span);
40                 },
41                 BinOpKind::Shl | BinOpKind::Shr | BinOpKind::Sub => check(cx, right, 0, e.span, left.span),
42                 BinOpKind::Mul => {
43                     check(cx, left, 1, e.span, right.span);
44                     check(cx, right, 1, e.span, left.span);
45                 },
46                 BinOpKind::Div => check(cx, right, 1, e.span, left.span),
47                 BinOpKind::BitAnd => {
48                     check(cx, left, -1, e.span, right.span);
49                     check(cx, right, -1, e.span, left.span);
50                 },
51                 _ => (),
52             }
53         }
54     }
55 }
56
57 #[allow(clippy::cast_possible_wrap)]
58 fn check(cx: &LateContext<'_, '_>, e: &Expr<'_>, m: i8, span: Span, arg: Span) {
59     if let Some(Constant::Int(v)) = constant_simple(cx, cx.tables, e) {
60         let check = match cx.tables.expr_ty(e).kind {
61             ty::Int(ity) => unsext(cx.tcx, -1_i128, ity),
62             ty::Uint(uty) => clip(cx.tcx, !0, uty),
63             _ => return,
64         };
65         if match m {
66             0 => v == 0,
67             -1 => v == check,
68             1 => v == 1,
69             _ => unreachable!(),
70         } {
71             span_lint(
72                 cx,
73                 IDENTITY_OP,
74                 span,
75                 &format!(
76                     "the operation is ineffective. Consider reducing it to `{}`",
77                     snippet(cx, arg, "..")
78                 ),
79             );
80         }
81     }
82 }