1 use consts::{constant_simple, Constant};
4 use rustc_const_math::ConstInt;
5 use syntax::codemap::Span;
6 use utils::{in_macro, snippet, span_lint};
8 /// **What it does:** Checks for identity operations, e.g. `x + 0`.
10 /// **Why is this bad?** This code can be removed without changing the
11 /// meaning. So it just obscures what's going on. Delete it mercilessly.
13 /// **Known problems:** None.
17 /// x / 1 + 0 * 1 - 0 | 0
22 "using identity operations, e.g. `x + 0` or `y / 1`"
25 #[derive(Copy, Clone)]
26 pub struct IdentityOp;
28 impl LintPass for IdentityOp {
29 fn get_lints(&self) -> LintArray {
30 lint_array!(IDENTITY_OP)
34 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityOp {
35 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
39 if let ExprBinary(ref cmp, ref left, ref right) = e.node {
41 BiAdd | BiBitOr | BiBitXor => {
42 check(cx, left, 0, e.span, right.span);
43 check(cx, right, 0, e.span, left.span);
45 BiShl | BiShr | BiSub => check(cx, right, 0, e.span, left.span),
47 check(cx, left, 1, e.span, right.span);
48 check(cx, right, 1, e.span, left.span);
50 BiDiv => check(cx, right, 1, e.span, left.span),
52 check(cx, left, -1, e.span, right.span);
53 check(cx, right, -1, e.span, left.span);
61 fn all_ones(v: &ConstInt) -> bool {
63 ConstInt::I8(i) => i == !0,
64 ConstInt::I16(i) => i == !0,
65 ConstInt::I32(i) => i == !0,
66 ConstInt::I64(i) => i == !0,
67 ConstInt::I128(i) => i == !0,
68 ConstInt::U8(i) => i == !0,
69 ConstInt::U16(i) => i == !0,
70 ConstInt::U32(i) => i == !0,
71 ConstInt::U64(i) => i == !0,
72 ConstInt::U128(i) => i == !0,
77 #[allow(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, e) {
81 0 => v.to_u128_unchecked() == 0,
83 1 => v.to_u128_unchecked() == 1,
91 "the operation is ineffective. Consider reducing it to `{}`",
92 snippet(cx, arg, "..")