Skip to content

Commit e9c2d82

Browse files
committed
Add is_floating_point_integer_literal utility
1 parent 8ea395e commit e9c2d82

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
@@ -1638,6 +1638,18 @@ pub fn is_integer_literal(expr: &Expr<'_>, value: u128) -> bool {
16381638
false
16391639
}
16401640

1641+
/// Checks whether the given expression is a constant integer of the given value
1642+
/// as a floating point literal.
1643+
pub fn is_floating_point_integer_literal(expr: &Expr<'_>, value: u32) -> bool {
1644+
if let ExprKind::Lit(spanned) = expr.kind
1645+
&& let LitKind::Float(v, _) = spanned.node
1646+
{
1647+
v.as_str().parse() == Ok(f64::from(value))
1648+
} else {
1649+
false
1650+
}
1651+
}
1652+
16411653
/// Returns `true` if the given `Expr` has been coerced before.
16421654
///
16431655
/// Examples of coercions can be found in the Nomicon at

0 commit comments

Comments
 (0)