From c3e85f3ae96f35a43d966614bf73b11b096897fe Mon Sep 17 00:00:00 2001 From: Folkert Date: Wed, 3 Apr 2024 10:10:00 +0200 Subject: [PATCH] fix incorrect suggestion for `!(a as type >= b)` --- clippy_lints/src/booleans.rs | 16 +++++++++++----- tests/ui/nonminimal_bool.rs | 7 +++++++ tests/ui/nonminimal_bool.stderr | 14 +++++++++++++- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/clippy_lints/src/booleans.rs b/clippy_lints/src/booleans.rs index 6edfebb5534f..de833481bed1 100644 --- a/clippy_lints/src/booleans.rs +++ b/clippy_lints/src/booleans.rs @@ -346,11 +346,17 @@ fn simplify_not(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option { _ => None, } .and_then(|op| { - Some(format!( - "{}{op}{}", - snippet_opt(cx, lhs.span)?, - snippet_opt(cx, rhs.span)? - )) + let lhs_snippet = snippet_opt(cx, lhs.span)?; + let rhs_snippet = snippet_opt(cx, rhs.span)?; + + match &lhs.kind { + ExprKind::Cast(..) if binop.node == BinOpKind::Ge => { + // e.g. `(a as u64) < b`. Without the parens the `<` is + // interpreted as a start of generic arguments for `u64` + Some(format!("({lhs_snippet}){op}{rhs_snippet}")) + }, + _ => Some(format!("{lhs_snippet}{op}{rhs_snippet}")), + } }) }, ExprKind::MethodCall(path, receiver, [], _) => { diff --git a/tests/ui/nonminimal_bool.rs b/tests/ui/nonminimal_bool.rs index 38157116e910..daa7f847bef6 100644 --- a/tests/ui/nonminimal_bool.rs +++ b/tests/ui/nonminimal_bool.rs @@ -179,3 +179,10 @@ fn issue_12371(x: usize) -> bool { // Should not warn! !x != 0 } + +fn issue_12625() { + let a = 0; + let b = 0; + if !(a as u64 >= b) {} //~ ERROR: this boolean expression can be simplified + if !(a as u64 <= b) {} //~ ERROR: this boolean expression can be simplified +} diff --git a/tests/ui/nonminimal_bool.stderr b/tests/ui/nonminimal_bool.stderr index b6af06d845ae..deaa4d898b43 100644 --- a/tests/ui/nonminimal_bool.stderr +++ b/tests/ui/nonminimal_bool.stderr @@ -213,5 +213,17 @@ error: this boolean expression can be simplified LL | if !b != !c {} | ^^^^^^^^ help: try: `b != c` -error: aborting due to 29 previous errors +error: this boolean expression can be simplified + --> tests/ui/nonminimal_bool.rs:186:8 + | +LL | if !(a as u64 >= b) {} + | ^^^^^^^^^^^^^^^^ help: try: `(a as u64) < b` + +error: this boolean expression can be simplified + --> tests/ui/nonminimal_bool.rs:187:8 + | +LL | if !(a as u64 <= b) {} + | ^^^^^^^^^^^^^^^^ help: try: `a as u64 > b` + +error: aborting due to 31 previous errors