Skip to content

Commit 3002063

Browse files
committed
Add is_float_literal utility
1 parent 2220806 commit 3002063

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

clippy_utils/src/lib.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1648,6 +1648,17 @@ pub fn is_integer_literal(expr: &Expr<'_>, value: u128) -> bool {
16481648
false
16491649
}
16501650

1651+
/// Checks whether the given expression is a constant literal of the given value.
1652+
pub fn is_float_literal(expr: &Expr<'_>, value: f64) -> bool {
1653+
if let ExprKind::Lit(spanned) = expr.kind
1654+
&& let LitKind::Float(v, _) = spanned.node
1655+
{
1656+
v.as_str().parse() == Ok(value)
1657+
} else {
1658+
false
1659+
}
1660+
}
1661+
16511662
/// Returns `true` if the given `Expr` has been coerced before.
16521663
///
16531664
/// Examples of coercions can be found in the Nomicon at

0 commit comments

Comments
 (0)