]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/identity_op.rs
Rollup merge of #96142 - cjgillot:no-crate-def-index, r=petrochenkov
[rust.git] / 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_full_int, constant_simple, Constant, FullInt};
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     #[clippy::version = "pre 1.29.0"]
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                 BinOpKind::Rem => check_remainder(cx, left, right, e.span, left.span),
58                 _ => (),
59             }
60         }
61     }
62 }
63
64 fn is_allowed(cx: &LateContext<'_>, cmp: BinOp, left: &Expr<'_>, right: &Expr<'_>) -> bool {
65     // This lint applies to integers
66     !cx.typeck_results().expr_ty(left).peel_refs().is_integral()
67         || !cx.typeck_results().expr_ty(right).peel_refs().is_integral()
68         // `1 << 0` is a common pattern in bit manipulation code
69         || (cmp.node == BinOpKind::Shl
70             && constant_simple(cx, cx.typeck_results(), right) == Some(Constant::Int(0))
71             && constant_simple(cx, cx.typeck_results(), left) == Some(Constant::Int(1)))
72 }
73
74 fn check_remainder(cx: &LateContext<'_>, left: &Expr<'_>, right: &Expr<'_>, span: Span, arg: Span) {
75     let lhs_const = constant_full_int(cx, cx.typeck_results(), left);
76     let rhs_const = constant_full_int(cx, cx.typeck_results(), right);
77     if match (lhs_const, rhs_const) {
78         (Some(FullInt::S(lv)), Some(FullInt::S(rv))) => lv.abs() < rv.abs(),
79         (Some(FullInt::U(lv)), Some(FullInt::U(rv))) => lv < rv,
80         _ => return,
81     } {
82         span_ineffective_operation(cx, span, arg);
83     }
84 }
85
86 fn check(cx: &LateContext<'_>, e: &Expr<'_>, m: i8, span: Span, arg: Span) {
87     if let Some(Constant::Int(v)) = constant_simple(cx, cx.typeck_results(), e).map(Constant::peel_refs) {
88         let check = match *cx.typeck_results().expr_ty(e).peel_refs().kind() {
89             ty::Int(ity) => unsext(cx.tcx, -1_i128, ity),
90             ty::Uint(uty) => clip(cx.tcx, !0, uty),
91             _ => return,
92         };
93         if match m {
94             0 => v == 0,
95             -1 => v == check,
96             1 => v == 1,
97             _ => unreachable!(),
98         } {
99             span_ineffective_operation(cx, span, arg);
100         }
101     }
102 }
103
104 fn span_ineffective_operation(cx: &LateContext<'_>, span: Span, arg: Span) {
105     span_lint(
106         cx,
107         IDENTITY_OP,
108         span,
109         &format!(
110             "the operation is ineffective. Consider reducing it to `{}`",
111             snippet(cx, arg, "..")
112         ),
113     );
114 }