]> git.lizzy.rs Git - rust.git/blob - tests/ui/collapsible_if.rs
Merge pull request #3332 from lukasstevens/fix798
[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
11
12
13
14 #[warn(clippy::collapsible_if)]
15 fn main() {
16     let x = "hello";
17     let y = "world";
18     if x == "hello" {
19         if y == "world" {
20             println!("Hello world!");
21         }
22     }
23
24     if x == "hello" || x == "world" {
25         if y == "world" || y == "hello" {
26             println!("Hello world!");
27         }
28     }
29
30     if x == "hello" && x == "world" {
31         if y == "world" || y == "hello" {
32             println!("Hello world!");
33         }
34     }
35
36     if x == "hello" || x == "world" {
37         if y == "world" && y == "hello" {
38             println!("Hello world!");
39         }
40     }
41
42     if x == "hello" && x == "world" {
43         if y == "world" && y == "hello" {
44             println!("Hello world!");
45         }
46     }
47
48     if 42 == 1337 {
49         if 'a' != 'A' {
50             println!("world!")
51         }
52     }
53
54     // Collapse `else { if .. }` to `else if ..`
55     if x == "hello" {
56         print!("Hello ");
57     } else {
58         if y == "world" {
59             println!("world!")
60         }
61     }
62
63     if x == "hello" {
64         print!("Hello ");
65     } else {
66         if let Some(42) = Some(42) {
67             println!("world!")
68         }
69     }
70
71     if x == "hello" {
72         print!("Hello ");
73     } else {
74         if y == "world" {
75             println!("world")
76         }
77         else {
78             println!("!")
79         }
80     }
81
82     if x == "hello" {
83         print!("Hello ");
84     } else {
85         if let Some(42) = Some(42) {
86             println!("world")
87         }
88         else {
89             println!("!")
90         }
91     }
92
93     if let Some(42) = Some(42) {
94         print!("Hello ");
95     } else {
96         if let Some(42) = Some(42) {
97             println!("world")
98         }
99         else {
100             println!("!")
101         }
102     }
103
104     if let Some(42) = Some(42) {
105         print!("Hello ");
106     } else {
107         if x == "hello" {
108             println!("world")
109         }
110         else {
111             println!("!")
112         }
113     }
114
115     if let Some(42) = Some(42) {
116         print!("Hello ");
117     } else {
118         if let Some(42) = Some(42) {
119             println!("world")
120         }
121         else {
122             println!("!")
123         }
124     }
125
126     // Works because any if with an else statement cannot be collapsed.
127     if x == "hello" {
128         if y == "world" {
129             println!("Hello world!");
130         }
131     } else {
132         println!("Not Hello world");
133     }
134
135     if x == "hello" {
136         if y == "world" {
137             println!("Hello world!");
138         } else {
139             println!("Hello something else");
140         }
141     }
142
143     if x == "hello" {
144         print!("Hello ");
145         if y == "world" {
146             println!("world!")
147         }
148     }
149
150     if true {
151     } else {
152         assert!(true); // assert! is just an `if`
153     }
154
155
156     // The following tests check for the fix of https://github.com/rust-lang-nursery/rust-clippy/issues/798
157     if x == "hello" {// Not collapsible
158         if y == "world" {
159             println!("Hello world!");
160         }
161     }
162
163     if x == "hello" { // Not collapsible
164         if y == "world" {
165             println!("Hello world!");
166         }
167     }
168
169     if x == "hello" {
170         // Not collapsible
171         if y == "world" {
172             println!("Hello world!");
173         }
174     }
175
176     if x == "hello" {
177         if y == "world" { // Collapsible
178             println!("Hello world!");
179         }
180     }
181
182     if x == "hello" {
183         print!("Hello ");
184     } else {
185         // Not collapsible
186         if y == "world" {
187             println!("world!")
188         }
189     }
190
191     if x == "hello" {
192         print!("Hello ");
193     } else {
194         // Not collapsible
195         if let Some(42) = Some(42) {
196             println!("world!")
197         }
198     }
199
200     if x == "hello" {
201         /* Not collapsible */
202         if y == "world" {
203             println!("Hello world!");
204         }
205     }
206
207     if x == "hello" { /* Not collapsible */
208         if y == "world" {
209             println!("Hello world!");
210         }
211     }
212 }