rust/clippy_lints/src/identity_op.rs

97 lines
3.2 KiB
Rust
Raw Normal View History

2018-10-06 09:18:06 -07:00
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
2018-05-30 10:15:50 +02:00
use crate::consts::{constant_simple, Constant};
2018-11-27 21:14:15 +01:00
use crate::utils::{clip, in_macro, snippet, span_lint, unsext};
use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use rustc::ty;
use rustc::{declare_tool_lint, lint_array};
use syntax::source_map::Span;
/// **What it does:** Checks for identity operations, e.g. `x + 0`.
///
/// **Why is this bad?** This code can be removed without changing the
/// meaning. So it just obscures what's going on. Delete it mercilessly.
///
/// **Known problems:** None.
///
2016-07-16 00:25:44 +02:00
/// **Example:**
/// ```rust
/// x / 1 + 0 * 1 - 0 | 0
/// ```
2018-03-28 15:24:26 +02:00
declare_clippy_lint! {
pub IDENTITY_OP,
2018-03-28 15:24:26 +02:00
complexity,
"using identity operations, e.g. `x + 0` or `y / 1`"
}
2017-08-09 09:30:56 +02:00
#[derive(Copy, Clone)]
pub struct IdentityOp;
impl LintPass for IdentityOp {
fn get_lints(&self) -> LintArray {
lint_array!(IDENTITY_OP)
}
}
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityOp {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
if in_macro(e.span) {
2016-01-04 09:56:12 +05:30
return;
}
2018-07-12 15:30:57 +08:00
if let ExprKind::Binary(ref cmp, ref left, ref right) = e.node {
match cmp.node {
2018-07-12 15:50:09 +08:00
BinOpKind::Add | BinOpKind::BitOr | BinOpKind::BitXor => {
check(cx, left, 0, e.span, right.span);
check(cx, right, 0, e.span, left.span);
2016-12-20 18:21:30 +01:00
},
2018-07-12 15:50:09 +08:00
BinOpKind::Shl | BinOpKind::Shr | BinOpKind::Sub => check(cx, right, 0, e.span, left.span),
BinOpKind::Mul => {
check(cx, left, 1, e.span, right.span);
check(cx, right, 1, e.span, left.span);
2016-12-20 18:21:30 +01:00
},
2018-07-12 15:50:09 +08:00
BinOpKind::Div => check(cx, right, 1, e.span, left.span),
BinOpKind::BitAnd => {
check(cx, left, -1, e.span, right.span);
check(cx, right, -1, e.span, left.span);
2016-12-20 18:21:30 +01:00
},
2016-01-04 09:56:12 +05:30
_ => (),
}
}
}
}
2018-08-01 22:48:41 +02:00
#[allow(clippy::cast_possible_wrap)]
2018-07-23 13:01:12 +02:00
fn check(cx: &LateContext<'_, '_>, e: &Expr, m: i8, span: Span, arg: Span) {
2018-05-13 13:16:31 +02:00
if let Some(Constant::Int(v)) = constant_simple(cx, cx.tables, e) {
2018-03-13 11:38:11 +01:00
let check = match cx.tables.expr_ty(e).sty {
ty::Int(ity) => unsext(cx.tcx, -1_i128, ity),
ty::Uint(uty) => clip(cx.tcx, !0, uty),
2018-03-13 11:38:11 +01:00
_ => return,
};
2015-09-06 10:53:55 +02:00
if match m {
2018-03-13 11:38:11 +01:00
0 => v == 0,
-1 => v == check,
1 => v == 1,
2015-09-06 10:53:55 +02:00
_ => unreachable!(),
2017-09-05 11:33:04 +02:00
} {
2017-08-09 09:30:56 +02:00
span_lint(
cx,
IDENTITY_OP,
span,
&format!(
"the operation is ineffective. Consider reducing it to `{}`",
snippet(cx, arg, "..")
),
);
}
}
}