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