Skip to content

Commit 10cd166

Browse files
committed
Update block_in_if_condition test files
1 parent 19f08c2 commit 10cd166

File tree

5 files changed

+163
-84
lines changed

5 files changed

+163
-84
lines changed

tests/ui/block_in_if_condition.fixed

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// run-rustfix
2+
#![warn(clippy::block_in_if_condition_expr)]
3+
#![warn(clippy::block_in_if_condition_stmt)]
4+
#![allow(unused, clippy::let_and_return)]
5+
#![warn(clippy::nonminimal_bool)]
6+
7+
macro_rules! blocky {
8+
() => {{
9+
true
10+
}};
11+
}
12+
13+
macro_rules! blocky_too {
14+
() => {{
15+
let r = true;
16+
r
17+
}};
18+
}
19+
20+
fn macro_if() {
21+
if blocky!() {}
22+
23+
if blocky_too!() {}
24+
}
25+
26+
fn condition_has_block() -> i32 {
27+
let res = {
28+
let x = 3;
29+
x == 3
30+
}; if res {
31+
6
32+
} else {
33+
10
34+
}
35+
}
36+
37+
fn condition_has_block_with_single_expression() -> i32 {
38+
if true {
39+
6
40+
} else {
41+
10
42+
}
43+
}
44+
45+
fn condition_is_normal() -> i32 {
46+
let x = 3;
47+
if x == 3 {
48+
6
49+
} else {
50+
10
51+
}
52+
}
53+
54+
fn condition_is_unsafe_block() {
55+
let a: i32 = 1;
56+
57+
// this should not warn because the condition is an unsafe block
58+
if unsafe { 1u32 == std::mem::transmute(a) } {
59+
println!("1u32 == a");
60+
}
61+
}
62+
63+
fn block_in_assert() {
64+
let opt = Some(42);
65+
assert!(opt
66+
.as_ref()
67+
.and_then(|val| {
68+
let mut v = val * 2;
69+
v -= 1;
70+
Some(v * 3)
71+
})
72+
.is_some());
73+
}
74+
75+
fn main() {}

tests/ui/block_in_if_condition.rs

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// run-rustfix
12
#![warn(clippy::block_in_if_condition_expr)]
23
#![warn(clippy::block_in_if_condition_stmt)]
34
#![allow(unused, clippy::let_and_return)]
@@ -41,37 +42,6 @@ fn condition_has_block_with_single_expression() -> i32 {
4142
}
4243
}
4344

44-
fn predicate<F: FnOnce(T) -> bool, T>(pfn: F, val: T) -> bool {
45-
pfn(val)
46-
}
47-
48-
fn pred_test() {
49-
let v = 3;
50-
let sky = "blue";
51-
// This is a sneaky case, where the block isn't directly in the condition,
52-
// but is actually nside a closure that the condition is using.
53-
// The same principle applies -- add some extra expressions to make sure
54-
// linter isn't confused by them.
55-
if v == 3
56-
&& sky == "blue"
57-
&& predicate(
58-
|x| {
59-
let target = 3;
60-
x == target
61-
},
62-
v,
63-
)
64-
{}
65-
66-
if predicate(
67-
|x| {
68-
let target = 3;
69-
x == target
70-
},
71-
v,
72-
) {}
73-
}
74-
7545
fn condition_is_normal() -> i32 {
7646
let x = 3;
7747
if true && x == 3 {
@@ -81,10 +51,6 @@ fn condition_is_normal() -> i32 {
8151
}
8252
}
8353

84-
fn closure_without_block() {
85-
if predicate(|x| x == 3, 6) {}
86-
}
87-
8854
fn condition_is_unsafe_block() {
8955
let a: i32 = 1;
9056

@@ -94,16 +60,6 @@ fn condition_is_unsafe_block() {
9460
}
9561
}
9662

97-
fn main() {}
98-
99-
fn macro_in_closure() {
100-
let option = Some(true);
101-
102-
if option.unwrap_or_else(|| unimplemented!()) {
103-
unimplemented!()
104-
}
105-
}
106-
10763
fn block_in_assert() {
10864
let opt = Some(42);
10965
assert!(opt
@@ -115,3 +71,5 @@ fn block_in_assert() {
11571
})
11672
.is_some());
11773
}
74+
75+
fn main() {}

tests/ui/block_in_if_condition.stderr

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,36 @@
11
error: in an `if` condition, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let`
2-
--> $DIR/block_in_if_condition.rs:26:8
2+
--> $DIR/block_in_if_condition.rs:27:5
33
|
4-
LL | if {
5-
| ________^
4+
LL | / if {
65
LL | | let x = 3;
76
LL | | x == 3
87
LL | | } {
98
| |_____^
109
|
1110
= note: `-D clippy::block-in-if-condition-stmt` implied by `-D warnings`
12-
= help: try
13-
let res = {
14-
let x = 3;
15-
x == 3
16-
};
17-
if res {
18-
6
19-
} ...
11+
help: try
12+
|
13+
LL | let res = {
14+
LL | let x = 3;
15+
LL | x == 3
16+
LL | }; if res {
17+
|
2018

2119
error: omit braces around single expression condition
22-
--> $DIR/block_in_if_condition.rs:37:8
20+
--> $DIR/block_in_if_condition.rs:38:8
2321
|
2422
LL | if { true } {
25-
| ^^^^^^^^
23+
| ^^^^^^^^ help: try: `true`
2624
|
2725
= note: `-D clippy::block-in-if-condition-expr` implied by `-D warnings`
28-
= help: try
29-
if true {
30-
6
31-
} ...
32-
33-
error: in an `if` condition, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let`
34-
--> $DIR/block_in_if_condition.rs:58:17
35-
|
36-
LL | |x| {
37-
| _________________^
38-
LL | | let target = 3;
39-
LL | | x == target
40-
LL | | },
41-
| |_____________^
42-
43-
error: in an `if` condition, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let`
44-
--> $DIR/block_in_if_condition.rs:67:13
45-
|
46-
LL | |x| {
47-
| _____________^
48-
LL | | let target = 3;
49-
LL | | x == target
50-
LL | | },
51-
| |_________^
5226

5327
error: this boolean expression can be simplified
54-
--> $DIR/block_in_if_condition.rs:77:8
28+
--> $DIR/block_in_if_condition.rs:47:8
5529
|
5630
LL | if true && x == 3 {
5731
| ^^^^^^^^^^^^^^ help: try: `x == 3`
5832
|
5933
= note: `-D clippy::nonminimal-bool` implied by `-D warnings`
6034

61-
error: aborting due to 5 previous errors
35+
error: aborting due to 3 previous errors
6236

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#![warn(clippy::block_in_if_condition_expr)]
2+
#![warn(clippy::block_in_if_condition_stmt)]
3+
#![allow(unused, clippy::let_and_return)]
4+
5+
fn predicate<F: FnOnce(T) -> bool, T>(pfn: F, val: T) -> bool {
6+
pfn(val)
7+
}
8+
9+
fn pred_test() {
10+
let v = 3;
11+
let sky = "blue";
12+
// This is a sneaky case, where the block isn't directly in the condition,
13+
// but is actually nside a closure that the condition is using.
14+
// The same principle applies -- add some extra expressions to make sure
15+
// linter isn't confused by them.
16+
if v == 3
17+
&& sky == "blue"
18+
&& predicate(
19+
|x| {
20+
let target = 3;
21+
x == target
22+
},
23+
v,
24+
)
25+
{}
26+
27+
if predicate(
28+
|x| {
29+
let target = 3;
30+
x == target
31+
},
32+
v,
33+
) {}
34+
}
35+
36+
fn closure_without_block() {
37+
if predicate(|x| x == 3, 6) {}
38+
}
39+
40+
fn macro_in_closure() {
41+
let option = Some(true);
42+
43+
if option.unwrap_or_else(|| unimplemented!()) {
44+
unimplemented!()
45+
}
46+
}
47+
48+
fn main() {}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error: in an `if` condition, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let`
2+
--> $DIR/block_in_if_condition_closure.rs:19:17
3+
|
4+
LL | |x| {
5+
| _________________^
6+
LL | | let target = 3;
7+
LL | | x == target
8+
LL | | },
9+
| |_____________^
10+
|
11+
= note: `-D clippy::block-in-if-condition-stmt` implied by `-D warnings`
12+
13+
error: in an `if` condition, avoid complex blocks or closures with blocks; instead, move the block or closure higher and bind it with a `let`
14+
--> $DIR/block_in_if_condition_closure.rs:28:13
15+
|
16+
LL | |x| {
17+
| _____________^
18+
LL | | let target = 3;
19+
LL | | x == target
20+
LL | | },
21+
| |_________^
22+
23+
error: aborting due to 2 previous errors
24+

0 commit comments

Comments
 (0)