]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/identity_op.rs
Merge pull request #3265 from mikerite/fix-export
[rust.git] / clippy_lints / src / identity_op.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10
11 use crate::consts::{constant_simple, Constant};
12 use crate::rustc::hir::*;
13 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
14 use crate::rustc::{declare_tool_lint, lint_array};
15 use crate::syntax::source_map::Span;
16 use crate::utils::{in_macro, snippet, span_lint, unsext, clip};
17 use crate::rustc::ty;
18
19 /// **What it does:** Checks for identity operations, e.g. `x + 0`.
20 ///
21 /// **Why is this bad?** This code can be removed without changing the
22 /// meaning. So it just obscures what's going on. Delete it mercilessly.
23 ///
24 /// **Known problems:** None.
25 ///
26 /// **Example:**
27 /// ```rust
28 /// x / 1 + 0 * 1 - 0 | 0
29 /// ```
30 declare_clippy_lint! {
31     pub IDENTITY_OP,
32     complexity,
33     "using identity operations, e.g. `x + 0` or `y / 1`"
34 }
35
36 #[derive(Copy, Clone)]
37 pub struct IdentityOp;
38
39 impl LintPass for IdentityOp {
40     fn get_lints(&self) -> LintArray {
41         lint_array!(IDENTITY_OP)
42     }
43 }
44
45 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityOp {
46     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
47         if in_macro(e.span) {
48             return;
49         }
50         if let ExprKind::Binary(ref cmp, ref left, ref right) = e.node {
51             match cmp.node {
52                 BinOpKind::Add | BinOpKind::BitOr | BinOpKind::BitXor => {
53                     check(cx, left, 0, e.span, right.span);
54                     check(cx, right, 0, e.span, left.span);
55                 },
56                 BinOpKind::Shl | BinOpKind::Shr | BinOpKind::Sub => check(cx, right, 0, e.span, left.span),
57                 BinOpKind::Mul => {
58                     check(cx, left, 1, e.span, right.span);
59                     check(cx, right, 1, e.span, left.span);
60                 },
61                 BinOpKind::Div => check(cx, right, 1, e.span, left.span),
62                 BinOpKind::BitAnd => {
63                     check(cx, left, -1, e.span, right.span);
64                     check(cx, right, -1, e.span, left.span);
65                 },
66                 _ => (),
67             }
68         }
69     }
70 }
71
72 #[allow(clippy::cast_possible_wrap)]
73 fn check(cx: &LateContext<'_, '_>, e: &Expr, m: i8, span: Span, arg: Span) {
74     if let Some(Constant::Int(v)) = constant_simple(cx, cx.tables, e) {
75         let check = match cx.tables.expr_ty(e).sty {
76             ty::Int(ity) => unsext(cx.tcx, -1_i128, ity),
77             ty::Uint(uty) => clip(cx.tcx, !0, uty),
78             _ => return,
79         };
80         if match m {
81             0 => v == 0,
82             -1 => v == check,
83             1 => v == 1,
84             _ => unreachable!(),
85         } {
86             span_lint(
87                 cx,
88                 IDENTITY_OP,
89                 span,
90                 &format!(
91                     "the operation is ineffective. Consider reducing it to `{}`",
92                     snippet(cx, arg, "..")
93                 ),
94             );
95         }
96     }
97 }