Skip to content

Commit c3e85f3

Browse files
committed
fix incorrect suggestion for !(a as type >= b)
1 parent f9f854f commit c3e85f3

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

clippy_lints/src/booleans.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,17 @@ fn simplify_not(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<String> {
346346
_ => None,
347347
}
348348
.and_then(|op| {
349-
Some(format!(
350-
"{}{op}{}",
351-
snippet_opt(cx, lhs.span)?,
352-
snippet_opt(cx, rhs.span)?
353-
))
349+
let lhs_snippet = snippet_opt(cx, lhs.span)?;
350+
let rhs_snippet = snippet_opt(cx, rhs.span)?;
351+
352+
match &lhs.kind {
353+
ExprKind::Cast(..) if binop.node == BinOpKind::Ge => {
354+
// e.g. `(a as u64) < b`. Without the parens the `<` is
355+
// interpreted as a start of generic arguments for `u64`
356+
Some(format!("({lhs_snippet}){op}{rhs_snippet}"))
357+
},
358+
_ => Some(format!("{lhs_snippet}{op}{rhs_snippet}")),
359+
}
354360
})
355361
},
356362
ExprKind::MethodCall(path, receiver, [], _) => {

tests/ui/nonminimal_bool.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,10 @@ fn issue_12371(x: usize) -> bool {
179179
// Should not warn!
180180
!x != 0
181181
}
182+
183+
fn issue_12625() {
184+
let a = 0;
185+
let b = 0;
186+
if !(a as u64 >= b) {} //~ ERROR: this boolean expression can be simplified
187+
if !(a as u64 <= b) {} //~ ERROR: this boolean expression can be simplified
188+
}

tests/ui/nonminimal_bool.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,5 +213,17 @@ error: this boolean expression can be simplified
213213
LL | if !b != !c {}
214214
| ^^^^^^^^ help: try: `b != c`
215215

216-
error: aborting due to 29 previous errors
216+
error: this boolean expression can be simplified
217+
--> tests/ui/nonminimal_bool.rs:186:8
218+
|
219+
LL | if !(a as u64 >= b) {}
220+
| ^^^^^^^^^^^^^^^^ help: try: `(a as u64) < b`
221+
222+
error: this boolean expression can be simplified
223+
--> tests/ui/nonminimal_bool.rs:187:8
224+
|
225+
LL | if !(a as u64 <= b) {}
226+
| ^^^^^^^^^^^^^^^^ help: try: `a as u64 > b`
227+
228+
error: aborting due to 31 previous errors
217229

0 commit comments

Comments
 (0)