]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/identity_op.rs
Rollup merge of #74196 - GuillaumeGomez:auto-collapse-implementors, r=Manishearth
[rust.git] / src / tools / clippy / clippy_lints / src / identity_op.rs
1 use if_chain::if_chain;
2 use rustc_hir::{BinOp, BinOpKind, Expr, ExprKind};
3 use rustc_lint::{LateContext, LateLintPass};
4 use rustc_middle::ty;
5 use rustc_session::{declare_lint_pass, declare_tool_lint};
6 use rustc_span::source_map::Span;
7
8 use crate::consts::{constant_simple, Constant};
9 use crate::utils::{clip, snippet, span_lint, unsext};
10
11 declare_clippy_lint! {
12     /// **What it does:** Checks for identity operations, e.g., `x + 0`.
13     ///
14     /// **Why is this bad?** This code can be removed without changing the
15     /// meaning. So it just obscures what's going on. Delete it mercilessly.
16     ///
17     /// **Known problems:** None.
18     ///
19     /// **Example:**
20     /// ```rust
21     /// # let x = 1;
22     /// x / 1 + 0 * 1 - 0 | 0;
23     /// ```
24     pub IDENTITY_OP,
25     complexity,
26     "using identity operations, e.g., `x + 0` or `y / 1`"
27 }
28
29 declare_lint_pass!(IdentityOp => [IDENTITY_OP]);
30
31 impl<'tcx> LateLintPass<'tcx> for IdentityOp {
32     fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
33         if e.span.from_expansion() {
34             return;
35         }
36         if let ExprKind::Binary(cmp, ref left, ref right) = e.kind {
37             if is_allowed(cx, cmp, left, right) {
38                 return;
39             }
40             match cmp.node {
41                 BinOpKind::Add | BinOpKind::BitOr | BinOpKind::BitXor => {
42                     check(cx, left, 0, e.span, right.span);
43                     check(cx, right, 0, e.span, left.span);
44                 },
45                 BinOpKind::Shl | BinOpKind::Shr | BinOpKind::Sub => check(cx, right, 0, e.span, left.span),
46                 BinOpKind::Mul => {
47                     check(cx, left, 1, e.span, right.span);
48                     check(cx, right, 1, e.span, left.span);
49                 },
50                 BinOpKind::Div => check(cx, right, 1, e.span, left.span),
51                 BinOpKind::BitAnd => {
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 is_allowed(cx: &LateContext<'_>, cmp: BinOp, left: &Expr<'_>, right: &Expr<'_>) -> bool {
62     // `1 << 0` is a common pattern in bit manipulation code
63     if_chain! {
64         if let BinOpKind::Shl = cmp.node;
65         if let Some(Constant::Int(0)) = constant_simple(cx, cx.tables(), right);
66         if let Some(Constant::Int(1)) = constant_simple(cx, cx.tables(), left);
67         then {
68             return true;
69         }
70     }
71
72     false
73 }
74
75 #[allow(clippy::cast_possible_wrap)]
76 fn check(cx: &LateContext<'_>, e: &Expr<'_>, m: i8, span: Span, arg: Span) {
77     if let Some(Constant::Int(v)) = constant_simple(cx, cx.tables(), e) {
78         let check = match cx.tables().expr_ty(e).kind {
79             ty::Int(ity) => unsext(cx.tcx, -1_i128, ity),
80             ty::Uint(uty) => clip(cx.tcx, !0, uty),
81             _ => return,
82         };
83         if match m {
84             0 => v == 0,
85             -1 => v == check,
86             1 => v == 1,
87             _ => unreachable!(),
88         } {
89             span_lint(
90                 cx,
91                 IDENTITY_OP,
92                 span,
93                 &format!(
94                     "the operation is ineffective. Consider reducing it to `{}`",
95                     snippet(cx, arg, "..")
96                 ),
97             );
98         }
99     }
100 }