From 06175f43b3119b537b48291fa771c7396b8e92cd Mon Sep 17 00:00:00 2001 From: Samuel Tardieu Date: Tue, 17 Dec 2024 22:43:19 +0100 Subject: [PATCH] Add `is_float_literal` utility --- clippy_utils/src/lib.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs index dc3bbb417c0a..3e9429399b31 100644 --- a/clippy_utils/src/lib.rs +++ b/clippy_utils/src/lib.rs @@ -1752,6 +1752,17 @@ pub fn is_integer_literal(expr: &Expr<'_>, value: u128) -> bool { false } +/// Checks whether the given expression is a constant literal of the given value. +pub fn is_float_literal(expr: &Expr<'_>, value: f64) -> bool { + if let ExprKind::Lit(spanned) = expr.kind + && let LitKind::Float(v, _) = spanned.node + { + v.as_str().parse() == Ok(value) + } else { + false + } +} + /// Returns `true` if the given `Expr` has been coerced before. /// /// Examples of coercions can be found in the Nomicon at