]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/bit_mask.rs
Merge branch 'macro-use' into HEAD
[rust.git] / clippy_lints / src / bit_mask.rs
index f0ff27d4d63788428a2573982c84f76655eeb13c..2e4e60c412a3de205a79d8badfbc727fb2ebfac7 100644 (file)
@@ -1,11 +1,12 @@
 use rustc::hir::*;
-use rustc::hir::def::Def;
 use rustc::lint::*;
-use rustc_const_eval::lookup_const_by_id;
+use rustc::{declare_lint, lint_array};
+use if_chain::if_chain;
 use syntax::ast::LitKind;
 use syntax::codemap::Span;
-use utils::{span_lint, span_lint_and_then};
-use utils::sugg::Sugg;
+use crate::utils::{span_lint, span_lint_and_then};
+use crate::utils::sugg::Sugg;
+use crate::consts::{constant, Constant};
 
 /// **What it does:** Checks for incompatible bit masks in comparisons.
 ///
@@ -37,9 +38,9 @@
 /// ```rust
 /// if (x & 1 == 2) { … }
 /// ```
-declare_lint! {
+declare_clippy_lint! {
     pub BAD_BIT_MASK,
-    Warn,
+    correctness,
     "expressions of the form `_ & mask == select` that will only ever return `true` or `false`"
 }
 
@@ -65,9 +66,9 @@
 /// ```rust
 /// if (x | 1 > 3) { … }
 /// ```
-declare_lint! {
+declare_clippy_lint! {
     pub INEFFECTIVE_BIT_MASK,
-    Warn,
+    correctness,
     "expressions where a bit mask will be rendered useless by a comparison, e.g. `(x | 1) > 2`"
 }
 
@@ -83,9 +84,9 @@
 /// ```rust
 /// x & 0x1111 == 0
 /// ```
-declare_lint! {
+declare_clippy_lint! {
     pub VERBOSE_BIT_MASK,
-    Warn,
+    style,
     "expressions where a bit mask is less readable than the corresponding method call"
 }
 
@@ -97,7 +98,7 @@ pub struct BitMask {
 impl BitMask {
     pub fn new(verbose_bit_mask_threshold: u64) -> Self {
         Self {
-            verbose_bit_mask_threshold: verbose_bit_mask_threshold,
+            verbose_bit_mask_threshold,
         }
     }
 }
@@ -110,23 +111,23 @@ fn get_lints(&self) -> LintArray {
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BitMask {
     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
-        if let ExprBinary(ref cmp, ref left, ref right) = e.node {
+        if let ExprKind::Binary(ref cmp, ref left, ref right) = e.node {
             if cmp.node.is_comparison() {
                 if let Some(cmp_opt) = fetch_int_literal(cx, right) {
-                    check_compare(cx, left, cmp.node, cmp_opt, &e.span)
+                    check_compare(cx, left, cmp.node, cmp_opt, e.span)
                 } else if let Some(cmp_val) = fetch_int_literal(cx, left) {
-                    check_compare(cx, right, invert_cmp(cmp.node), cmp_val, &e.span)
+                    check_compare(cx, right, invert_cmp(cmp.node), cmp_val, e.span)
                 }
             }
         }
         if_chain! {
-            if let Expr_::ExprBinary(ref op, ref left, ref right) = e.node;
-            if BinOp_::BiEq == op.node;
-            if let Expr_::ExprBinary(ref op1, ref left1, ref right1) = left.node;
-            if BinOp_::BiBitAnd == op1.node;
-            if let Expr_::ExprLit(ref lit) = right1.node;
+            if let ExprKind::Binary(ref op, ref left, ref right) = e.node;
+            if BinOpKind::Eq == op.node;
+            if let ExprKind::Binary(ref op1, ref left1, ref right1) = left.node;
+            if BinOpKind::BitAnd == op1.node;
+            if let ExprKind::Lit(ref lit) = right1.node;
             if let LitKind::Int(n, _) = lit.node;
-            if let Expr_::ExprLit(ref lit1) = right.node;
+            if let ExprKind::Lit(ref lit1) = right.node;
             if let LitKind::Int(0, _) = lit1.node;
             if n.leading_zeros() == n.count_zeros();
             if n > u128::from(self.verbose_bit_mask_threshold);
@@ -144,22 +145,22 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
     }
 }
 
-fn invert_cmp(cmp: BinOp_) -> BinOp_ {
+fn invert_cmp(cmp: BinOpKind) -> BinOpKind {
     match cmp {
-        BiEq => BiEq,
-        BiNe => BiNe,
-        BiLt => BiGt,
-        BiGt => BiLt,
-        BiLe => BiGe,
-        BiGe => BiLe,
-        _ => BiOr, // Dummy
+        BinOpKind::Eq => BinOpKind::Eq,
+        BinOpKind::Ne => BinOpKind::Ne,
+        BinOpKind::Lt => BinOpKind::Gt,
+        BinOpKind::Gt => BinOpKind::Lt,
+        BinOpKind::Le => BinOpKind::Ge,
+        BinOpKind::Ge => BinOpKind::Le,
+        _ => BinOpKind::Or, // Dummy
     }
 }
 
 
-fn check_compare(cx: &LateContext, bit_op: &Expr, cmp_op: BinOp_, cmp_value: u128, span: &Span) {
-    if let ExprBinary(ref op, ref left, ref right) = bit_op.node {
-        if op.node != BiBitAnd && op.node != BiBitOr {
+fn check_compare(cx: &LateContext, bit_op: &Expr, cmp_op: BinOpKind, cmp_value: u128, span: Span) {
+    if let ExprKind::Binary(ref op, ref left, ref right) = bit_op.node {
+        if op.node != BinOpKind::BitAnd && op.node != BinOpKind::BitOr {
             return;
         }
         fetch_int_literal(cx, right)
@@ -168,15 +169,15 @@ fn check_compare(cx: &LateContext, bit_op: &Expr, cmp_op: BinOp_, cmp_value: u12
     }
 }
 
-fn check_bit_mask(cx: &LateContext, bit_op: BinOp_, cmp_op: BinOp_, mask_value: u128, cmp_value: u128, span: &Span) {
+fn check_bit_mask(cx: &LateContext, bit_op: BinOpKind, cmp_op: BinOpKind, mask_value: u128, cmp_value: u128, span: Span) {
     match cmp_op {
-        BiEq | BiNe => match bit_op {
-            BiBitAnd => if mask_value & cmp_value != cmp_value {
+        BinOpKind::Eq | BinOpKind::Ne => match bit_op {
+            BinOpKind::BitAnd => if mask_value & cmp_value != cmp_value {
                 if cmp_value != 0 {
                     span_lint(
                         cx,
                         BAD_BIT_MASK,
-                        *span,
+                        span,
                         &format!(
                             "incompatible bit mask: `_ & {}` can never be equal to `{}`",
                             mask_value,
@@ -185,13 +186,13 @@ fn check_bit_mask(cx: &LateContext, bit_op: BinOp_, cmp_op: BinOp_, mask_value:
                     );
                 }
             } else if mask_value == 0 {
-                span_lint(cx, BAD_BIT_MASK, *span, "&-masking with zero");
+                span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero");
             },
-            BiBitOr => if mask_value | cmp_value != cmp_value {
+            BinOpKind::BitOr => if mask_value | cmp_value != cmp_value {
                 span_lint(
                     cx,
                     BAD_BIT_MASK,
-                    *span,
+                    span,
                     &format!(
                         "incompatible bit mask: `_ | {}` can never be equal to `{}`",
                         mask_value,
@@ -201,12 +202,12 @@ fn check_bit_mask(cx: &LateContext, bit_op: BinOp_, cmp_op: BinOp_, mask_value:
             },
             _ => (),
         },
-        BiLt | BiGe => match bit_op {
-            BiBitAnd => if mask_value < cmp_value {
+        BinOpKind::Lt | BinOpKind::Ge => match bit_op {
+            BinOpKind::BitAnd => if mask_value < cmp_value {
                 span_lint(
                     cx,
                     BAD_BIT_MASK,
-                    *span,
+                    span,
                     &format!(
                         "incompatible bit mask: `_ & {}` will always be lower than `{}`",
                         mask_value,
@@ -214,13 +215,13 @@ fn check_bit_mask(cx: &LateContext, bit_op: BinOp_, cmp_op: BinOp_, mask_value:
                     ),
                 );
             } else if mask_value == 0 {
-                span_lint(cx, BAD_BIT_MASK, *span, "&-masking with zero");
+                span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero");
             },
-            BiBitOr => if mask_value >= cmp_value {
+            BinOpKind::BitOr => if mask_value >= cmp_value {
                 span_lint(
                     cx,
                     BAD_BIT_MASK,
-                    *span,
+                    span,
                     &format!(
                         "incompatible bit mask: `_ | {}` will never be lower than `{}`",
                         mask_value,
@@ -228,17 +229,17 @@ fn check_bit_mask(cx: &LateContext, bit_op: BinOp_, cmp_op: BinOp_, mask_value:
                     ),
                 );
             } else {
-                check_ineffective_lt(cx, *span, mask_value, cmp_value, "|");
+                check_ineffective_lt(cx, span, mask_value, cmp_value, "|");
             },
-            BiBitXor => check_ineffective_lt(cx, *span, mask_value, cmp_value, "^"),
+            BinOpKind::BitXor => check_ineffective_lt(cx, span, mask_value, cmp_value, "^"),
             _ => (),
         },
-        BiLe | BiGt => match bit_op {
-            BiBitAnd => if mask_value <= cmp_value {
+        BinOpKind::Le | BinOpKind::Gt => match bit_op {
+            BinOpKind::BitAnd => if mask_value <= cmp_value {
                 span_lint(
                     cx,
                     BAD_BIT_MASK,
-                    *span,
+                    span,
                     &format!(
                         "incompatible bit mask: `_ & {}` will never be higher than `{}`",
                         mask_value,
@@ -246,13 +247,13 @@ fn check_bit_mask(cx: &LateContext, bit_op: BinOp_, cmp_op: BinOp_, mask_value:
                     ),
                 );
             } else if mask_value == 0 {
-                span_lint(cx, BAD_BIT_MASK, *span, "&-masking with zero");
+                span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero");
             },
-            BiBitOr => if mask_value > cmp_value {
+            BinOpKind::BitOr => if mask_value > cmp_value {
                 span_lint(
                     cx,
                     BAD_BIT_MASK,
-                    *span,
+                    span,
                     &format!(
                         "incompatible bit mask: `_ | {}` will always be higher than `{}`",
                         mask_value,
@@ -260,9 +261,9 @@ fn check_bit_mask(cx: &LateContext, bit_op: BinOp_, cmp_op: BinOp_, mask_value:
                     ),
                 );
             } else {
-                check_ineffective_gt(cx, *span, mask_value, cmp_value, "|");
+                check_ineffective_gt(cx, span, mask_value, cmp_value, "|");
             },
-            BiBitXor => check_ineffective_gt(cx, *span, mask_value, cmp_value, "^"),
+            BinOpKind::BitXor => check_ineffective_gt(cx, span, mask_value, cmp_value, "^"),
             _ => (),
         },
         _ => (),
@@ -302,31 +303,8 @@ fn check_ineffective_gt(cx: &LateContext, span: Span, m: u128, c: u128, op: &str
 }
 
 fn fetch_int_literal(cx: &LateContext, lit: &Expr) -> Option<u128> {
-    use rustc::ty::subst::Substs;
-    match lit.node {
-        ExprLit(ref lit_ptr) => {
-            if let LitKind::Int(value, _) = lit_ptr.node {
-                Some(value) // TODO: Handle sign
-            } else {
-                None
-            }
-        },
-        ExprPath(ref qpath) => {
-            let def = cx.tables.qpath_def(qpath, lit.hir_id);
-            if let Def::Const(def_id) = def {
-                lookup_const_by_id(cx.tcx, cx.param_env.and((def_id, Substs::empty()))).and_then(|(l, _ty)| {
-                    let body = if let Some(id) = cx.tcx.hir.as_local_node_id(l) {
-                        cx.tcx.mir_const_qualif(def_id);
-                        cx.tcx.hir.body(cx.tcx.hir.body_owned_by(id))
-                    } else {
-                        cx.tcx.extern_const_body(def_id).body
-                    };
-                    fetch_int_literal(cx, &body.value)
-                })
-            } else {
-                None
-            }
-        },
+    match constant(cx, cx.tables, lit)?.0 {
+        Constant::Int(n) => Some(n),
         _ => None,
     }
 }