Skip to content

Commit c004be4

Browse files
authored
Fix nonminimal_bool wrongly unmangled terms (#16017)
Closes #16014 changelog: [`nonminimal_bool`] fix wrongly unmangled terms
2 parents 36324e8 + c100a08 commit c004be4

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

clippy_lints/src/booleans.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
33
use clippy_utils::higher::has_let_expr;
44
use clippy_utils::msrvs::{self, Msrv};
55
use clippy_utils::res::MaybeDef;
6-
use clippy_utils::source::SpanRangeExt;
6+
use clippy_utils::source::{SpanRangeExt, snippet_with_context};
77
use clippy_utils::sugg::Sugg;
88
use clippy_utils::ty::implements_trait;
99
use clippy_utils::{eq_expr_value, sym};
@@ -415,19 +415,20 @@ fn simplify_not(cx: &LateContext<'_>, curr_msrv: Msrv, expr: &Expr<'_>) -> Optio
415415
BinOpKind::Ge => Some(" < "),
416416
_ => None,
417417
}
418-
.and_then(|op| {
419-
let lhs_snippet = lhs.span.get_source_text(cx)?;
420-
let rhs_snippet = rhs.span.get_source_text(cx)?;
418+
.map(|op| {
419+
let mut app = Applicability::MachineApplicable;
420+
let (lhs_snippet, _) = snippet_with_context(cx, lhs.span, SyntaxContext::root(), "", &mut app);
421+
let (rhs_snippet, _) = snippet_with_context(cx, rhs.span, SyntaxContext::root(), "", &mut app);
421422

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

430-
Some(format!("{lhs_snippet}{op}{rhs_snippet}"))
431+
format!("{lhs_snippet}{op}{rhs_snippet}")
431432
})
432433
},
433434
ExprKind::MethodCall(path, receiver, args, _) => {

tests/ui/nonminimal_bool_methods.fixed

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,9 @@ fn issue_13436() {
242242
}
243243
}
244244

245+
fn issue16014() {
246+
(vec![1, 2, 3] > vec![1, 2, 3, 3]);
247+
//~^ nonminimal_bool
248+
}
249+
245250
fn main() {}

tests/ui/nonminimal_bool_methods.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,9 @@ fn issue_13436() {
242242
}
243243
}
244244

245+
fn issue16014() {
246+
!(vec![1, 2, 3] <= vec![1, 2, 3, 3]);
247+
//~^ nonminimal_bool
248+
}
249+
245250
fn main() {}

tests/ui/nonminimal_bool_methods.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,5 +247,11 @@ error: this boolean expression can be simplified
247247
LL | _ = !opt.is_none_or(|x| x.is_err());
248248
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `opt.is_some_and(|x| x.is_ok())`
249249

250-
error: aborting due to 41 previous errors
250+
error: this boolean expression can be simplified
251+
--> tests/ui/nonminimal_bool_methods.rs:246:5
252+
|
253+
LL | !(vec![1, 2, 3] <= vec![1, 2, 3, 3]);
254+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `(vec![1, 2, 3] > vec![1, 2, 3, 3])`
255+
256+
error: aborting due to 42 previous errors
251257

0 commit comments

Comments
 (0)