Skip to content

Commit 06175f4

Browse files
committed
Add is_float_literal utility
1 parent 715d3f9 commit 06175f4

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

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

0 commit comments

Comments
 (0)