]>
Commit | Line | Data |
---|---|---|
f20569fa XL |
1 | // run-rustfix |
2 | #![allow(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 | // Works because any if with an else statement cannot be collapsed. | |
34 | if x == "hello" { | |
35 | if y == "world" { | |
36 | println!("Hello world!"); | |
37 | } | |
38 | } else { | |
39 | println!("Not Hello world"); | |
40 | } | |
41 | ||
42 | if x == "hello" { | |
43 | if y == "world" { | |
44 | println!("Hello world!"); | |
45 | } else { | |
46 | println!("Hello something else"); | |
47 | } | |
48 | } | |
49 | ||
50 | if x == "hello" { | |
51 | print!("Hello "); | |
52 | if y == "world" { | |
53 | println!("world!") | |
54 | } | |
55 | } | |
56 | ||
57 | if true { | |
58 | } else { | |
59 | assert!(true); // assert! is just an `if` | |
60 | } | |
61 | ||
62 | ||
63 | // The following tests check for the fix of https://github.com/rust-lang/rust-clippy/issues/798 | |
64 | if x == "hello" {// Not collapsible | |
65 | if y == "world" { | |
66 | println!("Hello world!"); | |
67 | } | |
68 | } | |
69 | ||
70 | if x == "hello" { // Not collapsible | |
71 | if y == "world" { | |
72 | println!("Hello world!"); | |
73 | } | |
74 | } | |
75 | ||
76 | if x == "hello" { | |
77 | // Not collapsible | |
78 | if y == "world" { | |
79 | println!("Hello world!"); | |
80 | } | |
81 | } | |
82 | ||
83 | if x == "hello" && y == "world" { // Collapsible | |
84 | println!("Hello world!"); | |
85 | } | |
86 | ||
87 | if x == "hello" { | |
88 | print!("Hello "); | |
89 | } else { | |
90 | // Not collapsible | |
91 | if y == "world" { | |
92 | println!("world!") | |
93 | } | |
94 | } | |
95 | ||
96 | if x == "hello" { | |
97 | print!("Hello "); | |
98 | } else { | |
99 | // Not collapsible | |
100 | if let Some(42) = Some(42) { | |
101 | println!("world!") | |
102 | } | |
103 | } | |
104 | ||
105 | if x == "hello" { | |
106 | /* Not collapsible */ | |
107 | if y == "world" { | |
108 | println!("Hello world!"); | |
109 | } | |
110 | } | |
111 | ||
112 | if x == "hello" { /* Not collapsible */ | |
113 | if y == "world" { | |
114 | println!("Hello world!"); | |
115 | } | |
116 | } | |
117 | ||
118 | // Test behavior wrt. `let_chains`. | |
119 | // None of the cases below should be collapsed. | |
120 | fn truth() -> bool { true } | |
121 | ||
122 | // Prefix: | |
123 | if let 0 = 1 { | |
124 | if truth() {} | |
125 | } | |
126 | ||
127 | // Suffix: | |
128 | if truth() { | |
129 | if let 0 = 1 {} | |
130 | } | |
131 | ||
132 | // Midfix: | |
133 | if truth() { | |
134 | if let 0 = 1 { | |
135 | if truth() {} | |
136 | } | |
137 | } | |
138 | ||
139 | // Fix #5962 | |
140 | if matches!(true, true) && matches!(true, true) {} | |
141 | ||
142 | if true { | |
143 | #[cfg(not(teehee))] | |
144 | if true { | |
145 | println!("Hello world!"); | |
146 | } | |
147 | } | |
148 | } |