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