Skip to content

Commit 2e4a11e

Browse files
committed
Auto merge of rust-lang#13338 - CoCo-Japan-pan:nonminimal_bool_casted, r=Centri3
fix incorrect suggestion for `!(a >= b) as i32 == c` fixes rust-lang#12761 The expression `!(a >= b) as i32 == c` got simplified to `a < b as i32 == c`, but this is a syntax error. The result we want is `(a < b) as i32 == c`. This is fixed by adding a parenthesis to the suggestion given in `check_simplify_not` when the boolean expression is casted. changelog: [`nonminimal_bool`]: fix incorrect suggestion for `!(a >= b) as i32 == c`
2 parents a529c44 + d30a026 commit 2e4a11e

File tree

5 files changed

+134
-29
lines changed

5 files changed

+134
-29
lines changed

clippy_lints/src/booleans.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,21 @@ fn check_simplify_not(cx: &LateContext<'_>, msrv: &Msrv, expr: &Expr<'_>) {
203203
&& let Some(suggestion) = simplify_not(cx, msrv, inner)
204204
&& cx.tcx.lint_level_at_node(NONMINIMAL_BOOL, expr.hir_id).0 != Level::Allow
205205
{
206+
use clippy_utils::sugg::{Sugg, has_enclosing_paren};
207+
let maybe_par = if let Some(sug) = Sugg::hir_opt(cx, inner) {
208+
match sug {
209+
Sugg::BinOp(..) => true,
210+
Sugg::MaybeParen(sug) if !has_enclosing_paren(&sug) => true,
211+
_ => false,
212+
}
213+
} else {
214+
false
215+
};
216+
let suggestion = if maybe_par {
217+
format!("({suggestion})")
218+
} else {
219+
suggestion
220+
};
206221
span_lint_and_sugg(
207222
cx,
208223
NONMINIMAL_BOOL,

tests/ui/nonminimal_bool.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,25 +118,25 @@ error: this boolean expression can be simplified
118118
--> tests/ui/nonminimal_bool.rs:161:8
119119
|
120120
LL | if !(12 == a) {}
121-
| ^^^^^^^^^^ help: try: `12 != a`
121+
| ^^^^^^^^^^ help: try: `(12 != a)`
122122

123123
error: this boolean expression can be simplified
124124
--> tests/ui/nonminimal_bool.rs:162:8
125125
|
126126
LL | if !(a == 12) {}
127-
| ^^^^^^^^^^ help: try: `a != 12`
127+
| ^^^^^^^^^^ help: try: `(a != 12)`
128128

129129
error: this boolean expression can be simplified
130130
--> tests/ui/nonminimal_bool.rs:163:8
131131
|
132132
LL | if !(12 != a) {}
133-
| ^^^^^^^^^^ help: try: `12 == a`
133+
| ^^^^^^^^^^ help: try: `(12 == a)`
134134

135135
error: this boolean expression can be simplified
136136
--> tests/ui/nonminimal_bool.rs:164:8
137137
|
138138
LL | if !(a != 12) {}
139-
| ^^^^^^^^^^ help: try: `a == 12`
139+
| ^^^^^^^^^^ help: try: `(a == 12)`
140140

141141
error: this boolean expression can be simplified
142142
--> tests/ui/nonminimal_bool.rs:168:8

tests/ui/nonminimal_bool_methods.fixed

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,33 @@ fn dont_warn_for_negated_partial_ord_comparison() {
110110
fn issue_12625() {
111111
let a = 0;
112112
let b = 0;
113-
if (a as u64) < b {} //~ ERROR: this boolean expression can be simplified
114-
if (a as u64) < b {} //~ ERROR: this boolean expression can be simplified
115-
if a as u64 > b {} //~ ERROR: this boolean expression can be simplified
113+
if ((a as u64) < b) {} //~ ERROR: this boolean expression can be simplified
114+
if ((a as u64) < b) {} //~ ERROR: this boolean expression can be simplified
115+
if (a as u64 > b) {} //~ ERROR: this boolean expression can be simplified
116+
}
117+
118+
fn issue_12761() {
119+
let a = 0;
120+
let b = 0;
121+
let c = 0;
122+
if (a < b) as i32 == c {} //~ ERROR: this boolean expression can be simplified
123+
if (a < b) | (a > c) {} //~ ERROR: this boolean expression can be simplified
124+
let opt: Option<usize> = Some(1);
125+
let res: Result<usize, usize> = Ok(1);
126+
if res.is_err() as i32 == c {} //~ ERROR: this boolean expression can be simplified
127+
if res.is_err() | opt.is_some() {} //~ ERROR: this boolean expression can be simplified
128+
129+
fn a(a: bool) -> bool {
130+
(4 <= 3).b() //~ ERROR: this boolean expression can be simplified
131+
}
132+
133+
trait B {
134+
fn b(&self) -> bool {
135+
true
136+
}
137+
}
138+
139+
impl B for bool {}
116140
}
117141

118142
fn issue_13436() {

tests/ui/nonminimal_bool_methods.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,30 @@ fn issue_12625() {
115115
if !(a as u64 <= b) {} //~ ERROR: this boolean expression can be simplified
116116
}
117117

118+
fn issue_12761() {
119+
let a = 0;
120+
let b = 0;
121+
let c = 0;
122+
if !(a >= b) as i32 == c {} //~ ERROR: this boolean expression can be simplified
123+
if !(a >= b) | !(a <= c) {} //~ ERROR: this boolean expression can be simplified
124+
let opt: Option<usize> = Some(1);
125+
let res: Result<usize, usize> = Ok(1);
126+
if !res.is_ok() as i32 == c {} //~ ERROR: this boolean expression can be simplified
127+
if !res.is_ok() | !opt.is_none() {} //~ ERROR: this boolean expression can be simplified
128+
129+
fn a(a: bool) -> bool {
130+
(!(4 > 3)).b() //~ ERROR: this boolean expression can be simplified
131+
}
132+
133+
trait B {
134+
fn b(&self) -> bool {
135+
true
136+
}
137+
}
138+
139+
impl B for bool {}
140+
}
141+
118142
fn issue_13436() {
119143
fn not_zero(x: i32) -> bool {
120144
x != 0

tests/ui/nonminimal_bool_methods.stderr

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -83,127 +83,169 @@ error: this boolean expression can be simplified
8383
--> tests/ui/nonminimal_bool_methods.rs:113:8
8484
|
8585
LL | if !(a as u64 >= b) {}
86-
| ^^^^^^^^^^^^^^^^ help: try: `(a as u64) < b`
86+
| ^^^^^^^^^^^^^^^^ help: try: `((a as u64) < b)`
8787

8888
error: this boolean expression can be simplified
8989
--> tests/ui/nonminimal_bool_methods.rs:114:8
9090
|
9191
LL | if !((a as u64) >= b) {}
92-
| ^^^^^^^^^^^^^^^^^^ help: try: `(a as u64) < b`
92+
| ^^^^^^^^^^^^^^^^^^ help: try: `((a as u64) < b)`
9393

9494
error: this boolean expression can be simplified
9595
--> tests/ui/nonminimal_bool_methods.rs:115:8
9696
|
9797
LL | if !(a as u64 <= b) {}
98-
| ^^^^^^^^^^^^^^^^ help: try: `a as u64 > b`
98+
| ^^^^^^^^^^^^^^^^ help: try: `(a as u64 > b)`
9999

100100
error: this boolean expression can be simplified
101-
--> tests/ui/nonminimal_bool_methods.rs:131:9
101+
--> tests/ui/nonminimal_bool_methods.rs:122:8
102+
|
103+
LL | if !(a >= b) as i32 == c {}
104+
| ^^^^^^^^^ help: try: `(a < b)`
105+
106+
error: this boolean expression can be simplified
107+
--> tests/ui/nonminimal_bool_methods.rs:123:8
108+
|
109+
LL | if !(a >= b) | !(a <= c) {}
110+
| ^^^^^^^^^ help: try: `(a < b)`
111+
112+
error: this boolean expression can be simplified
113+
--> tests/ui/nonminimal_bool_methods.rs:123:20
114+
|
115+
LL | if !(a >= b) | !(a <= c) {}
116+
| ^^^^^^^^^ help: try: `(a > c)`
117+
118+
error: this boolean expression can be simplified
119+
--> tests/ui/nonminimal_bool_methods.rs:126:8
120+
|
121+
LL | if !res.is_ok() as i32 == c {}
122+
| ^^^^^^^^^^^^ help: try: `res.is_err()`
123+
124+
error: this boolean expression can be simplified
125+
--> tests/ui/nonminimal_bool_methods.rs:127:8
126+
|
127+
LL | if !res.is_ok() | !opt.is_none() {}
128+
| ^^^^^^^^^^^^ help: try: `res.is_err()`
129+
130+
error: this boolean expression can be simplified
131+
--> tests/ui/nonminimal_bool_methods.rs:127:23
132+
|
133+
LL | if !res.is_ok() | !opt.is_none() {}
134+
| ^^^^^^^^^^^^^^ help: try: `opt.is_some()`
135+
136+
error: this boolean expression can be simplified
137+
--> tests/ui/nonminimal_bool_methods.rs:130:9
138+
|
139+
LL | (!(4 > 3)).b()
140+
| ^^^^^^^^^^ help: try: `(4 <= 3)`
141+
142+
error: this boolean expression can be simplified
143+
--> tests/ui/nonminimal_bool_methods.rs:155:9
102144
|
103145
LL | _ = !opt.is_some_and(|x| x < 1000);
104146
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `opt.is_none_or(|x| x >= 1000)`
105147

106148
error: this boolean expression can be simplified
107-
--> tests/ui/nonminimal_bool_methods.rs:132:9
149+
--> tests/ui/nonminimal_bool_methods.rs:156:9
108150
|
109151
LL | _ = !opt.is_some_and(|x| x <= 1000);
110152
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `opt.is_none_or(|x| x > 1000)`
111153

112154
error: this boolean expression can be simplified
113-
--> tests/ui/nonminimal_bool_methods.rs:133:9
155+
--> tests/ui/nonminimal_bool_methods.rs:157:9
114156
|
115157
LL | _ = !opt.is_some_and(|x| x > 1000);
116158
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `opt.is_none_or(|x| x <= 1000)`
117159

118160
error: this boolean expression can be simplified
119-
--> tests/ui/nonminimal_bool_methods.rs:134:9
161+
--> tests/ui/nonminimal_bool_methods.rs:158:9
120162
|
121163
LL | _ = !opt.is_some_and(|x| x >= 1000);
122164
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `opt.is_none_or(|x| x < 1000)`
123165

124166
error: this boolean expression can be simplified
125-
--> tests/ui/nonminimal_bool_methods.rs:135:9
167+
--> tests/ui/nonminimal_bool_methods.rs:159:9
126168
|
127169
LL | _ = !opt.is_some_and(|x| x == 1000);
128170
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `opt.is_none_or(|x| x != 1000)`
129171

130172
error: this boolean expression can be simplified
131-
--> tests/ui/nonminimal_bool_methods.rs:136:9
173+
--> tests/ui/nonminimal_bool_methods.rs:160:9
132174
|
133175
LL | _ = !opt.is_some_and(|x| x != 1000);
134176
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `opt.is_none_or(|x| x == 1000)`
135177

136178
error: this boolean expression can be simplified
137-
--> tests/ui/nonminimal_bool_methods.rs:145:9
179+
--> tests/ui/nonminimal_bool_methods.rs:169:9
138180
|
139181
LL | _ = !opt.is_none_or(|x| x < 1000);
140182
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `opt.is_some_and(|x| x >= 1000)`
141183

142184
error: this boolean expression can be simplified
143-
--> tests/ui/nonminimal_bool_methods.rs:146:9
185+
--> tests/ui/nonminimal_bool_methods.rs:170:9
144186
|
145187
LL | _ = !opt.is_none_or(|x| x <= 1000);
146188
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `opt.is_some_and(|x| x > 1000)`
147189

148190
error: this boolean expression can be simplified
149-
--> tests/ui/nonminimal_bool_methods.rs:147:9
191+
--> tests/ui/nonminimal_bool_methods.rs:171:9
150192
|
151193
LL | _ = !opt.is_none_or(|x| x > 1000);
152194
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `opt.is_some_and(|x| x <= 1000)`
153195

154196
error: this boolean expression can be simplified
155-
--> tests/ui/nonminimal_bool_methods.rs:148:9
197+
--> tests/ui/nonminimal_bool_methods.rs:172:9
156198
|
157199
LL | _ = !opt.is_none_or(|x| x >= 1000);
158200
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `opt.is_some_and(|x| x < 1000)`
159201

160202
error: this boolean expression can be simplified
161-
--> tests/ui/nonminimal_bool_methods.rs:149:9
203+
--> tests/ui/nonminimal_bool_methods.rs:173:9
162204
|
163205
LL | _ = !opt.is_none_or(|x| x == 1000);
164206
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `opt.is_some_and(|x| x != 1000)`
165207

166208
error: this boolean expression can be simplified
167-
--> tests/ui/nonminimal_bool_methods.rs:150:9
209+
--> tests/ui/nonminimal_bool_methods.rs:174:9
168210
|
169211
LL | _ = !opt.is_none_or(|x| x != 1000);
170212
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `opt.is_some_and(|x| x == 1000)`
171213

172214
error: this boolean expression can be simplified
173-
--> tests/ui/nonminimal_bool_methods.rs:157:9
215+
--> tests/ui/nonminimal_bool_methods.rs:181:9
174216
|
175217
LL | _ = !opt.is_some_and(|x| !x);
176218
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `opt.is_none_or(|x| x)`
177219

178220
error: this boolean expression can be simplified
179-
--> tests/ui/nonminimal_bool_methods.rs:161:9
221+
--> tests/ui/nonminimal_bool_methods.rs:185:9
180222
|
181223
LL | _ = !opt.is_none_or(|x| !x);
182224
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try: `opt.is_some_and(|x| x)`
183225

184226
error: this boolean expression can be simplified
185-
--> tests/ui/nonminimal_bool_methods.rs:168:9
227+
--> tests/ui/nonminimal_bool_methods.rs:192:9
186228
|
187229
LL | _ = !opt.is_some_and(|x| x.is_ok());
188230
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `opt.is_none_or(|x| x.is_err())`
189231

190232
error: this boolean expression can be simplified
191-
--> tests/ui/nonminimal_bool_methods.rs:169:9
233+
--> tests/ui/nonminimal_bool_methods.rs:193:9
192234
|
193235
LL | _ = !opt.is_some_and(|x| x.is_err());
194236
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `opt.is_none_or(|x| x.is_ok())`
195237

196238
error: this boolean expression can be simplified
197-
--> tests/ui/nonminimal_bool_methods.rs:170:9
239+
--> tests/ui/nonminimal_bool_methods.rs:194:9
198240
|
199241
LL | _ = !opt.is_none_or(|x| x.is_ok());
200242
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `opt.is_some_and(|x| x.is_err())`
201243

202244
error: this boolean expression can be simplified
203-
--> tests/ui/nonminimal_bool_methods.rs:171:9
245+
--> tests/ui/nonminimal_bool_methods.rs:195:9
204246
|
205247
LL | _ = !opt.is_none_or(|x| x.is_err());
206248
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `opt.is_some_and(|x| x.is_ok())`
207249

208-
error: aborting due to 34 previous errors
250+
error: aborting due to 41 previous errors
209251

0 commit comments

Comments
 (0)