]> git.lizzy.rs Git - rust.git/blob - tests/ui/collapsible_if.rs
Auto merge of #3603 - xfix:random-state-lint, r=phansch
[rust.git] / tests / ui / collapsible_if.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #[rustfmt::skip]
11 #[warn(clippy::collapsible_if)]
12 fn main() {
13     let x = "hello";
14     let y = "world";
15     if x == "hello" {
16         if y == "world" {
17             println!("Hello world!");
18         }
19     }
20
21     if x == "hello" || x == "world" {
22         if y == "world" || y == "hello" {
23             println!("Hello world!");
24         }
25     }
26
27     if x == "hello" && x == "world" {
28         if y == "world" || y == "hello" {
29             println!("Hello world!");
30         }
31     }
32
33     if x == "hello" || x == "world" {
34         if y == "world" && y == "hello" {
35             println!("Hello world!");
36         }
37     }
38
39     if x == "hello" && x == "world" {
40         if y == "world" && y == "hello" {
41             println!("Hello world!");
42         }
43     }
44
45     if 42 == 1337 {
46         if 'a' != 'A' {
47             println!("world!")
48         }
49     }
50
51     // Collapse `else { if .. }` to `else if ..`
52     if x == "hello" {
53         print!("Hello ");
54     } else {
55         if y == "world" {
56             println!("world!")
57         }
58     }
59
60     if x == "hello" {
61         print!("Hello ");
62     } else {
63         if let Some(42) = Some(42) {
64             println!("world!")
65         }
66     }
67
68     if x == "hello" {
69         print!("Hello ");
70     } else {
71         if y == "world" {
72             println!("world")
73         }
74         else {
75             println!("!")
76         }
77     }
78
79     if x == "hello" {
80         print!("Hello ");
81     } else {
82         if let Some(42) = Some(42) {
83             println!("world")
84         }
85         else {
86             println!("!")
87         }
88     }
89
90     if let Some(42) = Some(42) {
91         print!("Hello ");
92     } else {
93         if let Some(42) = Some(42) {
94             println!("world")
95         }
96         else {
97             println!("!")
98         }
99     }
100
101     if let Some(42) = Some(42) {
102         print!("Hello ");
103     } else {
104         if x == "hello" {
105             println!("world")
106         }
107         else {
108             println!("!")
109         }
110     }
111
112     if let Some(42) = Some(42) {
113         print!("Hello ");
114     } else {
115         if let Some(42) = Some(42) {
116             println!("world")
117         }
118         else {
119             println!("!")
120         }
121     }
122
123     // Works because any if with an else statement cannot be collapsed.
124     if x == "hello" {
125         if y == "world" {
126             println!("Hello world!");
127         }
128     } else {
129         println!("Not Hello world");
130     }
131
132     if x == "hello" {
133         if y == "world" {
134             println!("Hello world!");
135         } else {
136             println!("Hello something else");
137         }
138     }
139
140     if x == "hello" {
141         print!("Hello ");
142         if y == "world" {
143             println!("world!")
144         }
145     }
146
147     if true {
148     } else {
149         assert!(true); // assert! is just an `if`
150     }
151
152
153     // The following tests check for the fix of https://github.com/rust-lang/rust-clippy/issues/798
154     if x == "hello" {// Not collapsible
155         if y == "world" {
156             println!("Hello world!");
157         }
158     }
159
160     if x == "hello" { // Not collapsible
161         if y == "world" {
162             println!("Hello world!");
163         }
164     }
165
166     if x == "hello" {
167         // Not collapsible
168         if y == "world" {
169             println!("Hello world!");
170         }
171     }
172
173     if x == "hello" {
174         if y == "world" { // Collapsible
175             println!("Hello world!");
176         }
177     }
178
179     if x == "hello" {
180         print!("Hello ");
181     } else {
182         // Not collapsible
183         if y == "world" {
184             println!("world!")
185         }
186     }
187
188     if x == "hello" {
189         print!("Hello ");
190     } else {
191         // Not collapsible
192         if let Some(42) = Some(42) {
193             println!("world!")
194         }
195     }
196
197     if x == "hello" {
198         /* Not collapsible */
199         if y == "world" {
200             println!("Hello world!");
201         }
202     }
203
204     if x == "hello" { /* Not collapsible */
205         if y == "world" {
206             println!("Hello world!");
207         }
208     }
209 }