]> git.lizzy.rs Git - rust.git/commitdiff
Configuration option for VERBOSE_BIT_MASK threshold
authorYury Krivopalov <ykrivopalov@yandex.ru>
Mon, 25 Sep 2017 20:38:49 +0000 (23:38 +0300)
committerYury Krivopalov <ykrivopalov@yandex.ru>
Mon, 25 Sep 2017 20:38:49 +0000 (23:38 +0300)
By default is 1. u64, because I didn't figure out how to deserialize
u128 option from config.

clippy_lints/src/bit_mask.rs
clippy_lints/src/lib.rs
clippy_lints/src/utils/conf.rs
tests/ui/bit_masks.stderr
tests/ui/conf_unknown_key.stderr
tests/ui/trailing_zeros.rs

index 9b64a42d4f7fb6c0f71e4882babd06d874d362cd..6372221fd4405de1451b04a353aac7875dacc56d 100644 (file)
 }
 
 #[derive(Copy, Clone)]
-pub struct BitMask;
+pub struct BitMask {
+    verbose_bit_mask_threshold: u64,
+}
+
+impl BitMask {
+    pub fn new(verbose_bit_mask_threshold: u64) -> Self {
+        Self {
+            verbose_bit_mask_threshold: verbose_bit_mask_threshold,
+        }
+    }
+}
 
 impl LintPass for BitMask {
     fn get_lints(&self) -> LintArray {
@@ -119,6 +129,7 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
             let Expr_::ExprLit(ref lit1) = right.node,
             let LitKind::Int(0, _) = lit1.node,
             n.leading_zeros() == n.count_zeros(),
+            n > u128::from(self.verbose_bit_mask_threshold),
         ], {
             span_lint_and_then(cx,
                                VERBOSE_BIT_MASK,
index 5cb7034c7bfcff78fa4e8755b0473273358d70ce..955bb883635b4c3adebc3fe92f7d9d970526677f 100644 (file)
@@ -231,7 +231,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
     reg.register_early_lint_pass(box enum_variants::EnumVariantNames::new(conf.enum_variant_name_threshold));
     reg.register_late_lint_pass(box enum_glob_use::EnumGlobUse);
     reg.register_late_lint_pass(box enum_clike::UnportableVariant);
-    reg.register_late_lint_pass(box bit_mask::BitMask);
+    reg.register_late_lint_pass(box bit_mask::BitMask::new(conf.verbose_bit_mask_threshold));
     reg.register_late_lint_pass(box ptr::PointerPass);
     reg.register_late_lint_pass(box needless_bool::NeedlessBool);
     reg.register_late_lint_pass(box needless_bool::BoolComparison);
index f88294763dc372f7d911515788aa3a1268e34a99..5272e8a6ca646e7b62f092882a4939b718058033 100644 (file)
@@ -172,6 +172,8 @@ fn $rust_name() -> define_Conf!(TY $($ty)+) {
     (enum_variant_name_threshold, "enum_variant_name_threshold", 3 => u64),
     /// Lint: LARGE_ENUM_VARIANT. The maximum size of a emum's variant to avoid box suggestion
     (enum_variant_size_threshold, "enum_variant_size_threshold", 200 => u64),
+    /// Lint: VERBOSE_BIT_MASK. The maximum size of a bit mask, that won't be checked on verbosity
+    (verbose_bit_mask_threshold, "verbose_bit_mask_threshold", 1 => u64),
 }
 
 /// Search for the configuration file.
index 4b40fa086b84d52c5706da7f7e542a634f50dcee..40aa585d12430a4f4e24c84e958684dc49febd07 100644 (file)
@@ -6,20 +6,6 @@ error: &-masking with zero
    |
    = note: `-D bad-bit-mask` implied by `-D warnings`
 
-error: bit mask could be simplified with a call to `trailing_zeros`
-  --> $DIR/bit_masks.rs:12:5
-   |
-12 |     x & 0 == 0;
-   |     ^^^^^^^^^^ help: try: `x.trailing_zeros() >= 0`
-   |
-   = note: `-D verbose-bit-mask` implied by `-D warnings`
-
-error: bit mask could be simplified with a call to `trailing_zeros`
-  --> $DIR/bit_masks.rs:14:5
-   |
-14 |     x & 1 == 0; //ok, compared with zero
-   |     ^^^^^^^^^^ help: try: `x.trailing_zeros() >= 1`
-
 error: incompatible bit mask: `_ & 2` can never be equal to `1`
   --> $DIR/bit_masks.rs:15:5
    |
@@ -106,5 +92,5 @@ error: ineffective bit mask: `x | 1` compared to `8`, is the same as x compared
 55 |     x | 1 >= 8;
    |     ^^^^^^^^^^
 
-error: aborting due to 17 previous errors
+error: aborting due to 15 previous errors
 
index bd16dfd47da9e195f10a5bc48260b055e60f03ed..8de3cd93370e249f0195f8d724574b421be68de6 100644 (file)
@@ -1,4 +1,4 @@
-error: error reading Clippy's configuration file: unknown field `foobar`, expected one of `blacklisted-names`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `third-party`
+error: error reading Clippy's configuration file: unknown field `foobar`, expected one of `blacklisted-names`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `third-party`
 
 error: aborting due to previous error
 
index 40f588cecef2e82a479e62e5f800286aa6e1e2f1..9fc71506ffe422caabd35657174a606463c7bd74 100644 (file)
@@ -7,4 +7,5 @@ fn main() {
     let _ = #[clippy(author)] (x & 0b1111 == 0);  // suggest trailing_zeros
     let _ = x & 0b1_1111 == 0; // suggest trailing_zeros
     let _ = x & 0b1_1010 == 0; // do not lint
+    let _ = x & 1 == 0; // do not lint
 }