Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix incorrect suggestion for !(a as type >= b) #12626

Merged
merged 1 commit into from Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 12 additions & 5 deletions clippy_lints/src/booleans.rs
Expand Up @@ -346,11 +346,18 @@ 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)?;

if !(lhs_snippet.starts_with('(') && lhs_snippet.ends_with(')')) {
if let (ExprKind::Cast(..), BinOpKind::Ge) = (&lhs.kind, binop.node) {
// e.g. `(a as u64) < b`. Without the parens the `<` is
// interpreted as a start of generic arguments for `u64`
return Some(format!("({lhs_snippet}){op}{rhs_snippet}"));
}
}

Some(format!("{lhs_snippet}{op}{rhs_snippet}"))
})
},
ExprKind::MethodCall(path, receiver, [], _) => {
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/nonminimal_bool_methods.fixed
Expand Up @@ -109,4 +109,12 @@ fn dont_warn_for_negated_partial_ord_comparison() {
let _ = !(a >= b);
}

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
if a as u64 > b {} //~ ERROR: this boolean expression can be simplified
}

fn main() {}
8 changes: 8 additions & 0 deletions tests/ui/nonminimal_bool_methods.rs
Expand Up @@ -109,4 +109,12 @@ fn dont_warn_for_negated_partial_ord_comparison() {
let _ = !(a >= b);
}

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
if !(a as u64 <= b) {} //~ ERROR: this boolean expression can be simplified
}

fn main() {}
20 changes: 19 additions & 1 deletion tests/ui/nonminimal_bool_methods.stderr
Expand Up @@ -79,5 +79,23 @@ error: this boolean expression can be simplified
LL | if !res.is_none() {}
| ^^^^^^^^^^^^^^ help: try: `res.is_some()`

error: aborting due to 13 previous errors
error: this boolean expression can be simplified
--> tests/ui/nonminimal_bool_methods.rs:115:8
|
LL | if !(a as u64 >= b) {}
| ^^^^^^^^^^^^^^^^ help: try: `(a as u64) < b`

error: this boolean expression can be simplified
--> tests/ui/nonminimal_bool_methods.rs:116:8
|
LL | if !((a as u64) >= b) {}
| ^^^^^^^^^^^^^^^^^^ help: try: `(a as u64) < b`

error: this boolean expression can be simplified
--> tests/ui/nonminimal_bool_methods.rs:117:8
|
LL | if !(a as u64 <= b) {}
| ^^^^^^^^^^^^^^^^ help: try: `a as u64 > b`

error: aborting due to 16 previous errors