]> git.lizzy.rs Git - rust.git/blob - tests/ui/if_same_then_else.rs
Auto merge of #4314 - chansuke:add-negation-to-is_empty, r=flip1995
[rust.git] / tests / ui / if_same_then_else.rs
1 #![warn(clippy::if_same_then_else)]
2 #![allow(
3     clippy::blacklisted_name,
4     clippy::collapsible_if,
5     clippy::cognitive_complexity,
6     clippy::eq_op,
7     clippy::needless_return,
8     clippy::never_loop,
9     clippy::no_effect,
10     clippy::zero_divided_by_zero,
11     clippy::unused_unit
12 )]
13
14 struct Foo {
15     bar: u8,
16 }
17
18 fn foo() -> bool {
19     unimplemented!()
20 }
21
22 fn if_same_then_else() -> Result<&'static str, ()> {
23     if true {
24         Foo { bar: 42 };
25         0..10;
26         ..;
27         0..;
28         ..10;
29         0..=10;
30         foo();
31     } else {
32         //~ ERROR same body as `if` block
33         Foo { bar: 42 };
34         0..10;
35         ..;
36         0..;
37         ..10;
38         0..=10;
39         foo();
40     }
41
42     if true {
43         Foo { bar: 42 };
44     } else {
45         Foo { bar: 43 };
46     }
47
48     if true {
49         ();
50     } else {
51         ()
52     }
53
54     if true {
55         0..10;
56     } else {
57         0..=10;
58     }
59
60     if true {
61         foo();
62         foo();
63     } else {
64         foo();
65     }
66
67     let _ = if true {
68         0.0
69     } else {
70         //~ ERROR same body as `if` block
71         0.0
72     };
73
74     let _ = if true {
75         -0.0
76     } else {
77         //~ ERROR same body as `if` block
78         -0.0
79     };
80
81     let _ = if true { 0.0 } else { -0.0 };
82
83     // Different NaNs
84     let _ = if true { 0.0 / 0.0 } else { std::f32::NAN };
85
86     if true {
87         foo();
88     }
89
90     let _ = if true {
91         42
92     } else {
93         //~ ERROR same body as `if` block
94         42
95     };
96
97     if true {
98         for _ in &[42] {
99             let foo: &Option<_> = &Some::<u8>(42);
100             if true {
101                 break;
102             } else {
103                 continue;
104             }
105         }
106     } else {
107         //~ ERROR same body as `if` block
108         for _ in &[42] {
109             let foo: &Option<_> = &Some::<u8>(42);
110             if true {
111                 break;
112             } else {
113                 continue;
114             }
115         }
116     }
117
118     if true {
119         let bar = if true { 42 } else { 43 };
120
121         while foo() {
122             break;
123         }
124         bar + 1;
125     } else {
126         //~ ERROR same body as `if` block
127         let bar = if true { 42 } else { 43 };
128
129         while foo() {
130             break;
131         }
132         bar + 1;
133     }
134
135     if true {
136         let _ = match 42 {
137             42 => 1,
138             a if a > 0 => 2,
139             10..=15 => 3,
140             _ => 4,
141         };
142     } else if false {
143         foo();
144     } else if foo() {
145         let _ = match 42 {
146             42 => 1,
147             a if a > 0 => 2,
148             10..=15 => 3,
149             _ => 4,
150         };
151     }
152
153     if true {
154         if let Some(a) = Some(42) {}
155     } else {
156         //~ ERROR same body as `if` block
157         if let Some(a) = Some(42) {}
158     }
159
160     if true {
161         if let (1, .., 3) = (1, 2, 3) {}
162     } else {
163         //~ ERROR same body as `if` block
164         if let (1, .., 3) = (1, 2, 3) {}
165     }
166
167     if true {
168         if let (1, .., 3) = (1, 2, 3) {}
169     } else {
170         if let (.., 3) = (1, 2, 3) {}
171     }
172
173     if true {
174         if let (1, .., 3) = (1, 2, 3) {}
175     } else {
176         if let (.., 4) = (1, 2, 3) {}
177     }
178
179     if true {
180         if let (1, .., 3) = (1, 2, 3) {}
181     } else {
182         if let (.., 1, 3) = (1, 2, 3) {}
183     }
184
185     if true {
186         if let Some(42) = None {}
187     } else {
188         if let Option::Some(42) = None {}
189     }
190
191     if true {
192         if let Some(42) = None::<u8> {}
193     } else {
194         if let Some(42) = None {}
195     }
196
197     if true {
198         if let Some(42) = None::<u8> {}
199     } else {
200         if let Some(42) = None::<u32> {}
201     }
202
203     if true {
204         if let Some(a) = Some(42) {}
205     } else {
206         if let Some(a) = Some(43) {}
207     }
208
209     // Same NaNs
210     let _ = if true {
211         std::f32::NAN
212     } else {
213         //~ ERROR same body as `if` block
214         std::f32::NAN
215     };
216
217     if true {
218         r#try!(Ok("foo"));
219     } else {
220         //~ ERROR same body as `if` block
221         r#try!(Ok("foo"));
222     }
223
224     if true {
225         let foo = "";
226         return Ok(&foo[0..]);
227     } else if false {
228         let foo = "bar";
229         return Ok(&foo[0..]);
230     } else {
231         let foo = "";
232         return Ok(&foo[0..]);
233     }
234
235     if true {
236         let foo = "";
237         return Ok(&foo[0..]);
238     } else if false {
239         let foo = "bar";
240         return Ok(&foo[0..]);
241     } else if true {
242         let foo = "";
243         return Ok(&foo[0..]);
244     } else {
245         let foo = "";
246         return Ok(&foo[0..]);
247     }
248
249     // False positive `if_same_then_else`: `let (x, y)` vs. `let (y, x)`; see issue #3559.
250     if true {
251         let foo = "";
252         let (x, y) = (1, 2);
253         return Ok(&foo[x..y]);
254     } else {
255         let foo = "";
256         let (y, x) = (1, 2);
257         return Ok(&foo[x..y]);
258     }
259 }
260
261 // Issue #2423. This was causing an ICE.
262 fn func() {
263     if true {
264         f(&[0; 62]);
265         f(&[0; 4]);
266         f(&[0; 3]);
267     } else {
268         f(&[0; 62]);
269         f(&[0; 6]);
270         f(&[0; 6]);
271     }
272 }
273
274 fn f(val: &[u8]) {}
275
276 fn main() {}