fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
2.4 KiB132 linesraw
1#![warn(clippy::needless_bool)]
2#![allow(
3    unused,
4    dead_code,
5    clippy::no_effect,
6    clippy::if_same_then_else,
7    clippy::equatable_if_let,
8    clippy::needless_if,
9    clippy::needless_return,
10    clippy::self_named_constructors
11)]
12
13use std::cell::Cell;
14
15macro_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
37fn main() {
38    let x = true;
39    let y = false;
40    x;
41    !x;
42    !(x && y);
43    let a = 0;
44    let b = 1;
45
46    a != b;
47    a == b;
48    a >= b;
49    a > b;
50    a <= b;
51    a < b;
52    if x {
53        x
54    } else {
55        false
56    }; // would also be questionable, but we don't catch this yet
57    bool_ret3(x);
58    bool_ret4(x);
59    bool_ret5(x, x);
60    bool_ret6(x, x);
61    needless_bool(x);
62    needless_bool2(x);
63    needless_bool3(x);
64    needless_bool_condition();
65
66    if a == b {
67        true
68    } else {
69        // Do not lint as this comment might be important
70        false
71    };
72}
73
74fn bool_ret3(x: bool) -> bool {
75    return x;
76}
77
78fn bool_ret4(x: bool) -> bool {
79    return !x;
80}
81
82fn bool_ret5(x: bool, y: bool) -> bool {
83    return x && y;
84}
85
86fn bool_ret6(x: bool, y: bool) -> bool {
87    return !(x && y);
88}
89
90fn needless_bool(x: bool) {
91    if x {};
92}
93
94fn needless_bool2(x: bool) {
95    if !x {};
96}
97
98fn needless_bool3(x: bool) {
99    bool_comparison_trigger! {
100        test_one:   false, false;
101        test_three: false, false;
102        test_two:   true, true;
103    }
104
105    if x {};
106    if !x {};
107}
108
109fn needless_bool_in_the_suggestion_wraps_the_predicate_of_if_else_statement_in_brackets() {
110    let b = false;
111    let returns_bool = || false;
112
113    let x = if b {
114        true
115    } else { !returns_bool() };
116}
117
118unsafe fn no(v: u8) -> u8 {
119    v
120}
121
122#[allow(clippy::unnecessary_operation)]
123fn needless_bool_condition() -> bool {
124    (unsafe { no(4) } & 1 != 0);
125    let _brackets_unneeded = unsafe { no(4) } & 1 != 0;
126    fn foo() -> bool {
127        // parentheses are needed here
128        (unsafe { no(4) } & 1 != 0)
129    }
130
131    foo()
132}