]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/needless_bool/fixable.fixed
Merge pull request #16 from ian-h-chamberlain/feature/target-thread-local
[rust.git] / src / tools / clippy / tests / ui / needless_bool / fixable.fixed
1 // run-rustfix
2
3 #![warn(clippy::needless_bool)]
4 #![allow(
5     unused,
6     dead_code,
7     clippy::no_effect,
8     clippy::if_same_then_else,
9     clippy::equatable_if_let,
10     clippy::needless_return,
11     clippy::self_named_constructors
12 )]
13
14 use std::cell::Cell;
15
16 macro_rules! bool_comparison_trigger {
17     ($($i:ident: $def:expr, $stb:expr );+  $(;)*) => (
18
19         #[derive(Clone)]
20         pub struct Trigger {
21             $($i: (Cell<bool>, bool, bool)),+
22         }
23
24         #[allow(dead_code)]
25         impl Trigger {
26             pub fn trigger(&self, key: &str) -> bool {
27                 $(
28                     if let stringify!($i) = key {
29                         return self.$i.1 && self.$i.2 == $def;
30                     }
31                  )+
32                 false
33             }
34         }
35     )
36 }
37
38 fn main() {
39     let x = true;
40     let y = false;
41     x;
42     !x;
43     !(x && y);
44     let a = 0;
45     let b = 1;
46
47     a != b;
48     a == b;
49     a >= b;
50     a > b;
51     a <= b;
52     a < b;
53     if x {
54         x
55     } else {
56         false
57     }; // would also be questionable, but we don't catch this yet
58     bool_ret3(x);
59     bool_ret4(x);
60     bool_ret5(x, x);
61     bool_ret6(x, x);
62     needless_bool(x);
63     needless_bool2(x);
64     needless_bool3(x);
65     needless_bool_condition();
66 }
67
68 fn bool_ret3(x: bool) -> bool {
69     return x;
70 }
71
72 fn bool_ret4(x: bool) -> bool {
73     return !x;
74 }
75
76 fn bool_ret5(x: bool, y: bool) -> bool {
77     return x && y;
78 }
79
80 fn bool_ret6(x: bool, y: bool) -> bool {
81     return !(x && y);
82 }
83
84 fn needless_bool(x: bool) {
85     if x {};
86 }
87
88 fn needless_bool2(x: bool) {
89     if !x {};
90 }
91
92 fn needless_bool3(x: bool) {
93     bool_comparison_trigger! {
94         test_one:   false, false;
95         test_three: false, false;
96         test_two:   true, true;
97     }
98
99     if x {};
100     if !x {};
101 }
102
103 fn needless_bool_in_the_suggestion_wraps_the_predicate_of_if_else_statement_in_brackets() {
104     let b = false;
105     let returns_bool = || false;
106
107     let x = if b {
108         true
109     } else { !returns_bool() };
110 }
111
112 unsafe fn no(v: u8) -> u8 {
113     v
114 }
115
116 #[allow(clippy::unnecessary_operation)]
117 fn needless_bool_condition() -> bool {
118     (unsafe { no(4) } & 1 != 0);
119     let _brackets_unneeded = unsafe { no(4) } & 1 != 0;
120     fn foo() -> bool {
121         // parentheses are needed here
122         (unsafe { no(4) } & 1 != 0)
123     }
124
125     foo()
126 }