]> git.lizzy.rs Git - rust.git/blob - tests/ui/bit_masks.rs
Merge pull request #2984 from flip1995/single_char_pattern
[rust.git] / tests / ui / bit_masks.rs
1
2
3
4 const THREE_BITS : i64 = 7;
5 const EVEN_MORE_REDIRECTION : i64 = THREE_BITS;
6
7 #[warn(bad_bit_mask)]
8 #[allow(ineffective_bit_mask, identity_op, no_effect, unnecessary_operation)]
9 fn main() {
10     let x = 5;
11
12     x & 0 == 0;
13     x & 1 == 1; //ok, distinguishes bit 0
14     x & 1 == 0; //ok, compared with zero
15     x & 2 == 1;
16     x | 0 == 0; //ok, equals x == 0 (maybe warn?)
17     x | 1 == 3; //ok, equals x == 2 || x == 3
18     x | 3 == 3; //ok, equals x <= 3
19     x | 3 == 2;
20
21     x & 1 > 1;
22     x & 2 > 1; // ok, distinguishes x & 2 == 2 from x & 2 == 0
23     x & 2 < 1; // ok, distinguishes x & 2 == 2 from x & 2 == 0
24     x | 1 > 1; // ok (if a bit silly), equals x > 1
25     x | 2 > 1;
26     x | 2 <= 2; // ok (if a bit silly), equals x <= 2
27
28     x & 192 == 128; // ok, tests for bit 7 and not bit 6
29     x & 0xffc0 == 0xfe80; // ok
30
31     // this also now works with constants
32     x & THREE_BITS == 8;
33     x | EVEN_MORE_REDIRECTION < 7;
34
35     0 & x == 0;
36     1 | x > 1;
37
38     // and should now also match uncommon usage
39     1 < 2 | x;
40     2 == 3 | x;
41     1 == x & 2;
42
43     x | 1 > 2; // no error, because we allowed ineffective bit masks
44     ineffective();
45 }
46
47 #[warn(ineffective_bit_mask)]
48 #[allow(bad_bit_mask, no_effect, unnecessary_operation)]
49 fn ineffective() {
50     let x = 5;
51
52     x | 1 > 3;
53     x | 1 < 4;
54     x | 1 <= 3;
55     x | 1 >= 8;
56
57     x | 1 > 2; // not an error (yet), better written as x >= 2
58     x | 1 >= 7; // not an error (yet), better written as x >= 6
59     x | 3 > 4; // not an error (yet), better written as x >= 4
60     x | 4 <= 19;
61 }