Skip to content

Commit dcc678f

Browse files
committed
Add is_floating_point_integer_literal utility
1 parent 25d319d commit dcc678f

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

clippy_utils/src/lib.rs

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

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

0 commit comments

Comments
 (0)