-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
eq_op
lint in clippy is confusing for floats
#12222
Comments
currently there's a note suggest using However, it only apears with
this lint will need some extra explaination. One more thing, since the |
I think taking the note from the not equal operand and also applying it to subtraction should work (in this case). That would've left me less confused when I tried to replace the
As a follow-up, I wonder what other lints could be applied to floats or if this lint could be applied in other cases. Floats are pretty tricky to deal with (case in point, I didn't understand them) and it would be nice to have some lints in other cases. If adding this case to clippy makes sense, I can get started working on it. |
Yes! In general, the lint should be AFAIK, it's impossible for a single lint to have multiple levels, so it should be split in 2:
That wouldn't be a breaking-change, if it wasn't for |
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: