rust-lang / rust-lang/rust-clippy

eq_op false positive with floats

Open
#1,466 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Consider the following function:

fn nan_test() -> bool {
	let v = std::f32::NAN;

	v != v //warning: equal expressions as operands to `!=`
}

The eq_op warning is wrong here since floats only implement PartialOrd and doing v != v allows me to detect them in a generic context:

fn max<T: PartialOrd>(v1: T, v2: T) -> T {
	if v2 != v2 { v2 }
	else if v2 > v1 { v2 } else { v1 }
}

Maybe a solution is to check whether the result of the expression returns something that implements Ord or not?

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 the eq_op lint and reproduce the warning using the provided nan_test and generic max examples. Investigate how operand types and PartialOrd versus Ord are identified; done means valid float NaN checks no longer trigger the false positive while ordinary equal-expression warnings remain covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.