about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/manual_is_power_of_two.fixed
blob: 8a1ab785dfbfd353e5708977f90760c85abb67a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#![warn(clippy::manual_is_power_of_two)]
#![allow(clippy::precedence)]

macro_rules! binop {
    ($a: expr, equal, $b: expr) => {
        $a == $b
    };
    ($a: expr, and, $b: expr) => {
        $a & $b
    };
    ($a: expr, minus, $b: expr) => {
        $a - $b
    };
}

fn main() {
    let a = 16_u64;

    let _ = a.is_power_of_two();
    //~^ manual_is_power_of_two
    let _ = a.is_power_of_two();
    //~^ manual_is_power_of_two
    let _ = a.is_power_of_two();
    //~^ manual_is_power_of_two

    // Test different orders of expression
    let _ = a.is_power_of_two();
    //~^ manual_is_power_of_two
    let _ = a.is_power_of_two();
    //~^ manual_is_power_of_two
    let _ = a.is_power_of_two();
    //~^ manual_is_power_of_two
    let _ = a.is_power_of_two();
    //~^ manual_is_power_of_two

    let b = 4_i64;

    // is_power_of_two only works for unsigned integers
    let _ = b.count_ones() == 1;
    let _ = b & (b - 1) == 0;

    let i: i32 = 3;
    let _ = (i as u32).is_power_of_two();
    //~^ manual_is_power_of_two

    let _ = binop!(a.count_ones(), equal, 1);
    let _ = binop!(a, and, a - 1) == 0;
    let _ = a & binop!(a, minus, 1) == 0;
}

#[clippy::msrv = "1.31"]
const fn low_msrv(a: u32) -> bool {
    a & (a - 1) == 0
}

#[clippy::msrv = "1.32"]
const fn high_msrv(a: u32) -> bool {
    a.is_power_of_two()
    //~^ manual_is_power_of_two
}