]> git.lizzy.rs Git - rust.git/blob - tests/ui/needless_bool/fixable.fixed
Auto merge of #68717 - petrochenkov:stabexpat, r=varkor
[rust.git] / 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::needless_return
10 )]
11
12 use std::cell::Cell;
13
14 macro_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
36 fn main() {
37     let x = true;
38     let y = false;
39     x;
40     !x;
41     !(x && y);
42     if x {
43         x
44     } else {
45         false
46     }; // would also be questionable, but we don't catch this yet
47     bool_ret3(x);
48     bool_ret4(x);
49     bool_ret5(x, x);
50     bool_ret6(x, x);
51     needless_bool(x);
52     needless_bool2(x);
53     needless_bool3(x);
54 }
55
56 fn bool_ret3(x: bool) -> bool {
57     return x;
58 }
59
60 fn bool_ret4(x: bool) -> bool {
61     return !x;
62 }
63
64 fn bool_ret5(x: bool, y: bool) -> bool {
65     return x && y;
66 }
67
68 fn bool_ret6(x: bool, y: bool) -> bool {
69     return !(x && y);
70 }
71
72 fn needless_bool(x: bool) {
73     if x {};
74 }
75
76 fn needless_bool2(x: bool) {
77     if !x {};
78 }
79
80 fn needless_bool3(x: bool) {
81     bool_comparison_trigger! {
82         test_one:   false, false;
83         test_three: false, false;
84         test_two:   true, true;
85     }
86
87     if x {};
88     if !x {};
89 }
90
91 fn needless_bool_in_the_suggestion_wraps_the_predicate_of_if_else_statement_in_brackets() {
92     let b = false;
93     let returns_bool = || false;
94
95     let x = if b {
96         true
97     } else { !returns_bool() };
98 }