From dea03e4844c01818c437084f8e4f217606aaba56 Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Thu, 28 Aug 2025 07:54:08 -0400 Subject: [PATCH 1/2] Split and extend tests for `bad_bit_mask` and `ineffective_bit_mask`. --- tests/ui/bad_bit_masks.rs | 100 ++++++++++ tests/ui/bad_bit_masks.stderr | 233 +++++++++++++++++++++++ tests/ui/bit_masks.rs | 91 --------- tests/ui/bit_masks.stderr | 112 ----------- tests/ui/ineffective_bit_masks.rs | 194 +++++++++++++++++++ tests/ui/ineffective_bit_masks.stderr | 256 ++++++++++++++++++++++++++ 6 files changed, 783 insertions(+), 203 deletions(-) create mode 100644 tests/ui/bad_bit_masks.rs create mode 100644 tests/ui/bad_bit_masks.stderr delete mode 100644 tests/ui/bit_masks.rs delete mode 100644 tests/ui/bit_masks.stderr create mode 100644 tests/ui/ineffective_bit_masks.rs create mode 100644 tests/ui/ineffective_bit_masks.stderr diff --git a/tests/ui/bad_bit_masks.rs b/tests/ui/bad_bit_masks.rs new file mode 100644 index 000000000000..b12973118630 --- /dev/null +++ b/tests/ui/bad_bit_masks.rs @@ -0,0 +1,100 @@ +//@aux-build:proc_macros.rs + +#![warn(clippy::bad_bit_mask)] +#![allow(clippy::erasing_op, clippy::ineffective_bit_mask, clippy::identity_op)] + +use core::hint::black_box; +use proc_macros::{external, with_span}; + +fn main() { + let x = black_box(5u32); + + let _ = x & 0b0000 == 0b0000; //~ bad_bit_mask + let _ = x & 0b0000 == 0b0001; //~ bad_bit_mask + let _ = x & 0b0001 == 0b0000; + let _ = x & 0b0001 == 0b0001; + let _ = x & 0b0010 == 0b0001; //~ bad_bit_mask + let _ = x & 0b0001 == 0b0010; //~ bad_bit_mask + let _ = x & 0b0011 == 0b0001; + let _ = x & 0b0011 == 0b0010; + let _ = x & 0b0011 == 0b0011; + let _ = x & 0b0011 == 0b0100; //~ bad_bit_mask + let _ = x & 0b0100 == 0b0100; + let _ = x & 0b0110 == 0b0111; //~ bad_bit_mask + let _ = x & 0b0011 == 0b0101; //~ bad_bit_mask + let _ = x & 0b1111 == 0b1110; + let _ = x & 0b1111 == 0b1010; + + let _ = x | 0b0000 == 0b0000; + let _ = x | 0b0000 == 0b0001; + let _ = x | 0b0001 == 0b0000; //~ bad_bit_mask + let _ = x | 0b0001 == 0b0001; + let _ = x | 0b0010 == 0b0001; //~ bad_bit_mask + let _ = x | 0b0001 == 0b0010; //~ bad_bit_mask + let _ = x | 0b0011 == 0b0001; //~ bad_bit_mask + let _ = x | 0b0011 == 0b0010; //~ bad_bit_mask + let _ = x | 0b0011 == 0b0011; + let _ = x | 0b0011 == 0b0100; //~ bad_bit_mask + let _ = x | 0b0100 == 0b0100; + let _ = x | 0b0110 == 0b0111; + let _ = x | 0b0011 == 0b0101; //~ bad_bit_mask + let _ = x | 0b1111 == 0b1110; //~ bad_bit_mask + let _ = x | 0b1111 == 0b1010; //~ bad_bit_mask + + let _ = x & 0b0000 != 0b0000; //~ bad_bit_mask + let _ = x & 0b0000 != 0b0001; //~ bad_bit_mask + let _ = x & 0b0001 != 0b0000; + let _ = x & 0b0001 != 0b0001; + let _ = x & 0b0010 != 0b0001; //~ bad_bit_mask + let _ = x & 0b0001 != 0b0010; //~ bad_bit_mask + let _ = x & 0b0011 != 0b0001; + let _ = x & 0b0011 != 0b0010; + let _ = x & 0b0011 != 0b0011; + let _ = x & 0b0011 != 0b0100; //~ bad_bit_mask + let _ = x & 0b0100 != 0b0100; + let _ = x & 0b0110 != 0b0111; //~ bad_bit_mask + let _ = x & 0b0011 != 0b0101; //~ bad_bit_mask + let _ = x & 0b1111 != 0b1110; + let _ = x & 0b1111 != 0b1010; + + let _ = x | 0b0000 != 0b0000; + let _ = x | 0b0000 != 0b0001; + let _ = x | 0b0001 != 0b0000; //~ bad_bit_mask + let _ = x | 0b0001 != 0b0001; + let _ = x | 0b0010 != 0b0001; //~ bad_bit_mask + let _ = x | 0b0001 != 0b0010; //~ bad_bit_mask + let _ = x | 0b0011 != 0b0001; //~ bad_bit_mask + let _ = x | 0b0011 != 0b0010; //~ bad_bit_mask + let _ = x | 0b0011 != 0b0011; + let _ = x | 0b0011 != 0b0100; //~ bad_bit_mask + let _ = x | 0b0100 != 0b0100; + let _ = x | 0b0110 != 0b0111; + let _ = x | 0b0011 != 0b0101; //~ bad_bit_mask + let _ = x | 0b1111 != 0b1110; //~ bad_bit_mask + let _ = x | 0b1111 != 0b1010; //~ bad_bit_mask + + let _ = 0b0010 & x == 0b0001; //~ bad_bit_mask + let _ = 0b0001 == x & 0b0010; //~ bad_bit_mask + let _ = 0b0001 == 0b0010 & x; //~ bad_bit_mask + + let _ = x & (0b0100 | 0b0010) == (0b0111 ^ 0b1000); //~ bad_bit_mask + + external! { + let x = black_box(5u32); + let _ = x & 0b0010 == 0b0001; + } + with_span! { + sp + let x = black_box(5u32); + let _ = x & 0b0010 == 0b0001; + } + + { + const C: i32 = 0b0011; + + let x = black_box(5i32); + let _ = x & C == 0b0011; + let _ = x & C == 0b0100; //~ bad_bit_mask + let _ = x & 0b0001 == C; //~ bad_bit_mask + } +} diff --git a/tests/ui/bad_bit_masks.stderr b/tests/ui/bad_bit_masks.stderr new file mode 100644 index 000000000000..b37676b8665a --- /dev/null +++ b/tests/ui/bad_bit_masks.stderr @@ -0,0 +1,233 @@ +error: &-masking with zero + --> tests/ui/bad_bit_masks.rs:12:13 + | +LL | let _ = x & 0b0000 == 0b0000; + | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `-D clippy::bad-bit-mask` implied by `-D warnings` + = help: to override `-D warnings` add `#[allow(clippy::bad_bit_mask)]` + +error: incompatible bit mask: `_ & 0` can never be equal to `1` + --> tests/ui/bad_bit_masks.rs:13:13 + | +LL | let _ = x & 0b0000 == 0b0001; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ & 2` can never be equal to `1` + --> tests/ui/bad_bit_masks.rs:16:13 + | +LL | let _ = x & 0b0010 == 0b0001; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ & 1` can never be equal to `2` + --> tests/ui/bad_bit_masks.rs:17:13 + | +LL | let _ = x & 0b0001 == 0b0010; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ & 3` can never be equal to `4` + --> tests/ui/bad_bit_masks.rs:21:13 + | +LL | let _ = x & 0b0011 == 0b0100; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ & 6` can never be equal to `7` + --> tests/ui/bad_bit_masks.rs:23:13 + | +LL | let _ = x & 0b0110 == 0b0111; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ & 3` can never be equal to `5` + --> tests/ui/bad_bit_masks.rs:24:13 + | +LL | let _ = x & 0b0011 == 0b0101; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ | 1` can never be equal to `0` + --> tests/ui/bad_bit_masks.rs:30:13 + | +LL | let _ = x | 0b0001 == 0b0000; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ | 2` can never be equal to `1` + --> tests/ui/bad_bit_masks.rs:32:13 + | +LL | let _ = x | 0b0010 == 0b0001; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ | 1` can never be equal to `2` + --> tests/ui/bad_bit_masks.rs:33:13 + | +LL | let _ = x | 0b0001 == 0b0010; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ | 3` can never be equal to `1` + --> tests/ui/bad_bit_masks.rs:34:13 + | +LL | let _ = x | 0b0011 == 0b0001; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ | 3` can never be equal to `2` + --> tests/ui/bad_bit_masks.rs:35:13 + | +LL | let _ = x | 0b0011 == 0b0010; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ | 3` can never be equal to `4` + --> tests/ui/bad_bit_masks.rs:37:13 + | +LL | let _ = x | 0b0011 == 0b0100; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ | 3` can never be equal to `5` + --> tests/ui/bad_bit_masks.rs:40:13 + | +LL | let _ = x | 0b0011 == 0b0101; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ | 15` can never be equal to `14` + --> tests/ui/bad_bit_masks.rs:41:13 + | +LL | let _ = x | 0b1111 == 0b1110; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ | 15` can never be equal to `10` + --> tests/ui/bad_bit_masks.rs:42:13 + | +LL | let _ = x | 0b1111 == 0b1010; + | ^^^^^^^^^^^^^^^^^^^^ + +error: &-masking with zero + --> tests/ui/bad_bit_masks.rs:44:13 + | +LL | let _ = x & 0b0000 != 0b0000; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ & 0` can never be equal to `1` + --> tests/ui/bad_bit_masks.rs:45:13 + | +LL | let _ = x & 0b0000 != 0b0001; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ & 2` can never be equal to `1` + --> tests/ui/bad_bit_masks.rs:48:13 + | +LL | let _ = x & 0b0010 != 0b0001; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ & 1` can never be equal to `2` + --> tests/ui/bad_bit_masks.rs:49:13 + | +LL | let _ = x & 0b0001 != 0b0010; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ & 3` can never be equal to `4` + --> tests/ui/bad_bit_masks.rs:53:13 + | +LL | let _ = x & 0b0011 != 0b0100; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ & 6` can never be equal to `7` + --> tests/ui/bad_bit_masks.rs:55:13 + | +LL | let _ = x & 0b0110 != 0b0111; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ & 3` can never be equal to `5` + --> tests/ui/bad_bit_masks.rs:56:13 + | +LL | let _ = x & 0b0011 != 0b0101; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ | 1` can never be equal to `0` + --> tests/ui/bad_bit_masks.rs:62:13 + | +LL | let _ = x | 0b0001 != 0b0000; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ | 2` can never be equal to `1` + --> tests/ui/bad_bit_masks.rs:64:13 + | +LL | let _ = x | 0b0010 != 0b0001; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ | 1` can never be equal to `2` + --> tests/ui/bad_bit_masks.rs:65:13 + | +LL | let _ = x | 0b0001 != 0b0010; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ | 3` can never be equal to `1` + --> tests/ui/bad_bit_masks.rs:66:13 + | +LL | let _ = x | 0b0011 != 0b0001; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ | 3` can never be equal to `2` + --> tests/ui/bad_bit_masks.rs:67:13 + | +LL | let _ = x | 0b0011 != 0b0010; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ | 3` can never be equal to `4` + --> tests/ui/bad_bit_masks.rs:69:13 + | +LL | let _ = x | 0b0011 != 0b0100; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ | 3` can never be equal to `5` + --> tests/ui/bad_bit_masks.rs:72:13 + | +LL | let _ = x | 0b0011 != 0b0101; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ | 15` can never be equal to `14` + --> tests/ui/bad_bit_masks.rs:73:13 + | +LL | let _ = x | 0b1111 != 0b1110; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ | 15` can never be equal to `10` + --> tests/ui/bad_bit_masks.rs:74:13 + | +LL | let _ = x | 0b1111 != 0b1010; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ & 2` can never be equal to `1` + --> tests/ui/bad_bit_masks.rs:76:13 + | +LL | let _ = 0b0010 & x == 0b0001; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ & 2` can never be equal to `1` + --> tests/ui/bad_bit_masks.rs:77:13 + | +LL | let _ = 0b0001 == x & 0b0010; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ & 2` can never be equal to `1` + --> tests/ui/bad_bit_masks.rs:78:13 + | +LL | let _ = 0b0001 == 0b0010 & x; + | ^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ & 6` can never be equal to `15` + --> tests/ui/bad_bit_masks.rs:80:13 + | +LL | let _ = x & (0b0100 | 0b0010) == (0b0111 ^ 0b1000); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ & 3` can never be equal to `4` + --> tests/ui/bad_bit_masks.rs:97:17 + | +LL | let _ = x & C == 0b0100; + | ^^^^^^^^^^^^^^^ + +error: incompatible bit mask: `_ & 1` can never be equal to `3` + --> tests/ui/bad_bit_masks.rs:98:17 + | +LL | let _ = x & 0b0001 == C; + | ^^^^^^^^^^^^^^^ + +error: aborting due to 38 previous errors + diff --git a/tests/ui/bit_masks.rs b/tests/ui/bit_masks.rs deleted file mode 100644 index 87dcdb3084d0..000000000000 --- a/tests/ui/bit_masks.rs +++ /dev/null @@ -1,91 +0,0 @@ -const THREE_BITS: i64 = 7; -const EVEN_MORE_REDIRECTION: i64 = THREE_BITS; - -#[warn(clippy::bad_bit_mask)] -#[allow( - clippy::ineffective_bit_mask, - clippy::identity_op, - clippy::no_effect, - clippy::unnecessary_operation -)] -fn main() { - let x = 5; - - x & 0 == 0; - //~^ bad_bit_mask - //~| erasing_op - - x & 1 == 1; //ok, distinguishes bit 0 - x & 1 == 0; //ok, compared with zero - x & 2 == 1; - //~^ bad_bit_mask - - 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 - x | 3 == 2; - //~^ bad_bit_mask - - x & 1 > 1; - //~^ bad_bit_mask - - 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 - x | 2 > 1; - //~^ bad_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; - //~^ bad_bit_mask - - x | EVEN_MORE_REDIRECTION < 7; - //~^ bad_bit_mask - - 0 & x == 0; - //~^ bad_bit_mask - //~| erasing_op - - 1 | x > 1; - - // and should now also match uncommon usage - 1 < 2 | x; - //~^ bad_bit_mask - - 2 == 3 | x; - //~^ bad_bit_mask - - 1 == x & 2; - //~^ bad_bit_mask - - x | 1 > 2; // no error, because we allowed ineffective bit masks - ineffective(); -} - -#[warn(clippy::ineffective_bit_mask)] -#[allow(clippy::bad_bit_mask, clippy::no_effect, clippy::unnecessary_operation)] -fn ineffective() { - let x = 5; - - x | 1 > 3; - //~^ ineffective_bit_mask - - x | 1 < 4; - //~^ ineffective_bit_mask - - x | 1 <= 3; - //~^ ineffective_bit_mask - - x | 1 >= 8; - //~^ ineffective_bit_mask - - 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; -} diff --git a/tests/ui/bit_masks.stderr b/tests/ui/bit_masks.stderr deleted file mode 100644 index 666ad671edee..000000000000 --- a/tests/ui/bit_masks.stderr +++ /dev/null @@ -1,112 +0,0 @@ -error: &-masking with zero - --> tests/ui/bit_masks.rs:14:5 - | -LL | x & 0 == 0; - | ^^^^^^^^^^ - | - = note: `-D clippy::bad-bit-mask` implied by `-D warnings` - = help: to override `-D warnings` add `#[allow(clippy::bad_bit_mask)]` - -error: this operation will always return zero. This is likely not the intended outcome - --> tests/ui/bit_masks.rs:14:5 - | -LL | x & 0 == 0; - | ^^^^^ - | - = note: `#[deny(clippy::erasing_op)]` on by default - -error: incompatible bit mask: `_ & 2` can never be equal to `1` - --> tests/ui/bit_masks.rs:20:5 - | -LL | x & 2 == 1; - | ^^^^^^^^^^ - -error: incompatible bit mask: `_ | 3` can never be equal to `2` - --> tests/ui/bit_masks.rs:26:5 - | -LL | x | 3 == 2; - | ^^^^^^^^^^ - -error: incompatible bit mask: `_ & 1` will never be higher than `1` - --> tests/ui/bit_masks.rs:29:5 - | -LL | x & 1 > 1; - | ^^^^^^^^^ - -error: incompatible bit mask: `_ | 2` will always be higher than `1` - --> tests/ui/bit_masks.rs:35:5 - | -LL | x | 2 > 1; - | ^^^^^^^^^ - -error: incompatible bit mask: `_ & 7` can never be equal to `8` - --> tests/ui/bit_masks.rs:44:5 - | -LL | x & THREE_BITS == 8; - | ^^^^^^^^^^^^^^^^^^^ - -error: incompatible bit mask: `_ | 7` will never be lower than `7` - --> tests/ui/bit_masks.rs:47:5 - | -LL | x | EVEN_MORE_REDIRECTION < 7; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -error: &-masking with zero - --> tests/ui/bit_masks.rs:50:5 - | -LL | 0 & x == 0; - | ^^^^^^^^^^ - -error: this operation will always return zero. This is likely not the intended outcome - --> tests/ui/bit_masks.rs:50:5 - | -LL | 0 & x == 0; - | ^^^^^ - -error: incompatible bit mask: `_ | 2` will always be higher than `1` - --> tests/ui/bit_masks.rs:57:5 - | -LL | 1 < 2 | x; - | ^^^^^^^^^ - -error: incompatible bit mask: `_ | 3` can never be equal to `2` - --> tests/ui/bit_masks.rs:60:5 - | -LL | 2 == 3 | x; - | ^^^^^^^^^^ - -error: incompatible bit mask: `_ & 2` can never be equal to `1` - --> tests/ui/bit_masks.rs:63:5 - | -LL | 1 == x & 2; - | ^^^^^^^^^^ - -error: ineffective bit mask: `x | 1` compared to `3`, is the same as x compared directly - --> tests/ui/bit_masks.rs:75:5 - | -LL | x | 1 > 3; - | ^^^^^^^^^ - | - = note: `-D clippy::ineffective-bit-mask` implied by `-D warnings` - = help: to override `-D warnings` add `#[allow(clippy::ineffective_bit_mask)]` - -error: ineffective bit mask: `x | 1` compared to `4`, is the same as x compared directly - --> tests/ui/bit_masks.rs:78:5 - | -LL | x | 1 < 4; - | ^^^^^^^^^ - -error: ineffective bit mask: `x | 1` compared to `3`, is the same as x compared directly - --> tests/ui/bit_masks.rs:81:5 - | -LL | x | 1 <= 3; - | ^^^^^^^^^^ - -error: ineffective bit mask: `x | 1` compared to `8`, is the same as x compared directly - --> tests/ui/bit_masks.rs:84:5 - | -LL | x | 1 >= 8; - | ^^^^^^^^^^ - -error: aborting due to 17 previous errors - diff --git a/tests/ui/ineffective_bit_masks.rs b/tests/ui/ineffective_bit_masks.rs new file mode 100644 index 000000000000..aa61c43a429a --- /dev/null +++ b/tests/ui/ineffective_bit_masks.rs @@ -0,0 +1,194 @@ +//@aux-build:proc_macros.rs + +#![warn(clippy::bad_bit_mask)] +#![allow( + unused_comparisons, + clippy::absurd_extreme_comparisons, + clippy::bad_bit_mask, + clippy::identity_op +)] + +use core::hint::black_box; +use proc_macros::{external, with_span}; + +fn main() { + let x = black_box(5u32); + + let _ = x | 0b00_0000 > 0b00_0000; //~ ineffective_bit_mask + let _ = x | 0b00_0001 > 0b00_0000; + let _ = x | 0b00_0001 > 0b00_0001; //~ ineffective_bit_mask + let _ = x | 0b00_0010 > 0b00_0001; + let _ = x | 0b00_0011 > 0b00_0001; + let _ = x | 0b00_0000 > 0b00_0011; //~ ineffective_bit_mask + let _ = x | 0b00_0001 > 0b00_0011; //~ ineffective_bit_mask + let _ = x | 0b00_0010 > 0b00_0011; //~ ineffective_bit_mask + let _ = x | 0b00_0011 > 0b00_0011; //~ ineffective_bit_mask + let _ = x | 0b00_0100 > 0b00_0011; + let _ = x | 0b00_0101 > 0b00_0011; + let _ = x | 0b00_0001 > 0b00_0111; //~ ineffective_bit_mask + let _ = x | 0b00_0010 > 0b00_0111; //~ ineffective_bit_mask + let _ = x | 0b00_0011 > 0b00_0111; //~ ineffective_bit_mask + let _ = x | 0b00_0100 > 0b00_0111; //~ ineffective_bit_mask + let _ = x | 0b00_1000 > 0b00_0111; + let _ = x | 0b00_1010 > 0b00_0111; + let _ = x | 0b00_0001 > 0b00_1111; //~ ineffective_bit_mask + let _ = x | 0b00_0010 > 0b00_1111; //~ ineffective_bit_mask + let _ = x | 0b00_0011 > 0b00_1111; //~ ineffective_bit_mask + let _ = x | 0b00_0100 > 0b00_1111; //~ ineffective_bit_mask + let _ = x | 0b00_0101 > 0b00_1111; //~ ineffective_bit_mask + let _ = x | 0b00_0110 > 0b00_1111; //~ ineffective_bit_mask + let _ = x | 0b00_0111 > 0b00_1111; //~ ineffective_bit_mask + let _ = x | 0b00_1000 > 0b00_1111; //~ ineffective_bit_mask + let _ = x | 0b00_1001 > 0b00_1111; //~ ineffective_bit_mask + let _ = x | 0b00_1111 > 0b10_1111; + let _ = x | 0b01_0000 > 0b10_1111; + let _ = x | 0b01_1111 > 0b10_1111; + let _ = x | 0b10_0000 > 0b10_1111; + let _ = x | 0b11_1111 > 0b10_1111; + let _ = x | 0b00_1111 <= 0b10_1111; + let _ = x | 0b11_0000 <= 0b10_1111; + + let _ = x ^ 0b00_0000 > 0b00_0000; + let _ = x ^ 0b00_0001 > 0b00_0000; + let _ = x ^ 0b00_0001 > 0b00_0001; + let _ = x ^ 0b00_0010 > 0b00_0001; + let _ = x ^ 0b00_0011 > 0b00_0001; + let _ = x ^ 0b00_0000 > 0b00_0011; + let _ = x ^ 0b00_0001 > 0b00_0011; + let _ = x ^ 0b00_0010 > 0b00_0011; + let _ = x ^ 0b00_0011 > 0b00_0011; + let _ = x ^ 0b00_0100 > 0b00_0011; + let _ = x ^ 0b00_0101 > 0b00_0011; + let _ = x ^ 0b00_0001 > 0b00_0111; + let _ = x ^ 0b00_0010 > 0b00_0111; + let _ = x ^ 0b00_0011 > 0b00_0111; + let _ = x ^ 0b00_0100 > 0b00_0111; + let _ = x ^ 0b00_1000 > 0b00_0111; + let _ = x ^ 0b00_1010 > 0b00_0111; + let _ = x ^ 0b00_0001 > 0b00_1111; + let _ = x ^ 0b00_0010 > 0b00_1111; + let _ = x ^ 0b00_0011 > 0b00_1111; + let _ = x ^ 0b00_0100 > 0b00_1111; + let _ = x ^ 0b00_0101 > 0b00_1111; + let _ = x ^ 0b00_0110 > 0b00_1111; + let _ = x ^ 0b00_0111 > 0b00_1111; + let _ = x ^ 0b00_1000 > 0b00_1111; + let _ = x ^ 0b00_1001 > 0b00_1111; + let _ = x ^ 0b00_1111 > 0b10_1111; + let _ = x ^ 0b01_0000 > 0b10_1111; + let _ = x ^ 0b01_1111 > 0b10_1111; + let _ = x ^ 0b10_0000 > 0b10_1111; + let _ = x ^ 0b11_1111 > 0b10_1111; + let _ = x ^ 0b00_1111 <= 0b10_1111; + let _ = x ^ 0b11_0000 <= 0b10_1111; + + let _ = x | 0b00_0000 < 0b00_0000; + let _ = x | 0b00_0001 < 0b00_0000; + let _ = x | 0b00_0001 < 0b00_0001; + let _ = x | 0b00_0010 < 0b00_0001; + let _ = x | 0b00_0011 < 0b00_0001; + let _ = x | 0b00_0000 < 0b00_0010; //~ ineffective_bit_mask + let _ = x | 0b00_0001 < 0b00_0010; //~ ineffective_bit_mask + let _ = x | 0b00_0010 < 0b00_0010; + let _ = x | 0b00_0011 < 0b00_0010; + let _ = x | 0b00_0000 < 0b00_0100; //~ ineffective_bit_mask + let _ = x | 0b00_0001 < 0b00_0100; //~ ineffective_bit_mask + let _ = x | 0b00_0010 < 0b00_0100; //~ ineffective_bit_mask + let _ = x | 0b00_0011 < 0b00_0100; //~ ineffective_bit_mask + let _ = x | 0b00_0100 < 0b00_0100; + let _ = x | 0b00_0101 < 0b00_0100; + let _ = x | 0b00_0001 < 0b00_1000; //~ ineffective_bit_mask + let _ = x | 0b00_0010 < 0b00_1000; //~ ineffective_bit_mask + let _ = x | 0b00_0011 < 0b00_1000; //~ ineffective_bit_mask + let _ = x | 0b00_0100 < 0b00_1000; //~ ineffective_bit_mask + let _ = x | 0b00_1000 < 0b00_1000; + let _ = x | 0b00_1010 < 0b00_1000; + let _ = x | 0b00_0001 < 0b01_0000; //~ ineffective_bit_mask + let _ = x | 0b00_0010 < 0b01_0000; //~ ineffective_bit_mask + let _ = x | 0b00_0011 < 0b01_0000; //~ ineffective_bit_mask + let _ = x | 0b00_0100 < 0b01_0000; //~ ineffective_bit_mask + let _ = x | 0b00_0101 < 0b01_0000; //~ ineffective_bit_mask + let _ = x | 0b00_0110 < 0b01_0000; //~ ineffective_bit_mask + let _ = x | 0b00_0111 < 0b01_0000; //~ ineffective_bit_mask + let _ = x | 0b00_1000 < 0b01_0000; //~ ineffective_bit_mask + let _ = x | 0b00_1001 < 0b01_0000; //~ ineffective_bit_mask + let _ = x | 0b00_1111 < 0b11_0000; + let _ = x | 0b01_0000 < 0b11_0000; + let _ = x | 0b01_1111 < 0b11_0000; + let _ = x | 0b10_0000 < 0b11_0000; + let _ = x | 0b11_1111 < 0b11_0000; + let _ = x | 0b00_1111 >= 0b11_0000; + let _ = x | 0b11_0000 >= 0b11_0000; + + let _ = x ^ 0b00_0000 < 0b00_0000; + let _ = x ^ 0b00_0001 < 0b00_0000; + let _ = x ^ 0b00_0001 < 0b00_0001; + let _ = x ^ 0b00_0010 < 0b00_0001; + let _ = x ^ 0b00_0011 < 0b00_0001; + let _ = x ^ 0b00_0000 < 0b00_0010; + let _ = x ^ 0b00_0001 < 0b00_0010; + let _ = x ^ 0b00_0010 < 0b00_0010; + let _ = x ^ 0b00_0011 < 0b00_0010; + let _ = x ^ 0b00_0000 < 0b00_0100; + let _ = x ^ 0b00_0001 < 0b00_0100; + let _ = x ^ 0b00_0010 < 0b00_0100; + let _ = x ^ 0b00_0011 < 0b00_0100; + let _ = x ^ 0b00_0100 < 0b00_0100; + let _ = x ^ 0b00_0101 < 0b00_0100; + let _ = x ^ 0b00_0001 < 0b00_1000; + let _ = x ^ 0b00_0010 < 0b00_1000; + let _ = x ^ 0b00_0011 < 0b00_1000; + let _ = x ^ 0b00_0100 < 0b00_1000; + let _ = x ^ 0b00_1000 < 0b00_1000; + let _ = x ^ 0b00_1010 < 0b00_1000; + let _ = x ^ 0b00_0001 < 0b01_0000; + let _ = x ^ 0b00_0010 < 0b01_0000; + let _ = x ^ 0b00_0011 < 0b01_0000; + let _ = x ^ 0b00_0100 < 0b01_0000; + let _ = x ^ 0b00_0101 < 0b01_0000; + let _ = x ^ 0b00_0110 < 0b01_0000; + let _ = x ^ 0b00_0111 < 0b01_0000; + let _ = x ^ 0b00_1000 < 0b01_0000; + let _ = x ^ 0b00_1001 < 0b01_0000; + let _ = x ^ 0b00_1111 < 0b11_0000; + let _ = x ^ 0b01_0000 < 0b11_0000; + let _ = x ^ 0b01_1111 < 0b11_0000; + let _ = x ^ 0b10_0000 < 0b11_0000; + let _ = x ^ 0b11_1111 < 0b11_0000; + let _ = x ^ 0b00_1111 >= 0b11_0000; + let _ = x ^ 0b11_0000 >= 0b11_0000; + + let _ = x | 0x7fff_ffff > 0xffff_ffff; //~ ineffective_bit_mask + let _ = x | 0x8000_0000 > 0xffff_ffff; //~ ineffective_bit_mask + let _ = x | 0xffff_ffff > 0xffff_ffff; //~ ineffective_bit_mask + let _ = x ^ 0x7fff_ffff > 0xffff_ffff; + let _ = x ^ 0x8000_0000 > 0xffff_ffff; + let _ = x ^ 0xffff_ffff > 0xffff_ffff; + + let _ = x | 0x7fff_ffff < 0x8000_0000; //~ ineffective_bit_mask + let _ = x | 0x8000_0000 < 0x8000_0000; + let _ = x ^ 0x7fff_ffff < 0x8000_0000; + let _ = x ^ 0x8000_0000 < 0x8000_0000; + + let _ = x | 0b00_0001 > 0b00_0100; + let _ = x | 0b00_0010 > 0b00_0100; + let _ = x | 0b00_0011 > 0b00_0100; + let _ = x | 0b00_0100 > 0b00_0100; + let _ = x | 0b00_0101 > 0b00_0100; + + let _ = x | 0b00_0001 < 0b00_1011; + let _ = x | 0b00_0010 < 0b00_1011; + let _ = x | 0b00_0011 < 0b00_1011; + let _ = x | 0b00_0100 < 0b00_1011; + let _ = x | 0b00_0101 < 0b00_1011; + + external! { + let x = black_box(5u32); + let _ = x | 0b0001 > 0b00_0011; + } + with_span! { + sp + let x = black_box(5u32); + let _ = x | 0b0001 > 0b00_0011; + } +} diff --git a/tests/ui/ineffective_bit_masks.stderr b/tests/ui/ineffective_bit_masks.stderr new file mode 100644 index 000000000000..3af48bf7f639 --- /dev/null +++ b/tests/ui/ineffective_bit_masks.stderr @@ -0,0 +1,256 @@ +error: ineffective bit mask: `x | 0` compared to `0`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:17:13 + | +LL | let _ = x | 0b00_0000 > 0b00_0000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[deny(clippy::ineffective_bit_mask)]` on by default + +error: ineffective bit mask: `x | 1` compared to `1`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:19:13 + | +LL | let _ = x | 0b00_0001 > 0b00_0001; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 0` compared to `3`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:22:13 + | +LL | let _ = x | 0b00_0000 > 0b00_0011; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 1` compared to `3`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:23:13 + | +LL | let _ = x | 0b00_0001 > 0b00_0011; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 2` compared to `3`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:24:13 + | +LL | let _ = x | 0b00_0010 > 0b00_0011; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 3` compared to `3`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:25:13 + | +LL | let _ = x | 0b00_0011 > 0b00_0011; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 1` compared to `7`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:28:13 + | +LL | let _ = x | 0b00_0001 > 0b00_0111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 2` compared to `7`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:29:13 + | +LL | let _ = x | 0b00_0010 > 0b00_0111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 3` compared to `7`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:30:13 + | +LL | let _ = x | 0b00_0011 > 0b00_0111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 4` compared to `7`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:31:13 + | +LL | let _ = x | 0b00_0100 > 0b00_0111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 1` compared to `15`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:34:13 + | +LL | let _ = x | 0b00_0001 > 0b00_1111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 2` compared to `15`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:35:13 + | +LL | let _ = x | 0b00_0010 > 0b00_1111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 3` compared to `15`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:36:13 + | +LL | let _ = x | 0b00_0011 > 0b00_1111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 4` compared to `15`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:37:13 + | +LL | let _ = x | 0b00_0100 > 0b00_1111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 5` compared to `15`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:38:13 + | +LL | let _ = x | 0b00_0101 > 0b00_1111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 6` compared to `15`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:39:13 + | +LL | let _ = x | 0b00_0110 > 0b00_1111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 7` compared to `15`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:40:13 + | +LL | let _ = x | 0b00_0111 > 0b00_1111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 8` compared to `15`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:41:13 + | +LL | let _ = x | 0b00_1000 > 0b00_1111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 9` compared to `15`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:42:13 + | +LL | let _ = x | 0b00_1001 > 0b00_1111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 0` compared to `2`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:90:13 + | +LL | let _ = x | 0b00_0000 < 0b00_0010; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 1` compared to `2`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:91:13 + | +LL | let _ = x | 0b00_0001 < 0b00_0010; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 0` compared to `4`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:94:13 + | +LL | let _ = x | 0b00_0000 < 0b00_0100; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 1` compared to `4`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:95:13 + | +LL | let _ = x | 0b00_0001 < 0b00_0100; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 2` compared to `4`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:96:13 + | +LL | let _ = x | 0b00_0010 < 0b00_0100; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 3` compared to `4`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:97:13 + | +LL | let _ = x | 0b00_0011 < 0b00_0100; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 1` compared to `8`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:100:13 + | +LL | let _ = x | 0b00_0001 < 0b00_1000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 2` compared to `8`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:101:13 + | +LL | let _ = x | 0b00_0010 < 0b00_1000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 3` compared to `8`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:102:13 + | +LL | let _ = x | 0b00_0011 < 0b00_1000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 4` compared to `8`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:103:13 + | +LL | let _ = x | 0b00_0100 < 0b00_1000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 1` compared to `16`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:106:13 + | +LL | let _ = x | 0b00_0001 < 0b01_0000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 2` compared to `16`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:107:13 + | +LL | let _ = x | 0b00_0010 < 0b01_0000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 3` compared to `16`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:108:13 + | +LL | let _ = x | 0b00_0011 < 0b01_0000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 4` compared to `16`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:109:13 + | +LL | let _ = x | 0b00_0100 < 0b01_0000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 5` compared to `16`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:110:13 + | +LL | let _ = x | 0b00_0101 < 0b01_0000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 6` compared to `16`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:111:13 + | +LL | let _ = x | 0b00_0110 < 0b01_0000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 7` compared to `16`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:112:13 + | +LL | let _ = x | 0b00_0111 < 0b01_0000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 8` compared to `16`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:113:13 + | +LL | let _ = x | 0b00_1000 < 0b01_0000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 9` compared to `16`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:114:13 + | +LL | let _ = x | 0b00_1001 < 0b01_0000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 2147483647` compared to `4294967295`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:161:13 + | +LL | let _ = x | 0x7fff_ffff > 0xffff_ffff; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 2147483648` compared to `4294967295`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:162:13 + | +LL | let _ = x | 0x8000_0000 > 0xffff_ffff; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 4294967295` compared to `4294967295`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:163:13 + | +LL | let _ = x | 0xffff_ffff > 0xffff_ffff; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: ineffective bit mask: `x | 2147483647` compared to `2147483648`, is the same as x compared directly + --> tests/ui/ineffective_bit_masks.rs:168:13 + | +LL | let _ = x | 0x7fff_ffff < 0x8000_0000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 42 previous errors + From 97c83aa75aa9c120afae74fb0df0fef0e7188d12 Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Sun, 24 Aug 2025 10:38:06 -0400 Subject: [PATCH 2/2] Refactor `bit_mask` lints --- clippy_lints/src/operators/bit_mask.rs | 381 +++++++++++------ tests/ui/bad_bit_masks.stderr | 151 +++++-- tests/ui/ineffective_bit_masks.rs | 100 ++--- tests/ui/ineffective_bit_masks.stderr | 569 +++++++++++++++++++++++-- 4 files changed, 931 insertions(+), 270 deletions(-) diff --git a/clippy_lints/src/operators/bit_mask.rs b/clippy_lints/src/operators/bit_mask.rs index e87cfd103c30..b862f5ee9aa9 100644 --- a/clippy_lints/src/operators/bit_mask.rs +++ b/clippy_lints/src/operators/bit_mask.rs @@ -1,174 +1,277 @@ use clippy_utils::consts::{ConstEvalCtxt, Constant}; -use clippy_utils::diagnostics::span_lint; +use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::is_from_proc_macro; +use clippy_utils::source::walk_span_to_context; +use core::cmp::Ordering; +use core::convert::identity; use rustc_hir::{BinOpKind, Expr, ExprKind}; use rustc_lint::LateContext; -use rustc_span::Span; +use rustc_middle::ty; use super::{BAD_BIT_MASK, INEFFECTIVE_BIT_MASK}; pub(super) fn check<'tcx>( cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, - op: BinOpKind, - left: &'tcx Expr<'_>, - right: &'tcx Expr<'_>, + cmp_op: BinOpKind, + lhs: &'tcx Expr<'_>, + rhs: &'tcx Expr<'_>, ) { - if op.is_comparison() { - if let Some(cmp_opt) = fetch_int_literal(cx, right) { - check_compare(cx, left, op, cmp_opt, e.span); - } else if let Some(cmp_val) = fetch_int_literal(cx, left) { - check_compare(cx, right, invert_cmp(op), cmp_val, e.span); + let Some(cmp_op) = CmpOp::from_bin_op(cmp_op) else { + return; + }; + + // Check for a bitwise op compared to a constant. + let typeck = cx.typeck_results(); + let ecx = ConstEvalCtxt::new(cx); + let ctxt = e.span.ctxt(); + let Some((cmp_op, bit_op, bit_lhs, bit_rhs, cmp_val)) = try_maybe_swap( + lhs, + rhs, + |lhs, rhs| { + if let ExprKind::Binary(bit_op, bit_lhs, bit_rhs) = lhs.kind + && let bit_sp = bit_op.span + && let Some(bit_op) = BitOp::from_bin_op(bit_op.node) + && matches!(typeck.expr_ty(rhs).peel_refs().kind(), ty::Uint(_) | ty::Int(_)) + && bit_sp.ctxt() == ctxt + && let Some(Constant::Int(cmp_val)) = ecx.eval(rhs) + && walk_span_to_context(rhs.span, ctxt).is_some() + { + Some((cmp_op, bit_op, bit_lhs, bit_rhs, cmp_val)) + } else { + None + } + }, + |r| (r.0.swap_operands(), r.1, r.2, r.3, r.4), + ) else { + return; + }; + + if !ctxt.in_external_macro(cx.tcx.sess.source_map()) + && let ty = typeck.expr_ty(bit_lhs).peel_refs() + && matches!(ty.kind(), ty::Uint(_) | ty::Int(_)) + && matches!(typeck.expr_ty(bit_rhs).peel_refs().kind(), ty::Uint(_) | ty::Int(_)) + && let Some(Constant::Int(op_val)) = try_maybe_swap( + bit_lhs, + bit_rhs, + |_, e| ecx.eval(e).filter(|_| walk_span_to_context(e.span, ctxt).is_some()), + identity, + ) + { + let (lint, msg, note) = if matches!(bit_op, BitOp::And) && op_val == 0 { + let is_eq = cmp_op.matches_ordering(0.cmp(&cmp_val)); + (BAD_BIT_MASK, LintMsg::from_cmp_result(is_eq), NoteMsg::AndZero) + } else { + match cmp_op.kind { + CmpOpKind::Eq => { + let help_msg = match bit_op { + BitOp::And if op_val & cmp_val != cmp_val => NoteMsg::OpMissingBits, + BitOp::Or if op_val | cmp_val != cmp_val => NoteMsg::OpExtraBits, + _ => return, + }; + (BAD_BIT_MASK, LintMsg::from_cmp_result(cmp_op.negate), help_msg) + }, + CmpOpKind::Lt if matches!(ty.kind(), ty::Uint(_)) => match bit_op { + BitOp::And if op_val < cmp_val => { + (BAD_BIT_MASK, LintMsg::from_cmp_result(!cmp_op.negate), NoteMsg::OpLt) + }, + BitOp::Or if op_val >= cmp_val => { + (BAD_BIT_MASK, LintMsg::from_cmp_result(cmp_op.negate), NoteMsg::OpGe) + }, + BitOp::Xor if op_val == 0 && cmp_val == 0 => ( + BAD_BIT_MASK, + LintMsg::from_cmp_result(cmp_op.negate), + NoteMsg::XorCmpZero, + ), + BitOp::Xor | BitOp::Or if u128::MAX.wrapping_shl(cmp_val.trailing_zeros()) & op_val == 0 => ( + INEFFECTIVE_BIT_MASK, + LintMsg::Ineffective, + NoteMsg::OpBitsInTrailingZeros, + ), + _ => return, + }, + CmpOpKind::Gt if matches!(ty.kind(), ty::Uint(_)) => match bit_op { + BitOp::And if op_val <= cmp_val => { + (BAD_BIT_MASK, LintMsg::from_cmp_result(!cmp_op.negate), NoteMsg::OpLe) + }, + BitOp::Or if op_val > cmp_val => { + (BAD_BIT_MASK, LintMsg::from_cmp_result(cmp_op.negate), NoteMsg::OpGt) + }, + BitOp::Xor | BitOp::Or if u128::MAX.unbounded_shl(cmp_val.trailing_ones()) & op_val == 0 => ( + INEFFECTIVE_BIT_MASK, + LintMsg::Ineffective, + NoteMsg::OpBitsInTrailingOnes, + ), + _ => return, + }, + CmpOpKind::Lt | CmpOpKind::Gt => return, + } + }; + + if !is_from_proc_macro(cx, e) { + span_lint_and_then(cx, lint, e.span, msg.to_str(), |diag| { + diag.note(note.to_string(op_val, cmp_val)); + }); } } } -#[must_use] -fn invert_cmp(cmp: BinOpKind) -> BinOpKind { - match cmp { - BinOpKind::Eq => BinOpKind::Eq, - BinOpKind::Ne => BinOpKind::Ne, - BinOpKind::Lt => BinOpKind::Gt, - BinOpKind::Gt => BinOpKind::Lt, - BinOpKind::Le => BinOpKind::Ge, - BinOpKind::Ge => BinOpKind::Le, - _ => BinOpKind::Or, // Dummy +fn try_maybe_swap( + lhs: &T, + rhs: &T, + mut f: impl FnMut(&T, &T) -> Option, + inv: impl FnOnce(R) -> R, +) -> Option { + if let Some(x) = f(lhs, rhs) { + Some(x) + } else { + f(rhs, lhs).map(inv) } } -fn check_compare<'a>(cx: &LateContext<'a>, bit_op: &Expr<'a>, cmp_op: BinOpKind, cmp_value: u128, span: Span) { - if let ExprKind::Binary(op, left, right) = &bit_op.kind { - if op.node != BinOpKind::BitAnd && op.node != BinOpKind::BitOr || is_from_proc_macro(cx, bit_op) { - return; +#[derive(Clone, Copy)] +struct CmpOp { + pub kind: CmpOpKind, + pub negate: bool, +} +impl CmpOp { + fn from_bin_op(op: BinOpKind) -> Option { + match op { + BinOpKind::Eq => Some(Self { + kind: CmpOpKind::Eq, + negate: false, + }), + BinOpKind::Lt => Some(Self { + kind: CmpOpKind::Lt, + negate: false, + }), + BinOpKind::Le => Some(Self { + kind: CmpOpKind::Gt, + negate: true, + }), + BinOpKind::Ne => Some(Self { + kind: CmpOpKind::Eq, + negate: true, + }), + BinOpKind::Ge => Some(Self { + kind: CmpOpKind::Lt, + negate: true, + }), + BinOpKind::Gt => Some(Self { + kind: CmpOpKind::Gt, + negate: false, + }), + _ => None, } - if let Some(mask) = fetch_int_literal(cx, right).or_else(|| fetch_int_literal(cx, left)) { - check_bit_mask(cx, op.node, cmp_op, mask, cmp_value, span); + } + + fn swap_operands(self) -> Self { + Self { + kind: self.kind.swap_operands(), + negate: self.negate, } } + + fn matches_ordering(self, ord: Ordering) -> bool { + let res = matches!( + (self.kind, ord), + (CmpOpKind::Lt, Ordering::Less) | (CmpOpKind::Eq, Ordering::Equal) | (CmpOpKind::Gt, Ordering::Greater) + ); + if self.negate { !res } else { res } + } } -#[allow(clippy::too_many_lines)] -fn check_bit_mask( - cx: &LateContext<'_>, - bit_op: BinOpKind, - cmp_op: BinOpKind, - mask_value: u128, - cmp_value: u128, - span: Span, -) { - match cmp_op { - BinOpKind::Eq | BinOpKind::Ne => match bit_op { - BinOpKind::BitAnd => { - if mask_value & cmp_value != cmp_value { - if cmp_value != 0 { - span_lint( - cx, - BAD_BIT_MASK, - span, - format!("incompatible bit mask: `_ & {mask_value}` can never be equal to `{cmp_value}`"), - ); - } - } else if mask_value == 0 { - span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero"); - } - }, - BinOpKind::BitOr => { - if mask_value | cmp_value != cmp_value { - span_lint( - cx, - BAD_BIT_MASK, - span, - format!("incompatible bit mask: `_ | {mask_value}` can never be equal to `{cmp_value}`"), - ); - } - }, - _ => (), - }, - BinOpKind::Lt | BinOpKind::Ge => match bit_op { - BinOpKind::BitAnd => { - if mask_value < cmp_value { - span_lint( - cx, - BAD_BIT_MASK, - span, - format!("incompatible bit mask: `_ & {mask_value}` will always be lower than `{cmp_value}`"), - ); - } else if mask_value == 0 { - span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero"); - } +#[derive(Clone, Copy)] +enum CmpOpKind { + Eq, + Lt, + Gt, +} +impl CmpOpKind { + fn swap_operands(self) -> Self { + match self { + Self::Eq => Self::Eq, + Self::Lt => Self::Gt, + Self::Gt => Self::Lt, + } + } +} + +#[derive(Clone, Copy)] +enum BitOp { + Xor, + And, + Or, +} +impl BitOp { + fn from_bin_op(op: BinOpKind) -> Option { + match op { + BinOpKind::BitXor => Some(Self::Xor), + BinOpKind::BitAnd => Some(Self::And), + BinOpKind::BitOr => Some(Self::Or), + _ => None, + } + } +} + +#[derive(Clone, Copy)] +enum NoteMsg { + AndZero, + XorCmpZero, + OpMissingBits, + OpExtraBits, + OpLt, + OpLe, + OpGt, + OpGe, + OpBitsInTrailingZeros, + OpBitsInTrailingOnes, +} +impl NoteMsg { + pub fn to_string(self, op_val: u128, cmp_val: u128) -> String { + match self { + Self::AndZero => "`_ & 0` is always equal to zero".to_owned(), + Self::XorCmpZero => "with constants resolved this is `_ ^ 0 < 0`".to_owned(), + Self::OpMissingBits => { + format!("`0x{op_val:x}` is missing bits contained in the compared constant `0x{cmp_val:x}`") }, - BinOpKind::BitOr => { - if mask_value >= cmp_value { - span_lint( - cx, - BAD_BIT_MASK, - span, - format!("incompatible bit mask: `_ | {mask_value}` will never be lower than `{cmp_value}`"), - ); - } else { - check_ineffective_lt(cx, span, mask_value, cmp_value, "|"); - } + Self::OpExtraBits => { + format!("`0x{op_val:x}` has bits not contained in the compared constant `0x{cmp_val:x}`") }, - BinOpKind::BitXor => check_ineffective_lt(cx, span, mask_value, cmp_value, "^"), - _ => (), - }, - BinOpKind::Le | BinOpKind::Gt => match bit_op { - BinOpKind::BitAnd => { - if mask_value <= cmp_value { - span_lint( - cx, - BAD_BIT_MASK, - span, - format!("incompatible bit mask: `_ & {mask_value}` will never be higher than `{cmp_value}`"), - ); - } else if mask_value == 0 { - span_lint(cx, BAD_BIT_MASK, span, "&-masking with zero"); - } + Self::OpLt => format!("`0x{op_val:x}` is less than the compared constant `0x{cmp_val:x}`"), + Self::OpLe => { + format!("`0x{op_val:x}` is less than or equal to the compared constant `0x{cmp_val:x}`") }, - BinOpKind::BitOr => { - if mask_value > cmp_value { - span_lint( - cx, - BAD_BIT_MASK, - span, - format!("incompatible bit mask: `_ | {mask_value}` will always be higher than `{cmp_value}`"), - ); - } else { - check_ineffective_gt(cx, span, mask_value, cmp_value, "|"); - } + Self::OpGt => format!("`0x{op_val:x}` is greater than the compared constant `0x{cmp_val:x}`"), + Self::OpGe => { + format!("`0x{op_val:x}` is greater than or equal to the compared constant `0x{cmp_val:x}`") }, - BinOpKind::BitXor => check_ineffective_gt(cx, span, mask_value, cmp_value, "^"), - _ => (), - }, - _ => (), + Self::OpBitsInTrailingZeros => format!( + "`0x{op_val:x}` contains only bits in the trailing zeros of the compared constant `0x{cmp_val:x}`" + ), + Self::OpBitsInTrailingOnes => format!( + "`0x{op_val:x}` contains only bits in the trailing ones of the compared constant `0x{cmp_val:x}`" + ), + } } } -fn check_ineffective_lt(cx: &LateContext<'_>, span: Span, m: u128, c: u128, op: &str) { - if c.is_power_of_two() && m < c { - span_lint( - cx, - INEFFECTIVE_BIT_MASK, - span, - format!("ineffective bit mask: `x {op} {m}` compared to `{c}`, is the same as x compared directly"), - ); - } +#[derive(Clone, Copy)] +enum LintMsg { + AlwaysTrue, + AlwaysFalse, + Ineffective, } - -fn check_ineffective_gt(cx: &LateContext<'_>, span: Span, m: u128, c: u128, op: &str) { - if (c + 1).is_power_of_two() && m <= c { - span_lint( - cx, - INEFFECTIVE_BIT_MASK, - span, - format!("ineffective bit mask: `x {op} {m}` compared to `{c}`, is the same as x compared directly"), - ); +impl LintMsg { + fn from_cmp_result(res: bool) -> Self { + if res { Self::AlwaysTrue } else { Self::AlwaysFalse } } -} -fn fetch_int_literal(cx: &LateContext<'_>, lit: &Expr<'_>) -> Option { - match ConstEvalCtxt::new(cx).eval(lit)? { - Constant::Int(n) => Some(n), - _ => None, + fn to_str(self) -> &'static str { + match self { + Self::AlwaysTrue => "this comparison is always true", + Self::AlwaysFalse => "this comparison is always false", + Self::Ineffective => "this comparison's result is unaffected by the bitwise operation", + } } } diff --git a/tests/ui/bad_bit_masks.stderr b/tests/ui/bad_bit_masks.stderr index b37676b8665a..f89fa9e12032 100644 --- a/tests/ui/bad_bit_masks.stderr +++ b/tests/ui/bad_bit_masks.stderr @@ -1,233 +1,308 @@ -error: &-masking with zero +error: this comparison is always true --> tests/ui/bad_bit_masks.rs:12:13 | LL | let _ = x & 0b0000 == 0b0000; | ^^^^^^^^^^^^^^^^^^^^ | + = note: `_ & 0` is always equal to zero = note: `-D clippy::bad-bit-mask` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::bad_bit_mask)]` -error: incompatible bit mask: `_ & 0` can never be equal to `1` +error: this comparison is always false --> tests/ui/bad_bit_masks.rs:13:13 | LL | let _ = x & 0b0000 == 0b0001; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `_ & 0` is always equal to zero -error: incompatible bit mask: `_ & 2` can never be equal to `1` +error: this comparison is always false --> tests/ui/bad_bit_masks.rs:16:13 | LL | let _ = x & 0b0010 == 0b0001; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x2` is missing bits contained in the compared constant `0x1` -error: incompatible bit mask: `_ & 1` can never be equal to `2` +error: this comparison is always false --> tests/ui/bad_bit_masks.rs:17:13 | LL | let _ = x & 0b0001 == 0b0010; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x1` is missing bits contained in the compared constant `0x2` -error: incompatible bit mask: `_ & 3` can never be equal to `4` +error: this comparison is always false --> tests/ui/bad_bit_masks.rs:21:13 | LL | let _ = x & 0b0011 == 0b0100; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x3` is missing bits contained in the compared constant `0x4` -error: incompatible bit mask: `_ & 6` can never be equal to `7` +error: this comparison is always false --> tests/ui/bad_bit_masks.rs:23:13 | LL | let _ = x & 0b0110 == 0b0111; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x6` is missing bits contained in the compared constant `0x7` -error: incompatible bit mask: `_ & 3` can never be equal to `5` +error: this comparison is always false --> tests/ui/bad_bit_masks.rs:24:13 | LL | let _ = x & 0b0011 == 0b0101; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x3` is missing bits contained in the compared constant `0x5` -error: incompatible bit mask: `_ | 1` can never be equal to `0` +error: this comparison is always false --> tests/ui/bad_bit_masks.rs:30:13 | LL | let _ = x | 0b0001 == 0b0000; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x1` has bits not contained in the compared constant `0x0` -error: incompatible bit mask: `_ | 2` can never be equal to `1` +error: this comparison is always false --> tests/ui/bad_bit_masks.rs:32:13 | LL | let _ = x | 0b0010 == 0b0001; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x2` has bits not contained in the compared constant `0x1` -error: incompatible bit mask: `_ | 1` can never be equal to `2` +error: this comparison is always false --> tests/ui/bad_bit_masks.rs:33:13 | LL | let _ = x | 0b0001 == 0b0010; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x1` has bits not contained in the compared constant `0x2` -error: incompatible bit mask: `_ | 3` can never be equal to `1` +error: this comparison is always false --> tests/ui/bad_bit_masks.rs:34:13 | LL | let _ = x | 0b0011 == 0b0001; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x3` has bits not contained in the compared constant `0x1` -error: incompatible bit mask: `_ | 3` can never be equal to `2` +error: this comparison is always false --> tests/ui/bad_bit_masks.rs:35:13 | LL | let _ = x | 0b0011 == 0b0010; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x3` has bits not contained in the compared constant `0x2` -error: incompatible bit mask: `_ | 3` can never be equal to `4` +error: this comparison is always false --> tests/ui/bad_bit_masks.rs:37:13 | LL | let _ = x | 0b0011 == 0b0100; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x3` has bits not contained in the compared constant `0x4` -error: incompatible bit mask: `_ | 3` can never be equal to `5` +error: this comparison is always false --> tests/ui/bad_bit_masks.rs:40:13 | LL | let _ = x | 0b0011 == 0b0101; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x3` has bits not contained in the compared constant `0x5` -error: incompatible bit mask: `_ | 15` can never be equal to `14` +error: this comparison is always false --> tests/ui/bad_bit_masks.rs:41:13 | LL | let _ = x | 0b1111 == 0b1110; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0xf` has bits not contained in the compared constant `0xe` -error: incompatible bit mask: `_ | 15` can never be equal to `10` +error: this comparison is always false --> tests/ui/bad_bit_masks.rs:42:13 | LL | let _ = x | 0b1111 == 0b1010; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0xf` has bits not contained in the compared constant `0xa` -error: &-masking with zero +error: this comparison is always false --> tests/ui/bad_bit_masks.rs:44:13 | LL | let _ = x & 0b0000 != 0b0000; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `_ & 0` is always equal to zero -error: incompatible bit mask: `_ & 0` can never be equal to `1` +error: this comparison is always true --> tests/ui/bad_bit_masks.rs:45:13 | LL | let _ = x & 0b0000 != 0b0001; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `_ & 0` is always equal to zero -error: incompatible bit mask: `_ & 2` can never be equal to `1` +error: this comparison is always true --> tests/ui/bad_bit_masks.rs:48:13 | LL | let _ = x & 0b0010 != 0b0001; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x2` is missing bits contained in the compared constant `0x1` -error: incompatible bit mask: `_ & 1` can never be equal to `2` +error: this comparison is always true --> tests/ui/bad_bit_masks.rs:49:13 | LL | let _ = x & 0b0001 != 0b0010; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x1` is missing bits contained in the compared constant `0x2` -error: incompatible bit mask: `_ & 3` can never be equal to `4` +error: this comparison is always true --> tests/ui/bad_bit_masks.rs:53:13 | LL | let _ = x & 0b0011 != 0b0100; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x3` is missing bits contained in the compared constant `0x4` -error: incompatible bit mask: `_ & 6` can never be equal to `7` +error: this comparison is always true --> tests/ui/bad_bit_masks.rs:55:13 | LL | let _ = x & 0b0110 != 0b0111; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x6` is missing bits contained in the compared constant `0x7` -error: incompatible bit mask: `_ & 3` can never be equal to `5` +error: this comparison is always true --> tests/ui/bad_bit_masks.rs:56:13 | LL | let _ = x & 0b0011 != 0b0101; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x3` is missing bits contained in the compared constant `0x5` -error: incompatible bit mask: `_ | 1` can never be equal to `0` +error: this comparison is always true --> tests/ui/bad_bit_masks.rs:62:13 | LL | let _ = x | 0b0001 != 0b0000; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x1` has bits not contained in the compared constant `0x0` -error: incompatible bit mask: `_ | 2` can never be equal to `1` +error: this comparison is always true --> tests/ui/bad_bit_masks.rs:64:13 | LL | let _ = x | 0b0010 != 0b0001; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x2` has bits not contained in the compared constant `0x1` -error: incompatible bit mask: `_ | 1` can never be equal to `2` +error: this comparison is always true --> tests/ui/bad_bit_masks.rs:65:13 | LL | let _ = x | 0b0001 != 0b0010; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x1` has bits not contained in the compared constant `0x2` -error: incompatible bit mask: `_ | 3` can never be equal to `1` +error: this comparison is always true --> tests/ui/bad_bit_masks.rs:66:13 | LL | let _ = x | 0b0011 != 0b0001; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x3` has bits not contained in the compared constant `0x1` -error: incompatible bit mask: `_ | 3` can never be equal to `2` +error: this comparison is always true --> tests/ui/bad_bit_masks.rs:67:13 | LL | let _ = x | 0b0011 != 0b0010; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x3` has bits not contained in the compared constant `0x2` -error: incompatible bit mask: `_ | 3` can never be equal to `4` +error: this comparison is always true --> tests/ui/bad_bit_masks.rs:69:13 | LL | let _ = x | 0b0011 != 0b0100; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x3` has bits not contained in the compared constant `0x4` -error: incompatible bit mask: `_ | 3` can never be equal to `5` +error: this comparison is always true --> tests/ui/bad_bit_masks.rs:72:13 | LL | let _ = x | 0b0011 != 0b0101; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x3` has bits not contained in the compared constant `0x5` -error: incompatible bit mask: `_ | 15` can never be equal to `14` +error: this comparison is always true --> tests/ui/bad_bit_masks.rs:73:13 | LL | let _ = x | 0b1111 != 0b1110; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0xf` has bits not contained in the compared constant `0xe` -error: incompatible bit mask: `_ | 15` can never be equal to `10` +error: this comparison is always true --> tests/ui/bad_bit_masks.rs:74:13 | LL | let _ = x | 0b1111 != 0b1010; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0xf` has bits not contained in the compared constant `0xa` -error: incompatible bit mask: `_ & 2` can never be equal to `1` +error: this comparison is always false --> tests/ui/bad_bit_masks.rs:76:13 | LL | let _ = 0b0010 & x == 0b0001; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x2` is missing bits contained in the compared constant `0x1` -error: incompatible bit mask: `_ & 2` can never be equal to `1` +error: this comparison is always false --> tests/ui/bad_bit_masks.rs:77:13 | LL | let _ = 0b0001 == x & 0b0010; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x2` is missing bits contained in the compared constant `0x1` -error: incompatible bit mask: `_ & 2` can never be equal to `1` +error: this comparison is always false --> tests/ui/bad_bit_masks.rs:78:13 | LL | let _ = 0b0001 == 0b0010 & x; | ^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x2` is missing bits contained in the compared constant `0x1` -error: incompatible bit mask: `_ & 6` can never be equal to `15` +error: this comparison is always false --> tests/ui/bad_bit_masks.rs:80:13 | LL | let _ = x & (0b0100 | 0b0010) == (0b0111 ^ 0b1000); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x6` is missing bits contained in the compared constant `0xf` -error: incompatible bit mask: `_ & 3` can never be equal to `4` +error: this comparison is always false --> tests/ui/bad_bit_masks.rs:97:17 | LL | let _ = x & C == 0b0100; | ^^^^^^^^^^^^^^^ + | + = note: `0x3` is missing bits contained in the compared constant `0x4` -error: incompatible bit mask: `_ & 1` can never be equal to `3` +error: this comparison is always false --> tests/ui/bad_bit_masks.rs:98:17 | LL | let _ = x & 0b0001 == C; | ^^^^^^^^^^^^^^^ + | + = note: `0x1` is missing bits contained in the compared constant `0x3` error: aborting due to 38 previous errors diff --git a/tests/ui/ineffective_bit_masks.rs b/tests/ui/ineffective_bit_masks.rs index aa61c43a429a..5631f645b69b 100644 --- a/tests/ui/ineffective_bit_masks.rs +++ b/tests/ui/ineffective_bit_masks.rs @@ -40,46 +40,46 @@ fn main() { let _ = x | 0b00_0111 > 0b00_1111; //~ ineffective_bit_mask let _ = x | 0b00_1000 > 0b00_1111; //~ ineffective_bit_mask let _ = x | 0b00_1001 > 0b00_1111; //~ ineffective_bit_mask - let _ = x | 0b00_1111 > 0b10_1111; + let _ = x | 0b00_1111 > 0b10_1111; //~ ineffective_bit_mask let _ = x | 0b01_0000 > 0b10_1111; let _ = x | 0b01_1111 > 0b10_1111; let _ = x | 0b10_0000 > 0b10_1111; let _ = x | 0b11_1111 > 0b10_1111; - let _ = x | 0b00_1111 <= 0b10_1111; + let _ = x | 0b00_1111 <= 0b10_1111; //~ ineffective_bit_mask let _ = x | 0b11_0000 <= 0b10_1111; - let _ = x ^ 0b00_0000 > 0b00_0000; + let _ = x ^ 0b00_0000 > 0b00_0000; //~ ineffective_bit_mask let _ = x ^ 0b00_0001 > 0b00_0000; - let _ = x ^ 0b00_0001 > 0b00_0001; + let _ = x ^ 0b00_0001 > 0b00_0001; //~ ineffective_bit_mask let _ = x ^ 0b00_0010 > 0b00_0001; let _ = x ^ 0b00_0011 > 0b00_0001; - let _ = x ^ 0b00_0000 > 0b00_0011; - let _ = x ^ 0b00_0001 > 0b00_0011; - let _ = x ^ 0b00_0010 > 0b00_0011; - let _ = x ^ 0b00_0011 > 0b00_0011; + let _ = x ^ 0b00_0000 > 0b00_0011; //~ ineffective_bit_mask + let _ = x ^ 0b00_0001 > 0b00_0011; //~ ineffective_bit_mask + let _ = x ^ 0b00_0010 > 0b00_0011; //~ ineffective_bit_mask + let _ = x ^ 0b00_0011 > 0b00_0011; //~ ineffective_bit_mask let _ = x ^ 0b00_0100 > 0b00_0011; let _ = x ^ 0b00_0101 > 0b00_0011; - let _ = x ^ 0b00_0001 > 0b00_0111; - let _ = x ^ 0b00_0010 > 0b00_0111; - let _ = x ^ 0b00_0011 > 0b00_0111; - let _ = x ^ 0b00_0100 > 0b00_0111; + let _ = x ^ 0b00_0001 > 0b00_0111; //~ ineffective_bit_mask + let _ = x ^ 0b00_0010 > 0b00_0111; //~ ineffective_bit_mask + let _ = x ^ 0b00_0011 > 0b00_0111; //~ ineffective_bit_mask + let _ = x ^ 0b00_0100 > 0b00_0111; //~ ineffective_bit_mask let _ = x ^ 0b00_1000 > 0b00_0111; let _ = x ^ 0b00_1010 > 0b00_0111; - let _ = x ^ 0b00_0001 > 0b00_1111; - let _ = x ^ 0b00_0010 > 0b00_1111; - let _ = x ^ 0b00_0011 > 0b00_1111; - let _ = x ^ 0b00_0100 > 0b00_1111; - let _ = x ^ 0b00_0101 > 0b00_1111; - let _ = x ^ 0b00_0110 > 0b00_1111; - let _ = x ^ 0b00_0111 > 0b00_1111; - let _ = x ^ 0b00_1000 > 0b00_1111; - let _ = x ^ 0b00_1001 > 0b00_1111; - let _ = x ^ 0b00_1111 > 0b10_1111; + let _ = x ^ 0b00_0001 > 0b00_1111; //~ ineffective_bit_mask + let _ = x ^ 0b00_0010 > 0b00_1111; //~ ineffective_bit_mask + let _ = x ^ 0b00_0011 > 0b00_1111; //~ ineffective_bit_mask + let _ = x ^ 0b00_0100 > 0b00_1111; //~ ineffective_bit_mask + let _ = x ^ 0b00_0101 > 0b00_1111; //~ ineffective_bit_mask + let _ = x ^ 0b00_0110 > 0b00_1111; //~ ineffective_bit_mask + let _ = x ^ 0b00_0111 > 0b00_1111; //~ ineffective_bit_mask + let _ = x ^ 0b00_1000 > 0b00_1111; //~ ineffective_bit_mask + let _ = x ^ 0b00_1001 > 0b00_1111; //~ ineffective_bit_mask + let _ = x ^ 0b00_1111 > 0b10_1111; //~ ineffective_bit_mask let _ = x ^ 0b01_0000 > 0b10_1111; let _ = x ^ 0b01_1111 > 0b10_1111; let _ = x ^ 0b10_0000 > 0b10_1111; let _ = x ^ 0b11_1111 > 0b10_1111; - let _ = x ^ 0b00_1111 <= 0b10_1111; + let _ = x ^ 0b00_1111 <= 0b10_1111; //~ ineffective_bit_mask let _ = x ^ 0b11_0000 <= 0b10_1111; let _ = x | 0b00_0000 < 0b00_0000; @@ -112,12 +112,12 @@ fn main() { let _ = x | 0b00_0111 < 0b01_0000; //~ ineffective_bit_mask let _ = x | 0b00_1000 < 0b01_0000; //~ ineffective_bit_mask let _ = x | 0b00_1001 < 0b01_0000; //~ ineffective_bit_mask - let _ = x | 0b00_1111 < 0b11_0000; + let _ = x | 0b00_1111 < 0b11_0000; //~ ineffective_bit_mask let _ = x | 0b01_0000 < 0b11_0000; let _ = x | 0b01_1111 < 0b11_0000; let _ = x | 0b10_0000 < 0b11_0000; let _ = x | 0b11_1111 < 0b11_0000; - let _ = x | 0b00_1111 >= 0b11_0000; + let _ = x | 0b00_1111 >= 0b11_0000; //~ ineffective_bit_mask let _ = x | 0b11_0000 >= 0b11_0000; let _ = x ^ 0b00_0000 < 0b00_0000; @@ -125,49 +125,49 @@ fn main() { let _ = x ^ 0b00_0001 < 0b00_0001; let _ = x ^ 0b00_0010 < 0b00_0001; let _ = x ^ 0b00_0011 < 0b00_0001; - let _ = x ^ 0b00_0000 < 0b00_0010; - let _ = x ^ 0b00_0001 < 0b00_0010; + let _ = x ^ 0b00_0000 < 0b00_0010; //~ ineffective_bit_mask + let _ = x ^ 0b00_0001 < 0b00_0010; //~ ineffective_bit_mask let _ = x ^ 0b00_0010 < 0b00_0010; let _ = x ^ 0b00_0011 < 0b00_0010; - let _ = x ^ 0b00_0000 < 0b00_0100; - let _ = x ^ 0b00_0001 < 0b00_0100; - let _ = x ^ 0b00_0010 < 0b00_0100; - let _ = x ^ 0b00_0011 < 0b00_0100; + let _ = x ^ 0b00_0000 < 0b00_0100; //~ ineffective_bit_mask + let _ = x ^ 0b00_0001 < 0b00_0100; //~ ineffective_bit_mask + let _ = x ^ 0b00_0010 < 0b00_0100; //~ ineffective_bit_mask + let _ = x ^ 0b00_0011 < 0b00_0100; //~ ineffective_bit_mask let _ = x ^ 0b00_0100 < 0b00_0100; let _ = x ^ 0b00_0101 < 0b00_0100; - let _ = x ^ 0b00_0001 < 0b00_1000; - let _ = x ^ 0b00_0010 < 0b00_1000; - let _ = x ^ 0b00_0011 < 0b00_1000; - let _ = x ^ 0b00_0100 < 0b00_1000; + let _ = x ^ 0b00_0001 < 0b00_1000; //~ ineffective_bit_mask + let _ = x ^ 0b00_0010 < 0b00_1000; //~ ineffective_bit_mask + let _ = x ^ 0b00_0011 < 0b00_1000; //~ ineffective_bit_mask + let _ = x ^ 0b00_0100 < 0b00_1000; //~ ineffective_bit_mask let _ = x ^ 0b00_1000 < 0b00_1000; let _ = x ^ 0b00_1010 < 0b00_1000; - let _ = x ^ 0b00_0001 < 0b01_0000; - let _ = x ^ 0b00_0010 < 0b01_0000; - let _ = x ^ 0b00_0011 < 0b01_0000; - let _ = x ^ 0b00_0100 < 0b01_0000; - let _ = x ^ 0b00_0101 < 0b01_0000; - let _ = x ^ 0b00_0110 < 0b01_0000; - let _ = x ^ 0b00_0111 < 0b01_0000; - let _ = x ^ 0b00_1000 < 0b01_0000; - let _ = x ^ 0b00_1001 < 0b01_0000; - let _ = x ^ 0b00_1111 < 0b11_0000; + let _ = x ^ 0b00_0001 < 0b01_0000; //~ ineffective_bit_mask + let _ = x ^ 0b00_0010 < 0b01_0000; //~ ineffective_bit_mask + let _ = x ^ 0b00_0011 < 0b01_0000; //~ ineffective_bit_mask + let _ = x ^ 0b00_0100 < 0b01_0000; //~ ineffective_bit_mask + let _ = x ^ 0b00_0101 < 0b01_0000; //~ ineffective_bit_mask + let _ = x ^ 0b00_0110 < 0b01_0000; //~ ineffective_bit_mask + let _ = x ^ 0b00_0111 < 0b01_0000; //~ ineffective_bit_mask + let _ = x ^ 0b00_1000 < 0b01_0000; //~ ineffective_bit_mask + let _ = x ^ 0b00_1001 < 0b01_0000; //~ ineffective_bit_mask + let _ = x ^ 0b00_1111 < 0b11_0000; //~ ineffective_bit_mask let _ = x ^ 0b01_0000 < 0b11_0000; let _ = x ^ 0b01_1111 < 0b11_0000; let _ = x ^ 0b10_0000 < 0b11_0000; let _ = x ^ 0b11_1111 < 0b11_0000; - let _ = x ^ 0b00_1111 >= 0b11_0000; + let _ = x ^ 0b00_1111 >= 0b11_0000; //~ ineffective_bit_mask let _ = x ^ 0b11_0000 >= 0b11_0000; let _ = x | 0x7fff_ffff > 0xffff_ffff; //~ ineffective_bit_mask let _ = x | 0x8000_0000 > 0xffff_ffff; //~ ineffective_bit_mask let _ = x | 0xffff_ffff > 0xffff_ffff; //~ ineffective_bit_mask - let _ = x ^ 0x7fff_ffff > 0xffff_ffff; - let _ = x ^ 0x8000_0000 > 0xffff_ffff; - let _ = x ^ 0xffff_ffff > 0xffff_ffff; + let _ = x ^ 0x7fff_ffff > 0xffff_ffff; //~ ineffective_bit_mask + let _ = x ^ 0x8000_0000 > 0xffff_ffff; //~ ineffective_bit_mask + let _ = x ^ 0xffff_ffff > 0xffff_ffff; //~ ineffective_bit_mask let _ = x | 0x7fff_ffff < 0x8000_0000; //~ ineffective_bit_mask let _ = x | 0x8000_0000 < 0x8000_0000; - let _ = x ^ 0x7fff_ffff < 0x8000_0000; + let _ = x ^ 0x7fff_ffff < 0x8000_0000; //~ ineffective_bit_mask let _ = x ^ 0x8000_0000 < 0x8000_0000; let _ = x | 0b00_0001 > 0b00_0100; diff --git a/tests/ui/ineffective_bit_masks.stderr b/tests/ui/ineffective_bit_masks.stderr index 3af48bf7f639..39797793dae1 100644 --- a/tests/ui/ineffective_bit_masks.stderr +++ b/tests/ui/ineffective_bit_masks.stderr @@ -1,256 +1,739 @@ -error: ineffective bit mask: `x | 0` compared to `0`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:17:13 | LL | let _ = x | 0b00_0000 > 0b00_0000; | ^^^^^^^^^^^^^^^^^^^^^^^^^ | + = note: `0x0` contains only bits in the trailing ones of the compared constant `0x0` = note: `#[deny(clippy::ineffective_bit_mask)]` on by default -error: ineffective bit mask: `x | 1` compared to `1`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:19:13 | LL | let _ = x | 0b00_0001 > 0b00_0001; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x1` contains only bits in the trailing ones of the compared constant `0x1` -error: ineffective bit mask: `x | 0` compared to `3`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:22:13 | LL | let _ = x | 0b00_0000 > 0b00_0011; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x0` contains only bits in the trailing ones of the compared constant `0x3` -error: ineffective bit mask: `x | 1` compared to `3`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:23:13 | LL | let _ = x | 0b00_0001 > 0b00_0011; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x1` contains only bits in the trailing ones of the compared constant `0x3` -error: ineffective bit mask: `x | 2` compared to `3`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:24:13 | LL | let _ = x | 0b00_0010 > 0b00_0011; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x2` contains only bits in the trailing ones of the compared constant `0x3` -error: ineffective bit mask: `x | 3` compared to `3`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:25:13 | LL | let _ = x | 0b00_0011 > 0b00_0011; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x3` contains only bits in the trailing ones of the compared constant `0x3` -error: ineffective bit mask: `x | 1` compared to `7`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:28:13 | LL | let _ = x | 0b00_0001 > 0b00_0111; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x1` contains only bits in the trailing ones of the compared constant `0x7` -error: ineffective bit mask: `x | 2` compared to `7`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:29:13 | LL | let _ = x | 0b00_0010 > 0b00_0111; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x2` contains only bits in the trailing ones of the compared constant `0x7` -error: ineffective bit mask: `x | 3` compared to `7`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:30:13 | LL | let _ = x | 0b00_0011 > 0b00_0111; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x3` contains only bits in the trailing ones of the compared constant `0x7` -error: ineffective bit mask: `x | 4` compared to `7`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:31:13 | LL | let _ = x | 0b00_0100 > 0b00_0111; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x4` contains only bits in the trailing ones of the compared constant `0x7` -error: ineffective bit mask: `x | 1` compared to `15`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:34:13 | LL | let _ = x | 0b00_0001 > 0b00_1111; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x1` contains only bits in the trailing ones of the compared constant `0xf` -error: ineffective bit mask: `x | 2` compared to `15`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:35:13 | LL | let _ = x | 0b00_0010 > 0b00_1111; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x2` contains only bits in the trailing ones of the compared constant `0xf` -error: ineffective bit mask: `x | 3` compared to `15`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:36:13 | LL | let _ = x | 0b00_0011 > 0b00_1111; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x3` contains only bits in the trailing ones of the compared constant `0xf` -error: ineffective bit mask: `x | 4` compared to `15`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:37:13 | LL | let _ = x | 0b00_0100 > 0b00_1111; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x4` contains only bits in the trailing ones of the compared constant `0xf` -error: ineffective bit mask: `x | 5` compared to `15`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:38:13 | LL | let _ = x | 0b00_0101 > 0b00_1111; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x5` contains only bits in the trailing ones of the compared constant `0xf` -error: ineffective bit mask: `x | 6` compared to `15`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:39:13 | LL | let _ = x | 0b00_0110 > 0b00_1111; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x6` contains only bits in the trailing ones of the compared constant `0xf` -error: ineffective bit mask: `x | 7` compared to `15`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:40:13 | LL | let _ = x | 0b00_0111 > 0b00_1111; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x7` contains only bits in the trailing ones of the compared constant `0xf` -error: ineffective bit mask: `x | 8` compared to `15`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:41:13 | LL | let _ = x | 0b00_1000 > 0b00_1111; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x8` contains only bits in the trailing ones of the compared constant `0xf` -error: ineffective bit mask: `x | 9` compared to `15`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:42:13 | LL | let _ = x | 0b00_1001 > 0b00_1111; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x9` contains only bits in the trailing ones of the compared constant `0xf` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:43:13 + | +LL | let _ = x | 0b00_1111 > 0b10_1111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0xf` contains only bits in the trailing ones of the compared constant `0x2f` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:48:13 + | +LL | let _ = x | 0b00_1111 <= 0b10_1111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0xf` contains only bits in the trailing ones of the compared constant `0x2f` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:51:13 + | +LL | let _ = x ^ 0b00_0000 > 0b00_0000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x0` contains only bits in the trailing ones of the compared constant `0x0` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:53:13 + | +LL | let _ = x ^ 0b00_0001 > 0b00_0001; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x1` contains only bits in the trailing ones of the compared constant `0x1` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:56:13 + | +LL | let _ = x ^ 0b00_0000 > 0b00_0011; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x0` contains only bits in the trailing ones of the compared constant `0x3` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:57:13 + | +LL | let _ = x ^ 0b00_0001 > 0b00_0011; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x1` contains only bits in the trailing ones of the compared constant `0x3` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:58:13 + | +LL | let _ = x ^ 0b00_0010 > 0b00_0011; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x2` contains only bits in the trailing ones of the compared constant `0x3` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:59:13 + | +LL | let _ = x ^ 0b00_0011 > 0b00_0011; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x3` contains only bits in the trailing ones of the compared constant `0x3` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:62:13 + | +LL | let _ = x ^ 0b00_0001 > 0b00_0111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x1` contains only bits in the trailing ones of the compared constant `0x7` -error: ineffective bit mask: `x | 0` compared to `2`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:63:13 + | +LL | let _ = x ^ 0b00_0010 > 0b00_0111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x2` contains only bits in the trailing ones of the compared constant `0x7` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:64:13 + | +LL | let _ = x ^ 0b00_0011 > 0b00_0111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x3` contains only bits in the trailing ones of the compared constant `0x7` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:65:13 + | +LL | let _ = x ^ 0b00_0100 > 0b00_0111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x4` contains only bits in the trailing ones of the compared constant `0x7` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:68:13 + | +LL | let _ = x ^ 0b00_0001 > 0b00_1111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x1` contains only bits in the trailing ones of the compared constant `0xf` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:69:13 + | +LL | let _ = x ^ 0b00_0010 > 0b00_1111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x2` contains only bits in the trailing ones of the compared constant `0xf` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:70:13 + | +LL | let _ = x ^ 0b00_0011 > 0b00_1111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x3` contains only bits in the trailing ones of the compared constant `0xf` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:71:13 + | +LL | let _ = x ^ 0b00_0100 > 0b00_1111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x4` contains only bits in the trailing ones of the compared constant `0xf` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:72:13 + | +LL | let _ = x ^ 0b00_0101 > 0b00_1111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x5` contains only bits in the trailing ones of the compared constant `0xf` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:73:13 + | +LL | let _ = x ^ 0b00_0110 > 0b00_1111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x6` contains only bits in the trailing ones of the compared constant `0xf` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:74:13 + | +LL | let _ = x ^ 0b00_0111 > 0b00_1111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x7` contains only bits in the trailing ones of the compared constant `0xf` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:75:13 + | +LL | let _ = x ^ 0b00_1000 > 0b00_1111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x8` contains only bits in the trailing ones of the compared constant `0xf` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:76:13 + | +LL | let _ = x ^ 0b00_1001 > 0b00_1111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x9` contains only bits in the trailing ones of the compared constant `0xf` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:77:13 + | +LL | let _ = x ^ 0b00_1111 > 0b10_1111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0xf` contains only bits in the trailing ones of the compared constant `0x2f` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:82:13 + | +LL | let _ = x ^ 0b00_1111 <= 0b10_1111; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0xf` contains only bits in the trailing ones of the compared constant `0x2f` + +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:90:13 | LL | let _ = x | 0b00_0000 < 0b00_0010; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x0` contains only bits in the trailing zeros of the compared constant `0x2` -error: ineffective bit mask: `x | 1` compared to `2`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:91:13 | LL | let _ = x | 0b00_0001 < 0b00_0010; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x1` contains only bits in the trailing zeros of the compared constant `0x2` -error: ineffective bit mask: `x | 0` compared to `4`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:94:13 | LL | let _ = x | 0b00_0000 < 0b00_0100; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x0` contains only bits in the trailing zeros of the compared constant `0x4` -error: ineffective bit mask: `x | 1` compared to `4`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:95:13 | LL | let _ = x | 0b00_0001 < 0b00_0100; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x1` contains only bits in the trailing zeros of the compared constant `0x4` -error: ineffective bit mask: `x | 2` compared to `4`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:96:13 | LL | let _ = x | 0b00_0010 < 0b00_0100; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x2` contains only bits in the trailing zeros of the compared constant `0x4` -error: ineffective bit mask: `x | 3` compared to `4`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:97:13 | LL | let _ = x | 0b00_0011 < 0b00_0100; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x3` contains only bits in the trailing zeros of the compared constant `0x4` -error: ineffective bit mask: `x | 1` compared to `8`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:100:13 | LL | let _ = x | 0b00_0001 < 0b00_1000; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x1` contains only bits in the trailing zeros of the compared constant `0x8` -error: ineffective bit mask: `x | 2` compared to `8`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:101:13 | LL | let _ = x | 0b00_0010 < 0b00_1000; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x2` contains only bits in the trailing zeros of the compared constant `0x8` -error: ineffective bit mask: `x | 3` compared to `8`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:102:13 | LL | let _ = x | 0b00_0011 < 0b00_1000; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x3` contains only bits in the trailing zeros of the compared constant `0x8` -error: ineffective bit mask: `x | 4` compared to `8`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:103:13 | LL | let _ = x | 0b00_0100 < 0b00_1000; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x4` contains only bits in the trailing zeros of the compared constant `0x8` -error: ineffective bit mask: `x | 1` compared to `16`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:106:13 | LL | let _ = x | 0b00_0001 < 0b01_0000; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x1` contains only bits in the trailing zeros of the compared constant `0x10` -error: ineffective bit mask: `x | 2` compared to `16`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:107:13 | LL | let _ = x | 0b00_0010 < 0b01_0000; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x2` contains only bits in the trailing zeros of the compared constant `0x10` -error: ineffective bit mask: `x | 3` compared to `16`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:108:13 | LL | let _ = x | 0b00_0011 < 0b01_0000; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x3` contains only bits in the trailing zeros of the compared constant `0x10` -error: ineffective bit mask: `x | 4` compared to `16`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:109:13 | LL | let _ = x | 0b00_0100 < 0b01_0000; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x4` contains only bits in the trailing zeros of the compared constant `0x10` -error: ineffective bit mask: `x | 5` compared to `16`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:110:13 | LL | let _ = x | 0b00_0101 < 0b01_0000; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x5` contains only bits in the trailing zeros of the compared constant `0x10` -error: ineffective bit mask: `x | 6` compared to `16`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:111:13 | LL | let _ = x | 0b00_0110 < 0b01_0000; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x6` contains only bits in the trailing zeros of the compared constant `0x10` -error: ineffective bit mask: `x | 7` compared to `16`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:112:13 | LL | let _ = x | 0b00_0111 < 0b01_0000; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x7` contains only bits in the trailing zeros of the compared constant `0x10` -error: ineffective bit mask: `x | 8` compared to `16`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:113:13 | LL | let _ = x | 0b00_1000 < 0b01_0000; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x8` contains only bits in the trailing zeros of the compared constant `0x10` -error: ineffective bit mask: `x | 9` compared to `16`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:114:13 | LL | let _ = x | 0b00_1001 < 0b01_0000; | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x9` contains only bits in the trailing zeros of the compared constant `0x10` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:115:13 + | +LL | let _ = x | 0b00_1111 < 0b11_0000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0xf` contains only bits in the trailing zeros of the compared constant `0x30` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:120:13 + | +LL | let _ = x | 0b00_1111 >= 0b11_0000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0xf` contains only bits in the trailing zeros of the compared constant `0x30` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:128:13 + | +LL | let _ = x ^ 0b00_0000 < 0b00_0010; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x0` contains only bits in the trailing zeros of the compared constant `0x2` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:129:13 + | +LL | let _ = x ^ 0b00_0001 < 0b00_0010; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x1` contains only bits in the trailing zeros of the compared constant `0x2` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:132:13 + | +LL | let _ = x ^ 0b00_0000 < 0b00_0100; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x0` contains only bits in the trailing zeros of the compared constant `0x4` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:133:13 + | +LL | let _ = x ^ 0b00_0001 < 0b00_0100; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x1` contains only bits in the trailing zeros of the compared constant `0x4` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:134:13 + | +LL | let _ = x ^ 0b00_0010 < 0b00_0100; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x2` contains only bits in the trailing zeros of the compared constant `0x4` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:135:13 + | +LL | let _ = x ^ 0b00_0011 < 0b00_0100; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x3` contains only bits in the trailing zeros of the compared constant `0x4` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:138:13 + | +LL | let _ = x ^ 0b00_0001 < 0b00_1000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x1` contains only bits in the trailing zeros of the compared constant `0x8` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:139:13 + | +LL | let _ = x ^ 0b00_0010 < 0b00_1000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x2` contains only bits in the trailing zeros of the compared constant `0x8` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:140:13 + | +LL | let _ = x ^ 0b00_0011 < 0b00_1000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x3` contains only bits in the trailing zeros of the compared constant `0x8` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:141:13 + | +LL | let _ = x ^ 0b00_0100 < 0b00_1000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x4` contains only bits in the trailing zeros of the compared constant `0x8` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:144:13 + | +LL | let _ = x ^ 0b00_0001 < 0b01_0000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x1` contains only bits in the trailing zeros of the compared constant `0x10` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:145:13 + | +LL | let _ = x ^ 0b00_0010 < 0b01_0000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x2` contains only bits in the trailing zeros of the compared constant `0x10` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:146:13 + | +LL | let _ = x ^ 0b00_0011 < 0b01_0000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x3` contains only bits in the trailing zeros of the compared constant `0x10` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:147:13 + | +LL | let _ = x ^ 0b00_0100 < 0b01_0000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x4` contains only bits in the trailing zeros of the compared constant `0x10` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:148:13 + | +LL | let _ = x ^ 0b00_0101 < 0b01_0000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x5` contains only bits in the trailing zeros of the compared constant `0x10` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:149:13 + | +LL | let _ = x ^ 0b00_0110 < 0b01_0000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x6` contains only bits in the trailing zeros of the compared constant `0x10` -error: ineffective bit mask: `x | 2147483647` compared to `4294967295`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:150:13 + | +LL | let _ = x ^ 0b00_0111 < 0b01_0000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x7` contains only bits in the trailing zeros of the compared constant `0x10` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:151:13 + | +LL | let _ = x ^ 0b00_1000 < 0b01_0000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x8` contains only bits in the trailing zeros of the compared constant `0x10` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:152:13 + | +LL | let _ = x ^ 0b00_1001 < 0b01_0000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x9` contains only bits in the trailing zeros of the compared constant `0x10` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:153:13 + | +LL | let _ = x ^ 0b00_1111 < 0b11_0000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0xf` contains only bits in the trailing zeros of the compared constant `0x30` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:158:13 + | +LL | let _ = x ^ 0b00_1111 >= 0b11_0000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0xf` contains only bits in the trailing zeros of the compared constant `0x30` + +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:161:13 | LL | let _ = x | 0x7fff_ffff > 0xffff_ffff; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x7fffffff` contains only bits in the trailing ones of the compared constant `0xffffffff` -error: ineffective bit mask: `x | 2147483648` compared to `4294967295`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:162:13 | LL | let _ = x | 0x8000_0000 > 0xffff_ffff; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x80000000` contains only bits in the trailing ones of the compared constant `0xffffffff` -error: ineffective bit mask: `x | 4294967295` compared to `4294967295`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:163:13 | LL | let _ = x | 0xffff_ffff > 0xffff_ffff; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0xffffffff` contains only bits in the trailing ones of the compared constant `0xffffffff` -error: ineffective bit mask: `x | 2147483647` compared to `2147483648`, is the same as x compared directly +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:164:13 + | +LL | let _ = x ^ 0x7fff_ffff > 0xffff_ffff; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x7fffffff` contains only bits in the trailing ones of the compared constant `0xffffffff` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:165:13 + | +LL | let _ = x ^ 0x8000_0000 > 0xffff_ffff; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x80000000` contains only bits in the trailing ones of the compared constant `0xffffffff` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:166:13 + | +LL | let _ = x ^ 0xffff_ffff > 0xffff_ffff; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0xffffffff` contains only bits in the trailing ones of the compared constant `0xffffffff` + +error: this comparison's result is unaffected by the bitwise operation --> tests/ui/ineffective_bit_masks.rs:168:13 | LL | let _ = x | 0x7fff_ffff < 0x8000_0000; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x7fffffff` contains only bits in the trailing zeros of the compared constant `0x80000000` + +error: this comparison's result is unaffected by the bitwise operation + --> tests/ui/ineffective_bit_masks.rs:170:13 + | +LL | let _ = x ^ 0x7fff_ffff < 0x8000_0000; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `0x7fffffff` contains only bits in the trailing zeros of the compared constant `0x80000000` -error: aborting due to 42 previous errors +error: aborting due to 92 previous errors