Skip to content

Commit 0c2041f

Browse files
committed
float_cmp: Don't lint literal to literal comparisons
1 parent 4d23fb6 commit 0c2041f

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

clippy_lints/src/operators/float_cmp.rs

+12-16
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,17 @@ pub(crate) fn check<'tcx>(
1818
left: &'tcx Expr<'_>,
1919
right: &'tcx Expr<'_>,
2020
) {
21-
if (op == BinOpKind::Eq || op == BinOpKind::Ne)
21+
let peel_expr = |e: &'tcx Expr<'tcx>| match e.kind {
22+
ExprKind::Cast(e, _) | ExprKind::AddrOf(BorrowKind::Ref, _, e) | ExprKind::Unary(UnOp::Neg, e) => Some(e),
23+
_ => None,
24+
};
25+
26+
if matches!(op, BinOpKind::Eq | BinOpKind::Ne)
27+
&& let left = peel_hir_expr_while(left, peel_expr)
28+
&& let right = peel_hir_expr_while(right, peel_expr)
2229
&& is_float(cx, left)
30+
// Don't lint literal comparisons
31+
&& !(matches!(left.kind, ExprKind::Lit(_)) && matches!(right.kind, ExprKind::Lit(_)))
2332
// Allow comparing the results of signum()
2433
&& !(is_signum(cx, left) && is_signum(cx, right))
2534
{
@@ -41,14 +50,7 @@ pub(crate) fn check<'tcx>(
4150
return;
4251
}
4352

44-
let peel_expr = |e: &'tcx Expr<'tcx>| match e.kind {
45-
ExprKind::Cast(e, _) | ExprKind::AddrOf(BorrowKind::Ref, _, e) => Some(e),
46-
_ => None,
47-
};
48-
if config.ignore_named_constants
49-
&& (is_expr_named_const(cx, peel_hir_expr_while(left, peel_expr))
50-
|| is_expr_named_const(cx, peel_hir_expr_while(right, peel_expr)))
51-
{
53+
if config.ignore_named_constants && (is_expr_named_const(cx, left) || is_expr_named_const(cx, right)) {
5254
return;
5355
}
5456

@@ -106,16 +108,10 @@ fn is_allowed(val: &Constant<'_>) -> bool {
106108

107109
// Return true if `expr` is the result of `signum()` invoked on a float value.
108110
fn is_signum(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
109-
// The negation of a signum is still a signum
110-
if let ExprKind::Unary(UnOp::Neg, child_expr) = expr.kind {
111-
return is_signum(cx, child_expr);
112-
}
113-
114111
if let ExprKind::MethodCall(method_name, self_arg, ..) = expr.kind
115112
&& sym!(signum) == method_name.ident.name
116-
// Check that the receiver of the signum() is a float (expressions[0] is the receiver of
117-
// the method call)
118113
{
114+
// Check that the receiver of the signum() is a float
119115
return is_float(cx, self_arg);
120116
}
121117
false

tests/ui-toml/float_cmp_constant_comparisons/test.rs

+5
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@ fn main() {
1515
}
1616
let _ = f(1.0) == f(2.0);
1717
}
18+
{
19+
let _ = 1.0f32 == 2.0f32;
20+
let _ = -1.0f32 == -2.0f32;
21+
let _ = 1.0f64 == 2.0f64;
22+
}
1823
}

tests/ui-toml/float_cmp_named_constants/test.rs

+5
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@ fn main() {
1515
}
1616
let _ = f(1.0) == f(2.0);
1717
}
18+
{
19+
let _ = 1.0f32 == 2.0f32;
20+
let _ = -1.0f32 == -2.0f32;
21+
let _ = 1.0f64 == 2.0f64;
22+
}
1823
}

0 commit comments

Comments
 (0)