1 use rustc::declare_lint_pass;
3 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc_session::declare_tool_lint;
6 use rustc_span::source_map::Span;
8 use crate::consts::{constant_simple, Constant};
9 use crate::utils::{clip, snippet, span_lint, unsext};
11 declare_clippy_lint! {
12 /// **What it does:** Checks for identity operations, e.g., `x + 0`.
14 /// **Why is this bad?** This code can be removed without changing the
15 /// meaning. So it just obscures what's going on. Delete it mercilessly.
17 /// **Known problems:** None.
22 /// x / 1 + 0 * 1 - 0 | 0;
26 "using identity operations, e.g., `x + 0` or `y / 1`"
29 declare_lint_pass!(IdentityOp => [IDENTITY_OP]);
31 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityOp {
32 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr<'_>) {
33 if e.span.from_expansion() {
36 if let ExprKind::Binary(ref cmp, ref left, ref right) = e.kind {
38 BinOpKind::Add | BinOpKind::BitOr | BinOpKind::BitXor => {
39 check(cx, left, 0, e.span, right.span);
40 check(cx, right, 0, e.span, left.span);
42 BinOpKind::Shl | BinOpKind::Shr | BinOpKind::Sub => check(cx, right, 0, e.span, left.span),
44 check(cx, left, 1, e.span, right.span);
45 check(cx, right, 1, e.span, left.span);
47 BinOpKind::Div => check(cx, right, 1, e.span, left.span),
48 BinOpKind::BitAnd => {
49 check(cx, left, -1, e.span, right.span);
50 check(cx, right, -1, e.span, left.span);
58 #[allow(clippy::cast_possible_wrap)]
59 fn check(cx: &LateContext<'_, '_>, e: &Expr<'_>, m: i8, span: Span, arg: Span) {
60 if let Some(Constant::Int(v)) = constant_simple(cx, cx.tables, e) {
61 let check = match cx.tables.expr_ty(e).kind {
62 ty::Int(ity) => unsext(cx.tcx, -1_i128, ity),
63 ty::Uint(uty) => clip(cx.tcx, !0, uty),
77 "the operation is ineffective. Consider reducing it to `{}`",
78 snippet(cx, arg, "..")