fixed bad_bit_mask false positive

This commit is contained in:
Andre Bogus 2015-09-06 16:09:35 +02:00
parent dc32092ee4
commit b76ad366ab
3 changed files with 6 additions and 3 deletions

View File

@ -9,7 +9,7 @@ There are 55 lints included in this crate:
name | default | meaning
-----------------------------------------------------------------------------------------------------|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[approx_constant](https://github.com/Manishearth/rust-clippy/wiki#approx_constant) | warn | the approximate of a known float constant (in `std::f64::consts` or `std::f32::consts`) is found; suggests to use the constant
[bad_bit_mask](https://github.com/Manishearth/rust-clippy/wiki#bad_bit_mask) | deny | expressions of the form `_ & mask == select` that will only ever return `true` or `false` (because in the example `select` containing bits that `mask` doesn't have)
[bad_bit_mask](https://github.com/Manishearth/rust-clippy/wiki#bad_bit_mask) | warn | expressions of the form `_ & mask == select` that will only ever return `true` or `false` (because in the example `select` containing bits that `mask` doesn't have)
[box_vec](https://github.com/Manishearth/rust-clippy/wiki#box_vec) | warn | usage of `Box<Vec<T>>`, vector elements are already on the heap
[cast_possible_truncation](https://github.com/Manishearth/rust-clippy/wiki#cast_possible_truncation) | allow | casts that may cause truncation of the value, e.g `x as u8` where `x: u32`, or `x as i32` where `x: f32`
[cast_possible_wrap](https://github.com/Manishearth/rust-clippy/wiki#cast_possible_wrap) | allow | casts that may cause wrapping around the value, e.g `x as i32` where `x: u32` and `x > i32::MAX`

View File

@ -9,7 +9,7 @@ use utils::span_lint;
declare_lint! {
pub BAD_BIT_MASK,
Deny,
Warn,
"expressions of the form `_ & mask == select` that will only ever return `true` or `false` \
(because in the example `select` containing bits that `mask` doesn't have)"
}
@ -98,7 +98,7 @@ fn check_bit_mask(cx: &Context, bit_op: BinOp_, cmp_op: BinOp_,
mask_value: u64, cmp_value: u64, span: &Span) {
match cmp_op {
BiEq | BiNe => match bit_op {
BiBitAnd => if mask_value & cmp_value != mask_value {
BiBitAnd => if mask_value & cmp_value != cmp_value {
if cmp_value != 0 {
span_lint(cx, BAD_BIT_MASK, *span, &format!(
"incompatible bit mask: `_ & {}` can never be equal to `{}`",

View File

@ -25,6 +25,9 @@ fn main() {
x | 2 > 1; //~ERROR incompatible bit mask
x | 2 <= 2; // ok (if a bit silly), equals x <= 2
x & 192 == 128; // ok, tests for bit 7 and not bit 6
x & 0xffc0 == 0xfe80; // ok
// this also now works with constants
x & THREE_BITS == 8; //~ERROR incompatible bit mask
x | EVEN_MORE_REDIRECTION < 7; //~ERROR incompatible bit mask