]> git.lizzy.rs Git - rust.git/blob - tests/ui/collapsible_if.rs
Auto merge of #4314 - chansuke:add-negation-to-is_empty, r=flip1995
[rust.git] / tests / ui / collapsible_if.rs
1 // run-rustfix
2 #![allow(clippy::cognitive_complexity, clippy::assertions_on_constants)]
3
4 #[rustfmt::skip]
5 #[warn(clippy::collapsible_if)]
6 fn main() {
7     let x = "hello";
8     let y = "world";
9     if x == "hello" {
10         if y == "world" {
11             println!("Hello world!");
12         }
13     }
14
15     if x == "hello" || x == "world" {
16         if y == "world" || y == "hello" {
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 42 == 1337 {
40         if 'a' != 'A' {
41             println!("world!")
42         }
43     }
44
45     // Collapse `else { if .. }` to `else if ..`
46     if x == "hello" {
47         print!("Hello ");
48     } else {
49         if y == "world" {
50             println!("world!")
51         }
52     }
53
54     if x == "hello" {
55         print!("Hello ");
56     } else {
57         if let Some(42) = Some(42) {
58             println!("world!")
59         }
60     }
61
62     if x == "hello" {
63         print!("Hello ");
64     } else {
65         if y == "world" {
66             println!("world")
67         }
68         else {
69             println!("!")
70         }
71     }
72
73     if x == "hello" {
74         print!("Hello ");
75     } else {
76         if let Some(42) = Some(42) {
77             println!("world")
78         }
79         else {
80             println!("!")
81         }
82     }
83
84     if let Some(42) = Some(42) {
85         print!("Hello ");
86     } else {
87         if let Some(42) = Some(42) {
88             println!("world")
89         }
90         else {
91             println!("!")
92         }
93     }
94
95     if let Some(42) = Some(42) {
96         print!("Hello ");
97     } else {
98         if x == "hello" {
99             println!("world")
100         }
101         else {
102             println!("!")
103         }
104     }
105
106     if let Some(42) = Some(42) {
107         print!("Hello ");
108     } else {
109         if let Some(42) = Some(42) {
110             println!("world")
111         }
112         else {
113             println!("!")
114         }
115     }
116
117     // Works because any if with an else statement cannot be collapsed.
118     if x == "hello" {
119         if y == "world" {
120             println!("Hello world!");
121         }
122     } else {
123         println!("Not Hello world");
124     }
125
126     if x == "hello" {
127         if y == "world" {
128             println!("Hello world!");
129         } else {
130             println!("Hello something else");
131         }
132     }
133
134     if x == "hello" {
135         print!("Hello ");
136         if y == "world" {
137             println!("world!")
138         }
139     }
140
141     if true {
142     } else {
143         assert!(true); // assert! is just an `if`
144     }
145
146
147     // The following tests check for the fix of https://github.com/rust-lang/rust-clippy/issues/798
148     if x == "hello" {// Not collapsible
149         if y == "world" {
150             println!("Hello world!");
151         }
152     }
153
154     if x == "hello" { // Not collapsible
155         if y == "world" {
156             println!("Hello world!");
157         }
158     }
159
160     if x == "hello" {
161         // Not collapsible
162         if y == "world" {
163             println!("Hello world!");
164         }
165     }
166
167     if x == "hello" {
168         if y == "world" { // Collapsible
169             println!("Hello world!");
170         }
171     }
172
173     if x == "hello" {
174         print!("Hello ");
175     } else {
176         // Not collapsible
177         if y == "world" {
178             println!("world!")
179         }
180     }
181
182     if x == "hello" {
183         print!("Hello ");
184     } else {
185         // Not collapsible
186         if let Some(42) = Some(42) {
187             println!("world!")
188         }
189     }
190
191     if x == "hello" {
192         /* Not collapsible */
193         if y == "world" {
194             println!("Hello world!");
195         }
196     }
197
198     if x == "hello" { /* Not collapsible */
199         if y == "world" {
200             println!("Hello world!");
201         }
202     }
203
204     // Test behavior wrt. `let_chains`.
205     // None of the cases below should be collapsed.
206     fn truth() -> bool { true }
207
208     // Prefix:
209     if let 0 = 1 {
210         if truth() {}
211     }
212
213     // Suffix:
214     if truth() {
215         if let 0 = 1 {}
216     }
217
218     // Midfix:
219     if truth() {
220         if let 0 = 1 {
221             if truth() {}
222         }
223     }
224 }