2 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4 use rustc::{declare_lint_pass, declare_tool_lint};
5 use syntax::source_map::Span;
7 use crate::consts::{constant_simple, Constant};
8 use crate::utils::{clip, in_macro_or_desugar, snippet, span_lint, unsext};
10 declare_clippy_lint! {
11 /// **What it does:** Checks for identity operations, e.g., `x + 0`.
13 /// **Why is this bad?** This code can be removed without changing the
14 /// meaning. So it just obscures what's going on. Delete it mercilessly.
16 /// **Known problems:** None.
20 /// x / 1 + 0 * 1 - 0 | 0
24 "using identity operations, e.g., `x + 0` or `y / 1`"
27 declare_lint_pass!(IdentityOp => [IDENTITY_OP]);
29 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityOp {
30 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
31 if in_macro_or_desugar(e.span) {
34 if let ExprKind::Binary(ref cmp, ref left, ref right) = e.node {
36 BinOpKind::Add | BinOpKind::BitOr | BinOpKind::BitXor => {
37 check(cx, left, 0, e.span, right.span);
38 check(cx, right, 0, e.span, left.span);
40 BinOpKind::Shl | BinOpKind::Shr | BinOpKind::Sub => check(cx, right, 0, e.span, left.span),
42 check(cx, left, 1, e.span, right.span);
43 check(cx, right, 1, e.span, left.span);
45 BinOpKind::Div => check(cx, right, 1, e.span, left.span),
46 BinOpKind::BitAnd => {
47 check(cx, left, -1, e.span, right.span);
48 check(cx, right, -1, e.span, left.span);
56 #[allow(clippy::cast_possible_wrap)]
57 fn check(cx: &LateContext<'_, '_>, e: &Expr, m: i8, span: Span, arg: Span) {
58 if let Some(Constant::Int(v)) = constant_simple(cx, cx.tables, e) {
59 let check = match cx.tables.expr_ty(e).sty {
60 ty::Int(ity) => unsext(cx.tcx, -1_i128, ity),
61 ty::Uint(uty) => clip(cx.tcx, !0, uty),
75 "the operation is ineffective. Consider reducing it to `{}`",
76 snippet(cx, arg, "..")