Skip to content

Commit 38dbb0b

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

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-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_methods.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,11 @@ fn dont_warn_for_negated_partial_ord_comparison() {
109109
let _ = !(a >= b);
110110
}
111111

112+
fn issue_12625() {
113+
let a = 0;
114+
let b = 0;
115+
if (a as u64) < b {} //~ ERROR: this boolean expression can be simplified
116+
if a as u64 > b {} //~ ERROR: this boolean expression can be simplified
117+
}
118+
112119
fn main() {}

tests/ui/nonminimal_bool_methods.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,11 @@ fn dont_warn_for_negated_partial_ord_comparison() {
109109
let _ = !(a >= b);
110110
}
111111

112+
fn issue_12625() {
113+
let a = 0;
114+
let b = 0;
115+
if !(a as u64 >= b) {} //~ ERROR: this boolean expression can be simplified
116+
if !(a as u64 <= b) {} //~ ERROR: this boolean expression can be simplified
117+
}
118+
112119
fn main() {}

tests/ui/nonminimal_bool_methods.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,17 @@ error: this boolean expression can be simplified
7979
LL | if !res.is_none() {}
8080
| ^^^^^^^^^^^^^^ help: try: `res.is_some()`
8181

82-
error: aborting due to 13 previous errors
82+
error: this boolean expression can be simplified
83+
--> tests/ui/nonminimal_bool_methods.rs:115:8
84+
|
85+
LL | if !(a as u64 >= b) {}
86+
| ^^^^^^^^^^^^^^^^ help: try: `(a as u64) < b`
87+
88+
error: this boolean expression can be simplified
89+
--> tests/ui/nonminimal_bool_methods.rs:116:8
90+
|
91+
LL | if !(a as u64 <= b) {}
92+
| ^^^^^^^^^^^^^^^^ help: try: `a as u64 > b`
93+
94+
error: aborting due to 15 previous errors
8395

0 commit comments

Comments
 (0)