]> git.lizzy.rs Git - rust.git/blob - tests/ui/block_in_if_condition.rs
Merge pull request #3285 from devonhollowood/pedantic-dogfood-items-after-statements
[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
11 #![feature(tool_lints)]
12
13
14 #![warn(clippy::block_in_if_condition_expr)]
15 #![warn(clippy::block_in_if_condition_stmt)]
16 #![allow(unused, clippy::let_and_return)]
17 #![warn(clippy::nonminimal_bool)]
18
19
20 macro_rules! blocky {
21     () => {{true}}
22 }
23
24 macro_rules! blocky_too {
25     () => {{
26         let r = true;
27         r
28     }}
29 }
30
31 fn macro_if() {
32     if blocky!() {
33     }
34
35     if blocky_too!() {
36     }
37 }
38
39 fn condition_has_block() -> i32 {
40     if {
41         let x = 3;
42         x == 3
43     } {
44         6
45     } else {
46         10
47     }
48 }
49
50 fn condition_has_block_with_single_expression() -> i32 {
51     if { true } {
52         6
53     } else {
54         10
55     }
56 }
57
58 fn predicate<F: FnOnce(T) -> bool, T>(pfn: F, val:T) -> bool {
59     pfn(val)
60 }
61
62 fn pred_test() {
63     let v = 3;
64     let sky = "blue";
65     // this is a sneaky case, where the block isn't directly in the condition, but is actually
66     // inside a closure that the condition is using.  same principle applies.  add some extra
67     // expressions to make sure linter isn't confused by them.
68     if v == 3 && sky == "blue" && predicate(|x| { let target = 3; x == target }, v) {
69     }
70
71     if predicate(|x| { let target = 3; x == target }, 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 }
89
90 fn condition_is_unsafe_block() {
91     let a: i32 = 1;
92
93     // this should not warn because the condition is an unsafe block
94     if unsafe { 1u32 == std::mem::transmute(a) } {
95         println!("1u32 == a");
96     }
97 }
98
99 fn main() {
100 }