]> git.lizzy.rs Git - rust.git/blob - tests/ui/block_in_if_condition.rs
Auto merge of #3646 - matthiaskrgr:travis, r=phansch
[rust.git] / tests / ui / block_in_if_condition.rs
1 #![warn(clippy::block_in_if_condition_expr)]
2 #![warn(clippy::block_in_if_condition_stmt)]
3 #![allow(unused, clippy::let_and_return)]
4 #![warn(clippy::nonminimal_bool)]
5
6 macro_rules! blocky {
7     () => {{
8         true
9     }};
10 }
11
12 macro_rules! blocky_too {
13     () => {{
14         let r = true;
15         r
16     }};
17 }
18
19 fn macro_if() {
20     if blocky!() {}
21
22     if blocky_too!() {}
23 }
24
25 fn condition_has_block() -> i32 {
26     if {
27         let x = 3;
28         x == 3
29     } {
30         6
31     } else {
32         10
33     }
34 }
35
36 fn condition_has_block_with_single_expression() -> i32 {
37     if { true } {
38         6
39     } else {
40         10
41     }
42 }
43
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, but is actually
52     // inside a closure that the condition is using.  same principle applies.  add some extra
53     // expressions to make sure linter isn't confused by them.
54     if v == 3
55         && sky == "blue"
56         && predicate(
57             |x| {
58                 let target = 3;
59                 x == target
60             },
61             v,
62         )
63     {}
64
65     if predicate(
66         |x| {
67             let target = 3;
68             x == target
69         },
70         v,
71     ) {}
72 }
73
74 fn condition_is_normal() -> i32 {
75     let x = 3;
76     if true && x == 3 {
77         6
78     } else {
79         10
80     }
81 }
82
83 fn closure_without_block() {
84     if predicate(|x| x == 3, 6) {}
85 }
86
87 fn condition_is_unsafe_block() {
88     let a: i32 = 1;
89
90     // this should not warn because the condition is an unsafe block
91     if unsafe { 1u32 == std::mem::transmute(a) } {
92         println!("1u32 == a");
93     }
94 }
95
96 fn main() {}
97
98 fn macro_in_closure() {
99     let option = Some(true);
100
101     if option.unwrap_or_else(|| unimplemented!()) {
102         unimplemented!()
103     }
104 }