]> git.lizzy.rs Git - rust.git/blob - tests/ui/bit_masks.rs
Auto merge of #3635 - matthiaskrgr:revert_random_state_3603, r=xfix
[rust.git] / tests / ui / bit_masks.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 const THREE_BITS: i64 = 7;
11 const EVEN_MORE_REDIRECTION: i64 = THREE_BITS;
12
13 #[warn(clippy::bad_bit_mask)]
14 #[allow(
15     clippy::ineffective_bit_mask,
16     clippy::identity_op,
17     clippy::no_effect,
18     clippy::unnecessary_operation
19 )]
20 fn main() {
21     let x = 5;
22
23     x & 0 == 0;
24     x & 1 == 1; //ok, distinguishes bit 0
25     x & 1 == 0; //ok, compared with zero
26     x & 2 == 1;
27     x | 0 == 0; //ok, equals x == 0 (maybe warn?)
28     x | 1 == 3; //ok, equals x == 2 || x == 3
29     x | 3 == 3; //ok, equals x <= 3
30     x | 3 == 2;
31
32     x & 1 > 1;
33     x & 2 > 1; // ok, distinguishes x & 2 == 2 from x & 2 == 0
34     x & 2 < 1; // ok, distinguishes x & 2 == 2 from x & 2 == 0
35     x | 1 > 1; // ok (if a bit silly), equals x > 1
36     x | 2 > 1;
37     x | 2 <= 2; // ok (if a bit silly), equals x <= 2
38
39     x & 192 == 128; // ok, tests for bit 7 and not bit 6
40     x & 0xffc0 == 0xfe80; // ok
41
42     // this also now works with constants
43     x & THREE_BITS == 8;
44     x | EVEN_MORE_REDIRECTION < 7;
45
46     0 & x == 0;
47     1 | x > 1;
48
49     // and should now also match uncommon usage
50     1 < 2 | x;
51     2 == 3 | x;
52     1 == x & 2;
53
54     x | 1 > 2; // no error, because we allowed ineffective bit masks
55     ineffective();
56 }
57
58 #[warn(clippy::ineffective_bit_mask)]
59 #[allow(clippy::bad_bit_mask, clippy::no_effect, clippy::unnecessary_operation)]
60 fn ineffective() {
61     let x = 5;
62
63     x | 1 > 3;
64     x | 1 < 4;
65     x | 1 <= 3;
66     x | 1 >= 8;
67
68     x | 1 > 2; // not an error (yet), better written as x >= 2
69     x | 1 >= 7; // not an error (yet), better written as x >= 6
70     x | 3 > 4; // not an error (yet), better written as x >= 4
71     x | 4 <= 19;
72 }