]> git.lizzy.rs Git - rust.git/blob - tests/ui/block_in_if_condition.rs
Auto merge of #3603 - xfix:random-state-lint, r=phansch
[rust.git] / tests / ui / block_in_if_condition.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![warn(clippy::block_in_if_condition_expr)]
11 #![warn(clippy::block_in_if_condition_stmt)]
12 #![allow(unused, clippy::let_and_return)]
13 #![warn(clippy::nonminimal_bool)]
14
15 macro_rules! blocky {
16     () => {{
17         true
18     }};
19 }
20
21 macro_rules! blocky_too {
22     () => {{
23         let r = true;
24         r
25     }};
26 }
27
28 fn macro_if() {
29     if blocky!() {}
30
31     if blocky_too!() {}
32 }
33
34 fn condition_has_block() -> i32 {
35     if {
36         let x = 3;
37         x == 3
38     } {
39         6
40     } else {
41         10
42     }
43 }
44
45 fn condition_has_block_with_single_expression() -> i32 {
46     if { true } {
47         6
48     } else {
49         10
50     }
51 }
52
53 fn predicate<F: FnOnce(T) -> bool, T>(pfn: F, val: T) -> bool {
54     pfn(val)
55 }
56
57 fn pred_test() {
58     let v = 3;
59     let sky = "blue";
60     // this is a sneaky case, where the block isn't directly in the condition, but is actually
61     // inside a closure that the condition is using.  same principle applies.  add some extra
62     // expressions to make sure linter isn't confused by them.
63     if v == 3
64         && sky == "blue"
65         && predicate(
66             |x| {
67                 let target = 3;
68                 x == target
69             },
70             v,
71         )
72     {}
73
74     if predicate(
75         |x| {
76             let target = 3;
77             x == target
78         },
79         v,
80     ) {}
81 }
82
83 fn condition_is_normal() -> i32 {
84     let x = 3;
85     if true && x == 3 {
86         6
87     } else {
88         10
89     }
90 }
91
92 fn closure_without_block() {
93     if predicate(|x| x == 3, 6) {}
94 }
95
96 fn condition_is_unsafe_block() {
97     let a: i32 = 1;
98
99     // this should not warn because the condition is an unsafe block
100     if unsafe { 1u32 == std::mem::transmute(a) } {
101         println!("1u32 == a");
102     }
103 }
104
105 fn main() {}
106
107 fn macro_in_closure() {
108     let option = Some(true);
109
110     if option.unwrap_or_else(|| unimplemented!()) {
111         unimplemented!()
112     }
113 }