Skip to content

Commit

Permalink
fix incorrect suggestion for !(a as type >= b)
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Apr 3, 2024
1 parent f9f854f commit c3e85f3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
16 changes: 11 additions & 5 deletions clippy_lints/src/booleans.rs
Expand Up @@ -346,11 +346,17 @@ fn simplify_not(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<String> {
_ => 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, [], _) => {
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/nonminimal_bool.rs
Expand Up @@ -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
}
14 changes: 13 additions & 1 deletion tests/ui/nonminimal_bool.stderr
Expand Up @@ -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

0 comments on commit c3e85f3

Please sign in to comment.