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