|
| 1 | +use clippy_utils::{ |
| 2 | + consts::{constant, Constant}, |
| 3 | + diagnostics::span_lint_and_then, |
| 4 | + is_from_proc_macro, path_to_local, |
| 5 | + source::snippet_opt, |
| 6 | +}; |
| 7 | +use rustc_errors::Applicability; |
| 8 | +use rustc_hir::{BinOpKind, Expr, ExprKind}; |
| 9 | +use rustc_lint::{LateContext, LateLintPass, Lint, LintContext}; |
| 10 | +use rustc_middle::lint::in_external_macro; |
| 11 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 12 | + |
| 13 | +declare_clippy_lint! { |
| 14 | + /// ### What it does |
| 15 | + /// Checks for manual `is_infinite` reimplementations |
| 16 | + /// (i.e., `x == <float>::INFINITY || x == <float>::NEG_INFINITY`). |
| 17 | + /// |
| 18 | + /// ### Why is this bad? |
| 19 | + /// The method `is_infinite` is shorter and more readable. |
| 20 | + /// |
| 21 | + /// ### Example |
| 22 | + /// ```rust |
| 23 | + /// # let x = 1.0f32; |
| 24 | + /// if x == f32::INFINITY || x == f32::NEG_INFINITY {} |
| 25 | + /// ``` |
| 26 | + /// Use instead: |
| 27 | + /// ```rust |
| 28 | + /// # let x = 1.0f32; |
| 29 | + /// if x.is_infinite() {} |
| 30 | + /// ``` |
| 31 | + #[clippy::version = "1.72.0"] |
| 32 | + pub MANUAL_IS_INFINITE, |
| 33 | + style, |
| 34 | + "use dedicated method to check if a float is infinite" |
| 35 | +} |
| 36 | +declare_clippy_lint! { |
| 37 | + /// ### What it does |
| 38 | + /// Checks for manual `is_finite` reimplementations |
| 39 | + /// (i.e., `x != <float>::INFINITY && x != <float>::NEG_INFINITY`). |
| 40 | + /// |
| 41 | + /// ### Why is this bad? |
| 42 | + /// The method `is_finite` is shorter and more readable. |
| 43 | + /// |
| 44 | + /// ### Example |
| 45 | + /// ```rust |
| 46 | + /// # let x = 1.0f32; |
| 47 | + /// if x != f32::INFINITY && x != f32::NEG_INFINITY {} |
| 48 | + /// if x.abs() < f32::INFINITY {} |
| 49 | + /// ``` |
| 50 | + /// Use instead: |
| 51 | + /// ```rust |
| 52 | + /// # let x = 1.0f32; |
| 53 | + /// if x.is_finite() {} |
| 54 | + /// if x.is_finite() {} |
| 55 | + /// ``` |
| 56 | + #[clippy::version = "1.72.0"] |
| 57 | + pub MANUAL_IS_FINITE, |
| 58 | + style, |
| 59 | + "use dedicated method to check if a float is finite" |
| 60 | +} |
| 61 | +declare_lint_pass!(ManualFloatMethods => [MANUAL_IS_INFINITE, MANUAL_IS_FINITE]); |
| 62 | + |
| 63 | +#[derive(Clone, Copy)] |
| 64 | +enum Variant { |
| 65 | + ManualIsInfinite, |
| 66 | + ManualIsFinite, |
| 67 | +} |
| 68 | + |
| 69 | +impl Variant { |
| 70 | + pub fn lint(self) -> &'static Lint { |
| 71 | + match self { |
| 72 | + Self::ManualIsInfinite => MANUAL_IS_INFINITE, |
| 73 | + Self::ManualIsFinite => MANUAL_IS_FINITE, |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + pub fn msg(self) -> &'static str { |
| 78 | + match self { |
| 79 | + Self::ManualIsInfinite => "manually checking if a float is infinite", |
| 80 | + Self::ManualIsFinite => "manually checking if a float is finite", |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +impl<'tcx> LateLintPass<'tcx> for ManualFloatMethods { |
| 86 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { |
| 87 | + if !in_external_macro(cx.sess(), expr.span) |
| 88 | + && (!cx.param_env.is_const() || cx.tcx.features().active(sym!(const_float_classify))) |
| 89 | + && let ExprKind::Binary(kind, lhs, rhs) = expr.kind |
| 90 | + && let ExprKind::Binary(lhs_kind, lhs_lhs, lhs_rhs) = lhs.kind |
| 91 | + && let ExprKind::Binary(rhs_kind, rhs_lhs, rhs_rhs) = rhs.kind |
| 92 | + // Checking all possible scenarios using a function would be a hopeless task, as we have |
| 93 | + // 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() |
| 103 | + && path_to_local(first).is_some_and(|f| path_to_local(second).is_some_and(|s| f == s)) |
| 104 | + // The actual infinity check, we also allow `NEG_INFINITY` before` INFINITY` just in |
| 105 | + // 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)) |
| 108 | + && !is_from_proc_macro(cx, expr) |
| 109 | + && let Some(local_snippet) = snippet_opt(cx, first.span) |
| 110 | + { |
| 111 | + let variant = match (kind.node, lhs_kind.node, rhs_kind.node) { |
| 112 | + (BinOpKind::Or, BinOpKind::Eq, BinOpKind::Eq) => Variant::ManualIsInfinite, |
| 113 | + (BinOpKind::And, BinOpKind::Ne, BinOpKind::Ne) => Variant::ManualIsFinite, |
| 114 | + _ => return, |
| 115 | + }; |
| 116 | + |
| 117 | + span_lint_and_then( |
| 118 | + cx, |
| 119 | + variant.lint(), |
| 120 | + expr.span, |
| 121 | + variant.msg(), |
| 122 | + |diag| { |
| 123 | + match variant { |
| 124 | + Variant::ManualIsInfinite => { |
| 125 | + diag.span_suggestion( |
| 126 | + expr.span, |
| 127 | + "use the dedicated method instead", |
| 128 | + format!("{local_snippet}.is_infinite()"), |
| 129 | + Applicability::MachineApplicable, |
| 130 | + ); |
| 131 | + }, |
| 132 | + Variant::ManualIsFinite => { |
| 133 | + // TODO: There's probably some better way to do this, i.e., create |
| 134 | + // multiple suggestions with notes between each of them |
| 135 | + diag.span_suggestion_verbose( |
| 136 | + expr.span, |
| 137 | + "use the dedicated method instead", |
| 138 | + format!("{local_snippet}.is_finite()"), |
| 139 | + Applicability::MaybeIncorrect, |
| 140 | + ) |
| 141 | + .span_suggestion_verbose( |
| 142 | + expr.span, |
| 143 | + "this will alter how it handles NaN; if that is a problem, use instead", |
| 144 | + format!("{local_snippet}.is_finite() || {local_snippet}.is_nan()"), |
| 145 | + Applicability::MaybeIncorrect, |
| 146 | + ) |
| 147 | + .span_suggestion_verbose( |
| 148 | + expr.span, |
| 149 | + "or, for conciseness", |
| 150 | + format!("!{local_snippet}.is_infinite()"), |
| 151 | + Applicability::MaybeIncorrect, |
| 152 | + ); |
| 153 | + }, |
| 154 | + } |
| 155 | + }, |
| 156 | + ); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +fn is_infinity(constant: &Constant<'_>) -> bool { |
| 162 | + match constant { |
| 163 | + Constant::F32(float) => *float == f32::INFINITY, |
| 164 | + Constant::F64(float) => *float == f64::INFINITY, |
| 165 | + _ => false, |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +fn is_neg_infinity(constant: &Constant<'_>) -> bool { |
| 170 | + match constant { |
| 171 | + Constant::F32(float) => *float == f32::NEG_INFINITY, |
| 172 | + Constant::F64(float) => *float == f64::NEG_INFINITY, |
| 173 | + _ => false, |
| 174 | + } |
| 175 | +} |
0 commit comments