rust-lang / rust-lang/rust-clippy

`eq_op` lint in clippy is confusing for floats

Open
#12,222 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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 the eq_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 line return x - x; to return 0.0;, tests fail immediately in the repo.

thread 'sinf_matches_musl' panicked at /home/takashi/.build/debug/build/libm-33ae80df27fee36a/out/musl-tests.rs:71754:9:
INPUT: [2139095040] EXPECTED: [4290772992] ACTUAL 0.0
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

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 be return f32::NAN instead of return 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 doing x.is_nan() || x.is_infinite() before doing the operation, so return x - x; looks to be totally fine.

Reproducer

No response

Version
rustc 1.75.0 (82e1608df 2023-12-21)
binary: rustc
commit-hash: 82e1608dfa6e0b5569232559e3d385fea5a93112
commit-date: 2023-12-21
host: x86_64-unknown-linux-gnu
release: 1.75.0
LLVM version: 17.0.6
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Clippy's eq_op lint documentation and the libm example in src/math/sinf.rs at lines 81-83. Compare the lint's behavior with the cited sinf tests and the float cases described in the issue. Done means the diagnostic gives useful guidance or context for intentional float uses without losing the existing lint coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.