]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/identity_op.rs
Auto merge of #3593 - mikerite:readme-syspath-2, r=phansch
[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 use crate::consts::{constant_simple, Constant};
11 use crate::utils::{clip, in_macro, snippet, span_lint, unsext};
12 use rustc::hir::*;
13 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
14 use rustc::ty;
15 use rustc::{declare_tool_lint, lint_array};
16 use syntax::source_map::Span;
17
18 /// **What it does:** Checks for identity operations, e.g. `x + 0`.
19 ///
20 /// **Why is this bad?** This code can be removed without changing the
21 /// meaning. So it just obscures what's going on. Delete it mercilessly.
22 ///
23 /// **Known problems:** None.
24 ///
25 /// **Example:**
26 /// ```rust
27 /// x / 1 + 0 * 1 - 0 | 0
28 /// ```
29 declare_clippy_lint! {
30     pub IDENTITY_OP,
31     complexity,
32     "using identity operations, e.g. `x + 0` or `y / 1`"
33 }
34
35 #[derive(Copy, Clone)]
36 pub struct IdentityOp;
37
38 impl LintPass for IdentityOp {
39     fn get_lints(&self) -> LintArray {
40         lint_array!(IDENTITY_OP)
41     }
42 }
43
44 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityOp {
45     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
46         if in_macro(e.span) {
47             return;
48         }
49         if let ExprKind::Binary(ref cmp, ref left, ref right) = e.node {
50             match cmp.node {
51                 BinOpKind::Add | BinOpKind::BitOr | BinOpKind::BitXor => {
52                     check(cx, left, 0, e.span, right.span);
53                     check(cx, right, 0, e.span, left.span);
54                 },
55                 BinOpKind::Shl | BinOpKind::Shr | BinOpKind::Sub => check(cx, right, 0, e.span, left.span),
56                 BinOpKind::Mul => {
57                     check(cx, left, 1, e.span, right.span);
58                     check(cx, right, 1, e.span, left.span);
59                 },
60                 BinOpKind::Div => check(cx, right, 1, e.span, left.span),
61                 BinOpKind::BitAnd => {
62                     check(cx, left, -1, e.span, right.span);
63                     check(cx, right, -1, e.span, left.span);
64                 },
65                 _ => (),
66             }
67         }
68     }
69 }
70
71 #[allow(clippy::cast_possible_wrap)]
72 fn check(cx: &LateContext<'_, '_>, e: &Expr, m: i8, span: Span, arg: Span) {
73     if let Some(Constant::Int(v)) = constant_simple(cx, cx.tables, e) {
74         let check = match cx.tables.expr_ty(e).sty {
75             ty::Int(ity) => unsext(cx.tcx, -1_i128, ity),
76             ty::Uint(uty) => clip(cx.tcx, !0, uty),
77             _ => return,
78         };
79         if match m {
80             0 => v == 0,
81             -1 => v == check,
82             1 => v == 1,
83             _ => unreachable!(),
84         } {
85             span_lint(
86                 cx,
87                 IDENTITY_OP,
88                 span,
89                 &format!(
90                     "the operation is ineffective. Consider reducing it to `{}`",
91                     snippet(cx, arg, "..")
92                 ),
93             );
94         }
95     }
96 }