|
1 | 1 | use consts::{constant_simple, Constant};
|
2 |
| -use rustc::lint::*; |
3 | 2 | use rustc::hir::*;
|
| 3 | +use rustc::lint::*; |
| 4 | +use rustc_const_math::ConstInt; |
4 | 5 | use syntax::codemap::Span;
|
5 | 6 | use utils::{in_macro, snippet, span_lint};
|
6 |
| -use syntax::attr::IntType::{SignedInt, UnsignedInt}; |
7 | 7 |
|
8 | 8 | /// **What it does:** Checks for identity operations, e.g. `x + 0`.
|
9 | 9 | ///
|
@@ -58,15 +58,28 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityOp {
|
58 | 58 | }
|
59 | 59 | }
|
60 | 60 |
|
| 61 | +fn all_ones(v: &ConstInt) -> bool { |
| 62 | + match *v { |
| 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, |
| 73 | + _ => false |
| 74 | + } |
| 75 | +} |
| 76 | + |
61 | 77 | #[allow(cast_possible_wrap)]
|
62 | 78 | fn check(cx: &LateContext, e: &Expr, m: i8, span: Span, arg: Span) {
|
63 | 79 | if let Some(Constant::Int(v)) = constant_simple(cx, e) {
|
64 | 80 | if match m {
|
65 | 81 | 0 => v.to_u128_unchecked() == 0,
|
66 |
| - -1 => match v.int_type() { |
67 |
| - SignedInt(_) => (v.to_u128_unchecked() as i128 == -1), |
68 |
| - UnsignedInt(_) => false, |
69 |
| - }, |
| 82 | + -1 => all_ones(&v), |
70 | 83 | 1 => v.to_u128_unchecked() == 1,
|
71 | 84 | _ => unreachable!(),
|
72 | 85 | } {
|
|
0 commit comments