Skip to content

Commit 0aa03e1

Browse files
committed
Add is_float_literal utility
1 parent c5218d5 commit 0aa03e1

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

clippy_utils/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,6 +1655,17 @@ pub fn is_integer_literal(expr: &Expr<'_>, value: u128) -> bool {
16551655
false
16561656
}
16571657

1658+
/// Checks whether the given expression is a constant literal of the given value.
1659+
pub fn is_float_literal(expr: &Expr<'_>, value: f64) -> bool {
1660+
if let ExprKind::Lit(spanned) = expr.kind
1661+
&& let LitKind::Float(v, _) = spanned.node
1662+
{
1663+
v.as_str().parse() == Ok(value)
1664+
} else {
1665+
false
1666+
}
1667+
}
1668+
16581669
/// Returns `true` if the given `Expr` has been coerced before.
16591670
///
16601671
/// Examples of coercions can be found in the Nomicon at

0 commit comments

Comments
 (0)