]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/comparison_chain.rs
Auto merge of #83491 - jyn514:remove-pretty, r=pnkfelix
[rust.git] / src / tools / clippy / tests / ui / comparison_chain.rs
1 #![allow(dead_code)]
2 #![warn(clippy::comparison_chain)]
3
4 fn a() {}
5 fn b() {}
6 fn c() {}
7
8 fn f(x: u8, y: u8, z: u8) {
9     // Ignored: Only one branch
10     if x > y {
11         a()
12     }
13
14     if x > y {
15         a()
16     } else if x < y {
17         b()
18     }
19
20     // Ignored: Only one explicit conditional
21     if x > y {
22         a()
23     } else {
24         b()
25     }
26
27     if x > y {
28         a()
29     } else if x < y {
30         b()
31     } else {
32         c()
33     }
34
35     if x > y {
36         a()
37     } else if y > x {
38         b()
39     } else {
40         c()
41     }
42
43     if x > 1 {
44         a()
45     } else if x < 1 {
46         b()
47     } else if x == 1 {
48         c()
49     }
50
51     // Ignored: Binop args are not equivalent
52     if x > 1 {
53         a()
54     } else if y > 1 {
55         b()
56     } else {
57         c()
58     }
59
60     // Ignored: Binop args are not equivalent
61     if x > y {
62         a()
63     } else if x > z {
64         b()
65     } else if y > z {
66         c()
67     }
68
69     // Ignored: Not binary comparisons
70     if true {
71         a()
72     } else if false {
73         b()
74     } else {
75         c()
76     }
77 }
78
79 #[allow(clippy::float_cmp)]
80 fn g(x: f64, y: f64, z: f64) {
81     // Ignored: f64 doesn't implement Ord
82     if x > y {
83         a()
84     } else if x < y {
85         b()
86     }
87
88     // Ignored: f64 doesn't implement Ord
89     if x > y {
90         a()
91     } else if x < y {
92         b()
93     } else {
94         c()
95     }
96
97     // Ignored: f64 doesn't implement Ord
98     if x > y {
99         a()
100     } else if y > x {
101         b()
102     } else {
103         c()
104     }
105
106     // Ignored: f64 doesn't implement Ord
107     if x > 1.0 {
108         a()
109     } else if x < 1.0 {
110         b()
111     } else if x == 1.0 {
112         c()
113     }
114 }
115
116 fn h<T: Ord>(x: T, y: T, z: T) {
117     if x > y {
118         a()
119     } else if x < y {
120         b()
121     }
122
123     if x > y {
124         a()
125     } else if x < y {
126         b()
127     } else {
128         c()
129     }
130
131     if x > y {
132         a()
133     } else if y > x {
134         b()
135     } else {
136         c()
137     }
138 }
139
140 // The following uses should be ignored
141 mod issue_5212 {
142     use super::{a, b, c};
143     fn foo() -> u8 {
144         21
145     }
146
147     fn same_operation_equals() {
148         // operands are fixed
149
150         if foo() == 42 {
151             a()
152         } else if foo() == 42 {
153             b()
154         }
155
156         if foo() == 42 {
157             a()
158         } else if foo() == 42 {
159             b()
160         } else {
161             c()
162         }
163
164         // operands are transposed
165
166         if foo() == 42 {
167             a()
168         } else if 42 == foo() {
169             b()
170         }
171     }
172
173     fn same_operation_not_equals() {
174         // operands are fixed
175
176         if foo() > 42 {
177             a()
178         } else if foo() > 42 {
179             b()
180         }
181
182         if foo() > 42 {
183             a()
184         } else if foo() > 42 {
185             b()
186         } else {
187             c()
188         }
189
190         if foo() < 42 {
191             a()
192         } else if foo() < 42 {
193             b()
194         }
195
196         if foo() < 42 {
197             a()
198         } else if foo() < 42 {
199             b()
200         } else {
201             c()
202         }
203     }
204 }
205
206 enum Sign {
207     Negative,
208     Positive,
209     Zero,
210 }
211
212 impl Sign {
213     const fn sign_i8(n: i8) -> Self {
214         if n == 0 {
215             Sign::Zero
216         } else if n > 0 {
217             Sign::Positive
218         } else {
219             Sign::Negative
220         }
221     }
222 }
223
224 const fn sign_i8(n: i8) -> Sign {
225     if n == 0 {
226         Sign::Zero
227     } else if n > 0 {
228         Sign::Positive
229     } else {
230         Sign::Negative
231     }
232 }
233
234 fn main() {}