]> git.lizzy.rs Git - rust.git/blob - tests/ui/block_in_if_condition.rs
Adding try_err lint
[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,
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
75 fn condition_is_normal() -> i32 {
76     let x = 3;
77     if true && x == 3 {
78         6
79     } else {
80         10
81     }
82 }
83
84 fn closure_without_block() {
85     if predicate(|x| x == 3, 6) {}
86 }
87
88 fn condition_is_unsafe_block() {
89     let a: i32 = 1;
90
91     // this should not warn because the condition is an unsafe block
92     if unsafe { 1u32 == std::mem::transmute(a) } {
93         println!("1u32 == a");
94     }
95 }
96
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 }