Skip to content

Commit c0083e2

Browse files
committedJan 13, 2019
Add run-rustfix to collapsible_if test
1 parent 91ef4e3 commit c0083e2

File tree

3 files changed

+192
-14
lines changed

3 files changed

+192
-14
lines changed
 

‎tests/ui/collapsible_if.fixed

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
// run-rustfix
2+
#![allow(clippy::cyclomatic_complexity)]
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+
}

‎tests/ui/collapsible_if.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// run-rustfix
2+
#![allow(clippy::cyclomatic_complexity)]
3+
14
#[rustfmt::skip]
25
#[warn(clippy::collapsible_if)]
36
fn main() {

‎tests/ui/collapsible_if.stderr

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this if statement can be collapsed
2-
--> $DIR/collapsible_if.rs:6:5
2+
--> $DIR/collapsible_if.rs:9:5
33
|
44
LL | / if x == "hello" {
55
LL | | if y == "world" {
@@ -17,7 +17,7 @@ LL | }
1717
|
1818

1919
error: this if statement can be collapsed
20-
--> $DIR/collapsible_if.rs:12:5
20+
--> $DIR/collapsible_if.rs:15:5
2121
|
2222
LL | / if x == "hello" || x == "world" {
2323
LL | | if y == "world" || y == "hello" {
@@ -33,7 +33,7 @@ LL | }
3333
|
3434

3535
error: this if statement can be collapsed
36-
--> $DIR/collapsible_if.rs:18:5
36+
--> $DIR/collapsible_if.rs:21:5
3737
|
3838
LL | / if x == "hello" && x == "world" {
3939
LL | | if y == "world" || y == "hello" {
@@ -49,7 +49,7 @@ LL | }
4949
|
5050

5151
error: this if statement can be collapsed
52-
--> $DIR/collapsible_if.rs:24:5
52+
--> $DIR/collapsible_if.rs:27:5
5353
|
5454
LL | / if x == "hello" || x == "world" {
5555
LL | | if y == "world" && y == "hello" {
@@ -65,7 +65,7 @@ LL | }
6565
|
6666

6767
error: this if statement can be collapsed
68-
--> $DIR/collapsible_if.rs:30:5
68+
--> $DIR/collapsible_if.rs:33:5
6969
|
7070
LL | / if x == "hello" && x == "world" {
7171
LL | | if y == "world" && y == "hello" {
@@ -81,7 +81,7 @@ LL | }
8181
|
8282

8383
error: this if statement can be collapsed
84-
--> $DIR/collapsible_if.rs:36:5
84+
--> $DIR/collapsible_if.rs:39:5
8585
|
8686
LL | / if 42 == 1337 {
8787
LL | | if 'a' != 'A' {
@@ -97,7 +97,7 @@ LL | }
9797
|
9898

9999
error: this `else { if .. }` block can be collapsed
100-
--> $DIR/collapsible_if.rs:45:12
100+
--> $DIR/collapsible_if.rs:48:12
101101
|
102102
LL | } else {
103103
| ____________^
@@ -114,7 +114,7 @@ LL | }
114114
|
115115

116116
error: this `else { if .. }` block can be collapsed
117-
--> $DIR/collapsible_if.rs:53:12
117+
--> $DIR/collapsible_if.rs:56:12
118118
|
119119
LL | } else {
120120
| ____________^
@@ -131,7 +131,7 @@ LL | }
131131
|
132132

133133
error: this `else { if .. }` block can be collapsed
134-
--> $DIR/collapsible_if.rs:61:12
134+
--> $DIR/collapsible_if.rs:64:12
135135
|
136136
LL | } else {
137137
| ____________^
@@ -153,7 +153,7 @@ LL | }
153153
|
154154

155155
error: this `else { if .. }` block can be collapsed
156-
--> $DIR/collapsible_if.rs:72:12
156+
--> $DIR/collapsible_if.rs:75:12
157157
|
158158
LL | } else {
159159
| ____________^
@@ -175,7 +175,7 @@ LL | }
175175
|
176176

177177
error: this `else { if .. }` block can be collapsed
178-
--> $DIR/collapsible_if.rs:83:12
178+
--> $DIR/collapsible_if.rs:86:12
179179
|
180180
LL | } else {
181181
| ____________^
@@ -197,7 +197,7 @@ LL | }
197197
|
198198

199199
error: this `else { if .. }` block can be collapsed
200-
--> $DIR/collapsible_if.rs:94:12
200+
--> $DIR/collapsible_if.rs:97:12
201201
|
202202
LL | } else {
203203
| ____________^
@@ -219,7 +219,7 @@ LL | }
219219
|
220220

221221
error: this `else { if .. }` block can be collapsed
222-
--> $DIR/collapsible_if.rs:105:12
222+
--> $DIR/collapsible_if.rs:108:12
223223
|
224224
LL | } else {
225225
| ____________^
@@ -241,7 +241,7 @@ LL | }
241241
|
242242

243243
error: this if statement can be collapsed
244-
--> $DIR/collapsible_if.rs:164:5
244+
--> $DIR/collapsible_if.rs:167:5
245245
|
246246
LL | / if x == "hello" {
247247
LL | | if y == "world" { // Collapsible

0 commit comments

Comments
 (0)
Please sign in to comment.