eq_op
lint in clippy is confusing for floats
#12222
Labels
C-bug
Category: Clippy is not doing the correct thing
eq_op
lint in clippy is confusing for floats
#12222
Summary
Clippy suggests the
eq_op
lint in cases where it's not clear how to fix the underlying problem.I was looking at
libm
's code (https://github.com/rust-lang/libm/tree/cb2ffdf5435d3302c97a27c8ce7de48e214de037). For this block (https://github.com/rust-lang/libm/blob/cb2ffdf5435d3302c97a27c8ce7de48e214de037/src/math/sinf.rs#L81-L83), clippy shows an error, because this triggers theeq_op
lint: https://rust-lang.github.io/rust-clippy/master/index.html#/eq_op.In my mind, this makes sense, since
x - x
should always be 0. But, if I swap the linereturn x - x;
toreturn 0.0;
, tests fail immediately in the repo.as far as I can tell, the guard clause of
if ix >= 0x7f800000 {
means that x's bitwise representation is infinity or NaN, so the return can bereturn f32::NAN
instead ofreturn x - x;
. This works because NaN - NaN and inf - inf are NaN: (https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=b060559e29d69eca8c424429b908ad27), and thus the tests pass again.Is there a way to issue better diagnostics in this case / have the lint explain some common pitfalls with this lint when applied to floats? In the case of
libm
,return x - x;
is intentional because it's basically doingx.is_nan() || x.is_infinite()
before doing the operation, soreturn x - x;
looks to be totally fine.Reproducer
No response
Version
Additional Labels
No response
The text was updated successfully, but these errors were encountered: