]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/bit_mask.rs
Auto merge of #3946 - rchaser53:issue-3920, r=flip1995
[rust.git] / clippy_lints / src / bit_mask.rs
index b15ce871c3260e3851900f9e9e73c1e7eee1891c..76314a610b16ad2c77dabb7e0c022bbdc5ff9fc4 100644 (file)
-// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
 use crate::consts::{constant, Constant};
-use crate::rustc::hir::*;
-use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
-use crate::rustc::{declare_tool_lint, lint_array};
-use crate::rustc_errors::Applicability;
-use crate::syntax::ast::LitKind;
-use crate::syntax::source_map::Span;
 use crate::utils::sugg::Sugg;
 use crate::utils::{span_lint, span_lint_and_then};
 use if_chain::if_chain;
+use rustc::hir::*;
+use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
+use rustc::{declare_tool_lint, lint_array};
+use rustc_errors::Applicability;
+use syntax::ast::LitKind;
+use syntax::source_map::Span;
 
-/// **What it does:** Checks for incompatible bit masks in comparisons.
-///
-/// The formula for detecting if an expression of the type `_ <bit_op> m
-/// <cmp_op> c` (where `<bit_op>` is one of {`&`, `|`} and `<cmp_op>` is one of
-/// {`!=`, `>=`, `>`, `!=`, `>=`, `>`}) can be determined from the following
-/// table:
-///
-/// |Comparison  |Bit Op|Example     |is always|Formula               |
-/// |------------|------|------------|---------|----------------------|
-/// |`==` or `!=`| `&`  |`x & 2 == 3`|`false`  |`c & m != c`          |
-/// |`<`  or `>=`| `&`  |`x & 2 < 3` |`true`   |`m < c`               |
-/// |`>`  or `<=`| `&`  |`x & 1 > 1` |`false`  |`m <= c`              |
-/// |`==` or `!=`| `|`  |`x | 1 == 0`|`false`  |`c | m != c`          |
-/// |`<`  or `>=`| `|`  |`x | 1 < 1` |`false`  |`m >= c`              |
-/// |`<=` or `>` | `|`  |`x | 1 > 0` |`true`   |`m > c`               |
-///
-/// **Why is this bad?** If the bits that the comparison cares about are always
-/// set to zero or one by the bit mask, the comparison is constant `true` or
-/// `false` (depending on mask, compared value, and operators).
-///
-/// So the code is actively misleading, and the only reason someone would write
-/// this intentionally is to win an underhanded Rust contest or create a
-/// test-case for this lint.
-///
-/// **Known problems:** None.
-///
-/// **Example:**
-/// ```rust
-/// if (x & 1 == 2) { … }
-/// ```
 declare_clippy_lint! {
+    /// **What it does:** Checks for incompatible bit masks in comparisons.
+    ///
+    /// The formula for detecting if an expression of the type `_ <bit_op> m
+    /// <cmp_op> c` (where `<bit_op>` is one of {`&`, `|`} and `<cmp_op>` is one of
+    /// {`!=`, `>=`, `>`, `!=`, `>=`, `>`}) can be determined from the following
+    /// table:
+    ///
+    /// |Comparison  |Bit Op|Example     |is always|Formula               |
+    /// |------------|------|------------|---------|----------------------|
+    /// |`==` or `!=`| `&`  |`x & 2 == 3`|`false`  |`c & m != c`          |
+    /// |`<`  or `>=`| `&`  |`x & 2 < 3` |`true`   |`m < c`               |
+    /// |`>`  or `<=`| `&`  |`x & 1 > 1` |`false`  |`m <= c`              |
+    /// |`==` or `!=`| `|`  |`x | 1 == 0`|`false`  |`c | m != c`          |
+    /// |`<`  or `>=`| `|`  |`x | 1 < 1` |`false`  |`m >= c`              |
+    /// |`<=` or `>` | `|`  |`x | 1 > 0` |`true`   |`m > c`               |
+    ///
+    /// **Why is this bad?** If the bits that the comparison cares about are always
+    /// set to zero or one by the bit mask, the comparison is constant `true` or
+    /// `false` (depending on mask, compared value, and operators).
+    ///
+    /// So the code is actively misleading, and the only reason someone would write
+    /// this intentionally is to win an underhanded Rust contest or create a
+    /// test-case for this lint.
+    ///
+    /// **Known problems:** None.
+    ///
+    /// **Example:**
+    /// ```rust
+    /// # let x = 1;
+    /// if (x & 1 == 2) { }
+    /// ```
     pub BAD_BIT_MASK,
     correctness,
     "expressions of the form `_ & mask == select` that will only ever return `true` or `false`"
 }
 
-/// **What it does:** Checks for bit masks in comparisons which can be removed
-/// without changing the outcome. The basic structure can be seen in the
-/// following table:
-///
-/// |Comparison| Bit Op  |Example    |equals |
-/// |----------|---------|-----------|-------|
-/// |`>` / `<=`|`|` / `^`|`x | 2 > 3`|`x > 3`|
-/// |`<` / `>=`|`|` / `^`|`x ^ 1 < 4`|`x < 4`|
-///
-/// **Why is this bad?** Not equally evil as [`bad_bit_mask`](#bad_bit_mask),
-/// but still a bit misleading, because the bit mask is ineffective.
-///
-/// **Known problems:** False negatives: This lint will only match instances
-/// where we have figured out the math (which is for a power-of-two compared
-/// value). This means things like `x | 1 >= 7` (which would be better written
-/// as `x >= 6`) will not be reported (but bit masks like this are fairly
-/// uncommon).
-///
-/// **Example:**
-/// ```rust
-/// if (x | 1 > 3) { … }
-/// ```
 declare_clippy_lint! {
+    /// **What it does:** Checks for bit masks in comparisons which can be removed
+    /// without changing the outcome. The basic structure can be seen in the
+    /// following table:
+    ///
+    /// |Comparison| Bit Op  |Example    |equals |
+    /// |----------|---------|-----------|-------|
+    /// |`>` / `<=`|`|` / `^`|`x | 2 > 3`|`x > 3`|
+    /// |`<` / `>=`|`|` / `^`|`x ^ 1 < 4`|`x < 4`|
+    ///
+    /// **Why is this bad?** Not equally evil as [`bad_bit_mask`](#bad_bit_mask),
+    /// but still a bit misleading, because the bit mask is ineffective.
+    ///
+    /// **Known problems:** False negatives: This lint will only match instances
+    /// where we have figured out the math (which is for a power-of-two compared
+    /// value). This means things like `x | 1 >= 7` (which would be better written
+    /// as `x >= 6`) will not be reported (but bit masks like this are fairly
+    /// uncommon).
+    ///
+    /// **Example:**
+    /// ```rust
+    /// # let x = 1;
+    /// if (x | 1 > 3) {  }
+    /// ```
     pub INEFFECTIVE_BIT_MASK,
     correctness,
-    "expressions where a bit mask will be rendered useless by a comparison, e.g. `(x | 1) > 2`"
+    "expressions where a bit mask will be rendered useless by a comparison, e.g., `(x | 1) > 2`"
 }
 
-/// **What it does:** Checks for bit masks that can be replaced by a call
-/// to `trailing_zeros`
-///
-/// **Why is this bad?** `x.trailing_zeros() > 4` is much clearer than `x & 15
-/// == 0`
-///
-/// **Known problems:** llvm generates better code for `x & 15 == 0` on x86
-///
-/// **Example:**
-/// ```rust
-/// x & 0x1111 == 0
-/// ```
 declare_clippy_lint! {
+    /// **What it does:** Checks for bit masks that can be replaced by a call
+    /// to `trailing_zeros`
+    ///
+    /// **Why is this bad?** `x.trailing_zeros() > 4` is much clearer than `x & 15
+    /// == 0`
+    ///
+    /// **Known problems:** llvm generates better code for `x & 15 == 0` on x86
+    ///
+    /// **Example:**
+    /// ```rust
+    /// # let x = 1;
+    /// if x & 0x1111 == 0 { }
+    /// ```
     pub VERBOSE_BIT_MASK,
     style,
     "expressions where a bit mask is less readable than the corresponding method call"
@@ -117,11 +111,14 @@ impl LintPass for BitMask {
     fn get_lints(&self) -> LintArray {
         lint_array!(BAD_BIT_MASK, INEFFECTIVE_BIT_MASK, VERBOSE_BIT_MASK)
     }
+    fn name(&self) -> &'static str {
+        "BitMask"
+    }
 }
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BitMask {
     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
-        if let ExprKind::Binary(ref cmp, ref left, ref right) = e.node {
+        if let ExprKind::Binary(cmp, left, 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)
@@ -131,13 +128,13 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
             }
         }
         if_chain! {
-            if let ExprKind::Binary(ref op, ref left, ref right) = e.node;
+            if let ExprKind::Binary(op, left, right) = &e.node;
             if BinOpKind::Eq == op.node;
-            if let ExprKind::Binary(ref op1, ref left1, ref right1) = left.node;
+            if let ExprKind::Binary(op1, left1, right1) = &left.node;
             if BinOpKind::BitAnd == op1.node;
-            if let ExprKind::Lit(ref lit) = right1.node;
+            if let ExprKind::Lit(lit) = &right1.node;
             if let LitKind::Int(n, _) = lit.node;
-            if let ExprKind::Lit(ref lit1) = right.node;
+            if let ExprKind::Lit(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);
@@ -148,7 +145,7 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
                                    "bit mask could be simplified with a call to `trailing_zeros`",
                                    |db| {
                     let sugg = Sugg::hir(cx, left1, "...").maybe_par();
-                    db.span_suggestion_with_applicability(
+                    db.span_suggestion(
                         e.span,
                         "try",
                         format!("{}.trailing_zeros() >= {}", sugg, n.count_ones()),
@@ -173,7 +170,7 @@ fn invert_cmp(cmp: BinOpKind) -> BinOpKind {
 }
 
 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 let ExprKind::Binary(op, left, right) = &bit_op.node {
         if op.node != BinOpKind::BitAnd && op.node != BinOpKind::BitOr {
             return;
         }
@@ -183,6 +180,7 @@ fn check_compare(cx: &LateContext<'_, '_>, bit_op: &Expr, cmp_op: BinOpKind, cmp
     }
 }
 
+#[allow(clippy::too_many_lines)]
 fn check_bit_mask(
     cx: &LateContext<'_, '_>,
     bit_op: BinOpKind,