|
| 1 | +use clippy_utils::{ |
| 2 | + consts::constant, diagnostics::span_lint_and_sugg, is_from_proc_macro, path_to_local, source::snippet_opt, |
| 3 | +}; |
| 4 | +use rustc_errors::Applicability; |
| 5 | +use rustc_hir::{BinOpKind, Expr, ExprKind}; |
| 6 | +use rustc_lint::{LateContext, LateLintPass, LintContext}; |
| 7 | +use rustc_middle::lint::in_external_macro; |
| 8 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 9 | + |
| 10 | +declare_clippy_lint! { |
| 11 | + /// ### What it does |
| 12 | + /// Checks for `x == <float>::INFINITY || x == <float>::NEG_INFINITY`. |
| 13 | + /// |
| 14 | + /// ### Why is this bad? |
| 15 | + /// This should use the dedicated method instead, `is_infinite`. |
| 16 | + /// |
| 17 | + /// ### Example |
| 18 | + /// ```rust |
| 19 | + /// # let x = 1.0f32; |
| 20 | + /// if x == f32::INFINITY || x == f32::NEG_INFINITY {} |
| 21 | + /// ``` |
| 22 | + /// Use instead: |
| 23 | + /// ```rust |
| 24 | + /// # let x = 1.0f32; |
| 25 | + /// if x.is_infinite() {} |
| 26 | + /// ``` |
| 27 | + #[clippy::version = "1.72.0"] |
| 28 | + pub MANUAL_IS_INFINITE, |
| 29 | + style, |
| 30 | + "use dedicated method to check if a float is infinite" |
| 31 | +} |
| 32 | +declare_clippy_lint! { |
| 33 | + /// ### What it does |
| 34 | + /// Checks for `x != <float>::INFINITY && x != <float>::NEG_INFINITY`. |
| 35 | + /// |
| 36 | + /// ### Why is this bad? |
| 37 | + /// This should use the dedicated method instead, `is_finite`. |
| 38 | + /// |
| 39 | + /// ### Example |
| 40 | + /// ```rust |
| 41 | + /// # let x = 1.0f32; |
| 42 | + /// if x != f32::INFINITY && x != f32::NEG_INFINITY {} |
| 43 | + /// ``` |
| 44 | + /// Use instead: |
| 45 | + /// ```rust |
| 46 | + /// # let x = 1.0f32; |
| 47 | + /// if x.is_finite() {} |
| 48 | + /// ``` |
| 49 | + #[clippy::version = "1.72.0"] |
| 50 | + pub MANUAL_IS_FINITE, |
| 51 | + style, |
| 52 | + "use dedicated method to check if a float is finite" |
| 53 | +} |
| 54 | +declare_lint_pass!(ManualFloatMethods => [MANUAL_IS_INFINITE, MANUAL_IS_FINITE]); |
| 55 | + |
| 56 | +impl<'tcx> LateLintPass<'tcx> for ManualFloatMethods { |
| 57 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { |
| 58 | + if !in_external_macro(cx.sess(), expr.span) |
| 59 | + && let ExprKind::Binary(kind, lhs, rhs) = expr.kind |
| 60 | + && let ExprKind::Binary(lhs_kind, lhs_lhs, lhs_rhs) = lhs.kind |
| 61 | + && let ExprKind::Binary(rhs_kind, rhs_lhs, rhs_rhs) = rhs.kind |
| 62 | + && let (operands, consts) = [lhs_lhs, lhs_rhs, rhs_lhs, rhs_rhs] |
| 63 | + .into_iter() |
| 64 | + .partition::<Vec<&Expr<'_>>, _>(|i| path_to_local(i).is_some()) |
| 65 | + && let [first, second] = &*operands |
| 66 | + && let Some([const_1, const_2]) = consts |
| 67 | + .into_iter() |
| 68 | + .map(|i| constant(cx, cx.typeck_results(), i).and_then(|c| c.to_bits())) |
| 69 | + .collect::<Option<Vec<_>>>() |
| 70 | + .as_deref() |
| 71 | + && path_to_local(first).is_some_and(|f| path_to_local(second).is_some_and(|s| f == s)) |
| 72 | + && (is_infinity(*const_1) && is_neg_infinity(*const_2) |
| 73 | + || is_neg_infinity(*const_1) && is_infinity(*const_2)) |
| 74 | + && let Some(local_snippet) = snippet_opt(cx, first.span) |
| 75 | + && !is_from_proc_macro(cx, expr) |
| 76 | + { |
| 77 | + let (msg, lint, sugg_fn) = match (kind.node, lhs_kind.node, rhs_kind.node) { |
| 78 | + (BinOpKind::Or, BinOpKind::Eq, BinOpKind::Eq) => { |
| 79 | + ("manually checking if a float is infinite", MANUAL_IS_INFINITE, "is_infinite") |
| 80 | + }, |
| 81 | + (BinOpKind::And, BinOpKind::Ne, BinOpKind::Ne) => { |
| 82 | + ("manually checking if a float is finite", MANUAL_IS_FINITE, "is_finite") |
| 83 | + }, |
| 84 | + _ => return, |
| 85 | + }; |
| 86 | + |
| 87 | + span_lint_and_sugg( |
| 88 | + cx, |
| 89 | + lint, |
| 90 | + expr.span, |
| 91 | + msg, |
| 92 | + "try", |
| 93 | + format!("{local_snippet}.{sugg_fn}()"), |
| 94 | + Applicability::MachineApplicable, |
| 95 | + ); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +fn is_infinity(bits: u128) -> bool { |
| 101 | + bits == 0x7f80_0000 || bits == 0x7ff0_0000_0000_0000 |
| 102 | +} |
| 103 | + |
| 104 | +fn is_neg_infinity(bits: u128) -> bool { |
| 105 | + bits == 0xff80_0000 || bits == 0xfff0_0000_0000_0000 |
| 106 | +} |
0 commit comments