Skip to content

Commit bc76f39

Browse files
authored
Merge pull request #2084 from ykrivopalov/bit_mask_verbosity_threshold
Configuration option for VERBOSE_BIT_MASK threshold
2 parents 13caa00 + 5c56c92 commit bc76f39

File tree

6 files changed

+18
-18
lines changed

6 files changed

+18
-18
lines changed

clippy_lints/src/bit_mask.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,17 @@ declare_lint! {
9090
}
9191

9292
#[derive(Copy, Clone)]
93-
pub struct BitMask;
93+
pub struct BitMask {
94+
verbose_bit_mask_threshold: u64,
95+
}
96+
97+
impl BitMask {
98+
pub fn new(verbose_bit_mask_threshold: u64) -> Self {
99+
Self {
100+
verbose_bit_mask_threshold: verbose_bit_mask_threshold,
101+
}
102+
}
103+
}
94104

95105
impl LintPass for BitMask {
96106
fn get_lints(&self) -> LintArray {
@@ -119,6 +129,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BitMask {
119129
let Expr_::ExprLit(ref lit1) = right.node,
120130
let LitKind::Int(0, _) = lit1.node,
121131
n.leading_zeros() == n.count_zeros(),
132+
n > u128::from(self.verbose_bit_mask_threshold),
122133
], {
123134
span_lint_and_then(cx,
124135
VERBOSE_BIT_MASK,

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
232232
reg.register_early_lint_pass(box enum_variants::EnumVariantNames::new(conf.enum_variant_name_threshold));
233233
reg.register_late_lint_pass(box enum_glob_use::EnumGlobUse);
234234
reg.register_late_lint_pass(box enum_clike::UnportableVariant);
235-
reg.register_late_lint_pass(box bit_mask::BitMask);
235+
reg.register_late_lint_pass(box bit_mask::BitMask::new(conf.verbose_bit_mask_threshold));
236236
reg.register_late_lint_pass(box ptr::PointerPass);
237237
reg.register_late_lint_pass(box needless_bool::NeedlessBool);
238238
reg.register_late_lint_pass(box needless_bool::BoolComparison);

clippy_lints/src/utils/conf.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ define_Conf! {
175175
(enum_variant_name_threshold, "enum_variant_name_threshold", 3 => u64),
176176
/// Lint: LARGE_ENUM_VARIANT. The maximum size of a emum's variant to avoid box suggestion
177177
(enum_variant_size_threshold, "enum_variant_size_threshold", 200 => u64),
178+
/// Lint: VERBOSE_BIT_MASK. The maximum allowed size of a bit mask before suggesting to use 'trailing_zeros'
179+
(verbose_bit_mask_threshold, "verbose_bit_mask_threshold", 1 => u64),
178180
}
179181

180182
/// Search for the configuration file.

tests/ui/bit_masks.stderr

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,6 @@ error: &-masking with zero
66
|
77
= note: `-D bad-bit-mask` implied by `-D warnings`
88

9-
error: bit mask could be simplified with a call to `trailing_zeros`
10-
--> $DIR/bit_masks.rs:12:5
11-
|
12-
12 | x & 0 == 0;
13-
| ^^^^^^^^^^ help: try: `x.trailing_zeros() >= 0`
14-
|
15-
= note: `-D verbose-bit-mask` implied by `-D warnings`
16-
17-
error: bit mask could be simplified with a call to `trailing_zeros`
18-
--> $DIR/bit_masks.rs:14:5
19-
|
20-
14 | x & 1 == 0; //ok, compared with zero
21-
| ^^^^^^^^^^ help: try: `x.trailing_zeros() >= 1`
22-
239
error: incompatible bit mask: `_ & 2` can never be equal to `1`
2410
--> $DIR/bit_masks.rs:15:5
2511
|
@@ -106,5 +92,5 @@ error: ineffective bit mask: `x | 1` compared to `8`, is the same as x compared
10692
55 | x | 1 >= 8;
10793
| ^^^^^^^^^^
10894

109-
error: aborting due to 17 previous errors
95+
error: aborting due to 15 previous errors
11096

tests/ui/conf_unknown_key.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
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`
1+
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`
22

33
error: aborting due to previous error
44

tests/ui/trailing_zeros.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ fn main() {
77
let _ = #[clippy(author)] (x & 0b1111 == 0); // suggest trailing_zeros
88
let _ = x & 0b1_1111 == 0; // suggest trailing_zeros
99
let _ = x & 0b1_1010 == 0; // do not lint
10+
let _ = x & 1 == 0; // do not lint
1011
}

0 commit comments

Comments
 (0)