]> git.proxmox.com Git - rustc.git/blobdiff - src/tools/clippy/clippy_lints/src/identity_op.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / identity_op.rs
index a7c254e1e46405fe96f7c2533f6fa9c67e9bb0a8..e1d84a0743984ed9654b1c70a5d67fb2edf86bf4 100644 (file)
@@ -1,9 +1,9 @@
 use consts::{constant_simple, Constant};
-use rustc::lint::*;
 use rustc::hir::*;
+use rustc::lint::*;
+use rustc_const_math::ConstInt;
 use syntax::codemap::Span;
-use utils::{span_lint, snippet, in_macro};
-use syntax::attr::IntType::{SignedInt, UnsignedInt};
+use utils::{in_macro, snippet, span_lint};
 
 /// **What it does:** Checks for identity operations, e.g. `x + 0`.
 ///
@@ -58,21 +58,31 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityOp {
     }
 }
 
+fn all_ones(v: &ConstInt) -> bool {
+    match *v {
+        ConstInt::I8(i) => i == !0,
+        ConstInt::I16(i) => i == !0,
+        ConstInt::I32(i) => i == !0,
+        ConstInt::I64(i) => i == !0,
+        ConstInt::I128(i) => i == !0,
+        ConstInt::U8(i) => i == !0,
+        ConstInt::U16(i) => i == !0,
+        ConstInt::U32(i) => i == !0,
+        ConstInt::U64(i) => i == !0,
+        ConstInt::U128(i) => i == !0,
+        _ => false,
+    }
+}
+
 #[allow(cast_possible_wrap)]
 fn check(cx: &LateContext, e: &Expr, m: i8, span: Span, arg: Span) {
     if let Some(Constant::Int(v)) = constant_simple(cx, e) {
         if match m {
             0 => v.to_u128_unchecked() == 0,
-            -1 => {
-                match v.int_type() {
-                    SignedInt(_) => (v.to_u128_unchecked() as i128 == -1),
-                    UnsignedInt(_) => false,
-                }
-            },
+            -1 => all_ones(&v),
             1 => v.to_u128_unchecked() == 1,
             _ => unreachable!(),
-        }
-        {
+        } {
             span_lint(
                 cx,
                 IDENTITY_OP,