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