Skip to content

Commit 00042a8

Browse files
committed
Add is_float_literal utility
1 parent a8b1782 commit 00042a8

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
@@ -1750,6 +1750,17 @@ pub fn is_integer_literal(expr: &Expr<'_>, value: u128) -> bool {
17501750
false
17511751
}
17521752

1753+
/// Checks whether the given expression is a constant literal of the given value.
1754+
pub fn is_float_literal(expr: &Expr<'_>, value: f64) -> bool {
1755+
if let ExprKind::Lit(spanned) = expr.kind
1756+
&& let LitKind::Float(v, _) = spanned.node
1757+
{
1758+
v.as_str().parse() == Ok(value)
1759+
} else {
1760+
false
1761+
}
1762+
}
1763+
17531764
/// Returns `true` if the given `Expr` has been coerced before.
17541765
///
17551766
/// Examples of coercions can be found in the Nomicon at

0 commit comments

Comments
 (0)