Skip to content

Commit 95348ad

Browse files
committed
manual_float_methods:
* Check HIR tree first. * Use `partition_in_place`.
1 parent b9ba340 commit 95348ad

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

clippy_lints/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![feature(f16)]
66
#![feature(if_let_guard)]
77
#![feature(iter_intersperse)]
8+
#![feature(iter_partition_in_place)]
89
#![feature(let_chains)]
910
#![feature(never_type)]
1011
#![feature(rustc_private)]

clippy_lints/src/manual_float_methods.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,29 +82,26 @@ impl Variant {
8282

8383
impl<'tcx> LateLintPass<'tcx> for ManualFloatMethods {
8484
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
85-
if !in_external_macro(cx.sess(), expr.span)
86-
&& (
87-
matches!(cx.tcx.constness(cx.tcx.hir().enclosing_body_owner(expr.hir_id)), Constness::NotConst)
88-
|| cx.tcx.features().declared(sym!(const_float_classify))
89-
) && let ExprKind::Binary(kind, lhs, rhs) = expr.kind
85+
if let ExprKind::Binary(kind, lhs, rhs) = expr.kind
9086
&& let ExprKind::Binary(lhs_kind, lhs_lhs, lhs_rhs) = lhs.kind
9187
&& let ExprKind::Binary(rhs_kind, rhs_lhs, rhs_rhs) = rhs.kind
9288
// Checking all possible scenarios using a function would be a hopeless task, as we have
9389
// 16 possible alignments of constants/operands. For now, let's use `partition`.
94-
&& let (operands, constants) = [lhs_lhs, lhs_rhs, rhs_lhs, rhs_rhs]
95-
.into_iter()
96-
.partition::<Vec<&Expr<'_>>, _>(|i| path_to_local(i).is_some())
97-
&& let [first, second] = &*operands
98-
&& let Some([const_1, const_2]) = constants
99-
.into_iter()
100-
.map(|i| constant(cx, cx.typeck_results(), i))
101-
.collect::<Option<Vec<_>>>()
102-
.as_deref()
90+
&& let mut exprs = [lhs_lhs, lhs_rhs, rhs_lhs, rhs_rhs]
91+
&& exprs.iter_mut().partition_in_place(|i| path_to_local(i).is_some()) == 2
92+
&& !in_external_macro(cx.sess(), expr.span)
93+
&& (
94+
matches!(cx.tcx.constness(cx.tcx.hir().enclosing_body_owner(expr.hir_id)), Constness::NotConst)
95+
|| cx.tcx.features().declared(sym!(const_float_classify))
96+
)
97+
&& let [first, second, const_1, const_2] = exprs
98+
&& let Some(const_1) = constant(cx, cx.typeck_results(), const_1)
99+
&& let Some(const_2) = constant(cx, cx.typeck_results(), const_2)
103100
&& path_to_local(first).is_some_and(|f| path_to_local(second).is_some_and(|s| f == s))
104101
// The actual infinity check, we also allow `NEG_INFINITY` before` INFINITY` just in
105102
// case somebody does that for some reason
106-
&& (is_infinity(const_1) && is_neg_infinity(const_2)
107-
|| is_neg_infinity(const_1) && is_infinity(const_2))
103+
&& (is_infinity(&const_1) && is_neg_infinity(&const_2)
104+
|| is_neg_infinity(&const_1) && is_infinity(&const_2))
108105
&& let Some(local_snippet) = snippet_opt(cx, first.span)
109106
{
110107
let variant = match (kind.node, lhs_kind.node, rhs_kind.node) {

0 commit comments

Comments
 (0)