2018-12-09 22:26:16 +00:00
|
|
|
const THREE_BITS: i64 = 7;
|
|
|
|
const EVEN_MORE_REDIRECTION: i64 = THREE_BITS;
|
2015-04-30 13:17:06 +00:00
|
|
|
|
2018-07-28 15:34:52 +00:00
|
|
|
#[warn(clippy::bad_bit_mask)]
|
2018-12-09 22:26:16 +00:00
|
|
|
#[allow(
|
|
|
|
clippy::ineffective_bit_mask,
|
|
|
|
clippy::identity_op,
|
|
|
|
clippy::no_effect,
|
|
|
|
clippy::unnecessary_operation
|
|
|
|
)]
|
2015-04-30 09:48:43 +00:00
|
|
|
fn main() {
|
2015-08-11 18:22:20 +00:00
|
|
|
let x = 5;
|
|
|
|
|
2017-02-08 13:58:07 +00:00
|
|
|
x & 0 == 0;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: &-masking with zero
|
|
|
|
//~| NOTE: `-D clippy::bad-bit-mask` implied by `-D warnings`
|
|
|
|
//~| ERROR: this operation will always return zero. This is likely not the intended ou
|
|
|
|
//~| NOTE: `#[deny(clippy::erasing_op)]` on by default
|
2015-08-11 18:22:20 +00:00
|
|
|
x & 1 == 1; //ok, distinguishes bit 0
|
|
|
|
x & 1 == 0; //ok, compared with zero
|
2017-02-08 13:58:07 +00:00
|
|
|
x & 2 == 1;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: incompatible bit mask: `_ & 2` can never be equal to `1`
|
2015-08-11 18:22:20 +00:00
|
|
|
x | 0 == 0; //ok, equals x == 0 (maybe warn?)
|
|
|
|
x | 1 == 3; //ok, equals x == 2 || x == 3
|
|
|
|
x | 3 == 3; //ok, equals x <= 3
|
2017-02-08 13:58:07 +00:00
|
|
|
x | 3 == 2;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: incompatible bit mask: `_ | 3` can never be equal to `2`
|
2015-08-11 18:22:20 +00:00
|
|
|
|
2017-02-08 13:58:07 +00:00
|
|
|
x & 1 > 1;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: incompatible bit mask: `_ & 1` will never be higher than `1`
|
2015-08-11 18:22:20 +00:00
|
|
|
x & 2 > 1; // ok, distinguishes x & 2 == 2 from x & 2 == 0
|
|
|
|
x & 2 < 1; // ok, distinguishes x & 2 == 2 from x & 2 == 0
|
|
|
|
x | 1 > 1; // ok (if a bit silly), equals x > 1
|
2017-02-08 13:58:07 +00:00
|
|
|
x | 2 > 1;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: incompatible bit mask: `_ | 2` will always be higher than `1`
|
2015-08-11 18:22:20 +00:00
|
|
|
x | 2 <= 2; // ok (if a bit silly), equals x <= 2
|
|
|
|
|
2015-09-06 14:09:35 +00:00
|
|
|
x & 192 == 128; // ok, tests for bit 7 and not bit 6
|
|
|
|
x & 0xffc0 == 0xfe80; // ok
|
2015-09-28 05:11:03 +00:00
|
|
|
|
2015-08-11 18:22:20 +00:00
|
|
|
// this also now works with constants
|
2017-02-08 13:58:07 +00:00
|
|
|
x & THREE_BITS == 8;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: incompatible bit mask: `_ & 7` can never be equal to `8`
|
2017-02-08 13:58:07 +00:00
|
|
|
x | EVEN_MORE_REDIRECTION < 7;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: incompatible bit mask: `_ | 7` will never be lower than `7`
|
2015-08-11 18:22:20 +00:00
|
|
|
|
2017-02-08 13:58:07 +00:00
|
|
|
0 & x == 0;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: &-masking with zero
|
|
|
|
//~| ERROR: this operation will always return zero. This is likely not the intended ou
|
2015-08-11 18:22:20 +00:00
|
|
|
1 | x > 1;
|
|
|
|
|
|
|
|
// and should now also match uncommon usage
|
2017-02-08 13:58:07 +00:00
|
|
|
1 < 2 | x;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: incompatible bit mask: `_ | 2` will always be higher than `1`
|
2017-02-08 13:58:07 +00:00
|
|
|
2 == 3 | x;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: incompatible bit mask: `_ | 3` can never be equal to `2`
|
2017-02-08 13:58:07 +00:00
|
|
|
1 == x & 2;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: incompatible bit mask: `_ & 2` can never be equal to `1`
|
2015-08-11 18:22:20 +00:00
|
|
|
|
|
|
|
x | 1 > 2; // no error, because we allowed ineffective bit masks
|
|
|
|
ineffective();
|
2015-05-15 12:09:29 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 15:34:52 +00:00
|
|
|
#[warn(clippy::ineffective_bit_mask)]
|
|
|
|
#[allow(clippy::bad_bit_mask, clippy::no_effect, clippy::unnecessary_operation)]
|
2015-05-15 12:09:29 +00:00
|
|
|
fn ineffective() {
|
2015-08-11 18:22:20 +00:00
|
|
|
let x = 5;
|
|
|
|
|
2017-02-08 13:58:07 +00:00
|
|
|
x | 1 > 3;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: ineffective bit mask: `x | 1` compared to `3`, is the same as x compared d
|
|
|
|
//~| NOTE: `-D clippy::ineffective-bit-mask` implied by `-D warnings`
|
2017-02-08 13:58:07 +00:00
|
|
|
x | 1 < 4;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: ineffective bit mask: `x | 1` compared to `4`, is the same as x compared d
|
2017-02-08 13:58:07 +00:00
|
|
|
x | 1 <= 3;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: ineffective bit mask: `x | 1` compared to `3`, is the same as x compared d
|
2017-02-08 13:58:07 +00:00
|
|
|
x | 1 >= 8;
|
2023-08-24 19:32:12 +00:00
|
|
|
//~^ ERROR: ineffective bit mask: `x | 1` compared to `8`, is the same as x compared d
|
2015-08-19 07:07:50 +00:00
|
|
|
|
|
|
|
x | 1 > 2; // not an error (yet), better written as x >= 2
|
|
|
|
x | 1 >= 7; // not an error (yet), better written as x >= 6
|
|
|
|
x | 3 > 4; // not an error (yet), better written as x >= 4
|
|
|
|
x | 4 <= 19;
|
2015-04-30 09:48:43 +00:00
|
|
|
}
|