]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/tests/ui/needless_bool/fixable.rs
New upstream version 1.52.1+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / needless_bool / fixable.rs
CommitLineData
f20569fa
XL
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::needless_return
10)]
11
12use std::cell::Cell;
13
14macro_rules! bool_comparison_trigger {
15 ($($i:ident: $def:expr, $stb:expr );+ $(;)*) => (
16
17 #[derive(Clone)]
18 pub struct Trigger {
19 $($i: (Cell<bool>, bool, bool)),+
20 }
21
22 #[allow(dead_code)]
23 impl Trigger {
24 pub fn trigger(&self, key: &str) -> bool {
25 $(
26 if let stringify!($i) = key {
27 return self.$i.1 && self.$i.2 == $def;
28 }
29 )+
30 false
31 }
32 }
33 )
34}
35
36fn main() {
37 let x = true;
38 let y = false;
39 if x {
40 true
41 } else {
42 false
43 };
44 if x {
45 false
46 } else {
47 true
48 };
49 if x && y {
50 false
51 } else {
52 true
53 };
54 if x {
55 x
56 } else {
57 false
58 }; // would also be questionable, but we don't catch this yet
59 bool_ret3(x);
60 bool_ret4(x);
61 bool_ret5(x, x);
62 bool_ret6(x, x);
63 needless_bool(x);
64 needless_bool2(x);
65 needless_bool3(x);
66}
67
68fn bool_ret3(x: bool) -> bool {
69 if x {
70 return true;
71 } else {
72 return false;
73 };
74}
75
76fn bool_ret4(x: bool) -> bool {
77 if x {
78 return false;
79 } else {
80 return true;
81 };
82}
83
84fn bool_ret5(x: bool, y: bool) -> bool {
85 if x && y {
86 return true;
87 } else {
88 return false;
89 };
90}
91
92fn bool_ret6(x: bool, y: bool) -> bool {
93 if x && y {
94 return false;
95 } else {
96 return true;
97 };
98}
99
100fn needless_bool(x: bool) {
101 if x == true {};
102}
103
104fn needless_bool2(x: bool) {
105 if x == false {};
106}
107
108fn needless_bool3(x: bool) {
109 bool_comparison_trigger! {
110 test_one: false, false;
111 test_three: false, false;
112 test_two: true, true;
113 }
114
115 if x == true {};
116 if x == false {};
117}
118
119fn needless_bool_in_the_suggestion_wraps_the_predicate_of_if_else_statement_in_brackets() {
120 let b = false;
121 let returns_bool = || false;
122
123 let x = if b {
124 true
125 } else if returns_bool() {
126 false
127 } else {
128 true
129 };
130}