-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathbool_to_int_with_if.rs
183 lines (159 loc) · 2.73 KB
/
bool_to_int_with_if.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#![feature(let_chains)]
#![warn(clippy::bool_to_int_with_if)]
#![allow(unused, dead_code, clippy::unnecessary_operation, clippy::no_effect)]
fn main() {
let a = true;
let b = false;
let x = 1;
let y = 2;
// Should lint
// precedence
if a {
//~^ bool_to_int_with_if
1
} else {
0
};
if a {
//~^ bool_to_int_with_if
0
} else {
1
};
if !a {
//~^ bool_to_int_with_if
1
} else {
0
};
if a || b {
//~^ bool_to_int_with_if
1
} else {
0
};
if cond(a, b) {
//~^ bool_to_int_with_if
1
} else {
0
};
if x + y < 4 {
//~^ bool_to_int_with_if
1
} else {
0
};
// if else if
if a {
123
} else if b {
//~^ bool_to_int_with_if
1
} else {
0
};
// if else if inverted
if a {
123
} else if b {
//~^ bool_to_int_with_if
0
} else {
1
};
// Shouldn't lint
if a {
1
} else if b {
0
} else {
3
};
if a {
3
} else if b {
1
} else {
-2
};
if a {
3
} else {
0
};
if a {
side_effect();
1
} else {
0
};
if a {
1
} else {
side_effect();
0
};
// multiple else ifs
if a {
123
} else if b {
1
} else if a | b {
0
} else {
123
};
pub const SHOULD_NOT_LINT: usize = if true { 1 } else { 0 };
// https://github.com/rust-lang/rust-clippy/issues/10452
let should_not_lint = [(); if true { 1 } else { 0 }];
let should_not_lint = const { if true { 1 } else { 0 } };
some_fn(a);
}
// Lint returns and type inference
fn some_fn(a: bool) -> u8 {
if a { 1 } else { 0 }
//~^ bool_to_int_with_if
}
fn side_effect() {}
fn cond(a: bool, b: bool) -> bool {
a || b
}
enum Enum {
A,
B,
}
fn if_let(a: Enum, b: Enum) {
if let Enum::A = a {
1
} else {
0
};
if let Enum::A = a
&& let Enum::B = b
{
1
} else {
0
};
}
fn issue14628() {
macro_rules! mac {
(if $cond:expr, $then:expr, $else:expr) => {
if $cond { $then } else { $else }
};
(zero) => {
0
};
(one) => {
1
};
}
let _ = if dbg!(4 > 0) { 1 } else { 0 };
//~^ bool_to_int_with_if
let _ = dbg!(if 4 > 0 { 1 } else { 0 });
//~^ bool_to_int_with_if
let _ = mac!(if 4 > 0, 1, 0);
let _ = if 4 > 0 { mac!(one) } else { 0 };
let _ = if 4 > 0 { 1 } else { mac!(zero) };
}