about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/comparison_chain.rs
blob: 9c2128469de9ded7afeaf9571882f4e3be171383 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#![allow(dead_code)]
#![warn(clippy::comparison_chain)]

fn a() {}
fn b() {}
fn c() {}

fn f(x: u8, y: u8, z: u8) {
    // Ignored: Only one branch
    if x > y {
        a()
    }

    if x > y {
        a()
    } else if x < y {
        b()
    }

    // Ignored: Only one explicit conditional
    if x > y {
        a()
    } else {
        b()
    }

    if x > y {
        a()
    } else if x < y {
        b()
    } else {
        c()
    }

    if x > y {
        a()
    } else if y > x {
        b()
    } else {
        c()
    }

    if x > 1 {
        a()
    } else if x < 1 {
        b()
    } else if x == 1 {
        c()
    }

    // Ignored: Binop args are not equivalent
    if x > 1 {
        a()
    } else if y > 1 {
        b()
    } else {
        c()
    }

    // Ignored: Binop args are not equivalent
    if x > y {
        a()
    } else if x > z {
        b()
    } else if y > z {
        c()
    }

    // Ignored: Not binary comparisons
    if true {
        a()
    } else if false {
        b()
    } else {
        c()
    }
}

#[allow(clippy::float_cmp)]
fn g(x: f64, y: f64, z: f64) {
    // Ignored: f64 doesn't implement Ord
    if x > y {
        a()
    } else if x < y {
        b()
    }

    // Ignored: f64 doesn't implement Ord
    if x > y {
        a()
    } else if x < y {
        b()
    } else {
        c()
    }

    // Ignored: f64 doesn't implement Ord
    if x > y {
        a()
    } else if y > x {
        b()
    } else {
        c()
    }

    // Ignored: f64 doesn't implement Ord
    if x > 1.0 {
        a()
    } else if x < 1.0 {
        b()
    } else if x == 1.0 {
        c()
    }
}

fn h<T: Ord>(x: T, y: T, z: T) {
    if x > y {
        a()
    } else if x < y {
        b()
    }

    if x > y {
        a()
    } else if x < y {
        b()
    } else {
        c()
    }

    if x > y {
        a()
    } else if y > x {
        b()
    } else {
        c()
    }
}

fn main() {}