diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /src/tools/clippy/tests/ui/bit_masks.rs | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/tools/clippy/tests/ui/bit_masks.rs')
-rw-r--r-- | src/tools/clippy/tests/ui/bit_masks.rs | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/tools/clippy/tests/ui/bit_masks.rs b/src/tools/clippy/tests/ui/bit_masks.rs new file mode 100644 index 000000000..cfb493fb5 --- /dev/null +++ b/src/tools/clippy/tests/ui/bit_masks.rs @@ -0,0 +1,63 @@ +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; + x & 1 == 1; //ok, distinguishes bit 0 + x & 1 == 0; //ok, compared with zero + x & 2 == 1; + 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; + + x & 1 > 1; + 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; + 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; + x | EVEN_MORE_REDIRECTION < 7; + + 0 & x == 0; + 1 | x > 1; + + // and should now also match uncommon usage + 1 < 2 | x; + 2 == 3 | x; + 1 == x & 2; + + 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; + x | 1 < 4; + x | 1 <= 3; + x | 1 >= 8; + + 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; +} |