]> git.lizzy.rs Git - rust.git/blob - tests/ui/needless_bool.rs
Merge remote-tracking branch 'upstream/master'
[rust.git] / tests / ui / needless_bool.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 #![warn(clippy::needless_bool)]
12
13 use std::cell::Cell;
14
15 macro_rules! bool_comparison_trigger {
16     ($($i:ident: $def:expr, $stb:expr );+  $(;)*) => (
17
18         #[derive(Clone)]
19         pub struct Trigger {
20             $($i: (Cell<bool>, bool, bool)),+
21         }
22
23         #[allow(dead_code)]
24         impl Trigger {
25             pub fn trigger(&self, key: &str) -> bool {
26                 $(
27                     if let stringify!($i) = key {
28                         return self.$i.1 && self.$i.2 == $def;
29                     }
30                  )+
31                 false
32             }
33         }
34     )
35 }
36
37 #[allow(clippy::if_same_then_else)]
38 fn main() {
39     let x = true;
40     let y = false;
41     if x { true } else { true };
42     if x { false } else { false };
43     if x { true } else { false };
44     if x { false } else { true };
45     if x && y { false } else { true };
46     if x { x } else { false }; // would also be questionable, but we don't catch this yet
47     bool_ret(x);
48     bool_ret2(x);
49     bool_ret3(x);
50     bool_ret5(x, x);
51     bool_ret4(x);
52     bool_ret6(x, x);
53     needless_bool(x);
54     needless_bool2(x);
55     needless_bool3(x);
56 }
57
58 #[allow(clippy::if_same_then_else, clippy::needless_return)]
59 fn bool_ret(x: bool) -> bool {
60     if x { return true } else { return true };
61 }
62
63 #[allow(clippy::if_same_then_else, clippy::needless_return)]
64 fn bool_ret2(x: bool) -> bool {
65     if x { return false } else { return false };
66 }
67
68 #[allow(clippy::needless_return)]
69 fn bool_ret3(x: bool) -> bool {
70     if x { return true } else { return false };
71 }
72
73 #[allow(clippy::needless_return)]
74 fn bool_ret5(x: bool, y: bool) -> bool {
75     if x && y { return true } else { return false };
76 }
77
78 #[allow(clippy::needless_return)]
79 fn bool_ret4(x: bool) -> bool {
80     if x { return false } else { return true };
81 }
82
83 #[allow(clippy::needless_return)]
84 fn bool_ret6(x: bool, y: bool) -> bool {
85     if x && y { return false } else { return true };
86 }
87
88 fn needless_bool(x: bool) {
89    if x  == true { };
90 }
91
92 fn needless_bool2(x: bool) {
93    if x  == false { };
94 }
95
96 fn needless_bool3(x: bool) {
97     
98     bool_comparison_trigger! {
99         test_one:   false, false;
100         test_three: false, false;
101         test_two:   true, true;
102     }
103     
104     if x == true { };
105     if x == false { };
106 }