Skip to content
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 false positive with floats #1466

Open
lukebitts opened this issue Jan 22, 2017 · 5 comments
Open

eq_op false positive with floats #1466

lukebitts opened this issue Jan 22, 2017 · 5 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@lukebitts
Copy link

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?

@llogiq
Copy link
Contributor

llogiq commented Jan 22, 2017

I'd rather specialize reporting for floats and suggest v.is_nan() instead.

@lukebitts
Copy link
Author

I can't use is_nan in a generic context. I could bind the parameter to num::Float though.

@llogiq
Copy link
Contributor

llogiq commented Jan 22, 2017

True, but that's a fairly niche use case. Maybe it's best to simply #[allow(eq_op)] in your max function?

@lukebitts
Copy link
Author

Actually num::Float and is_nan turned out to be a better abstraction for my project. So I guess the warning was right

@BusyJay
Copy link

BusyJay commented Jan 21, 2021

#6167 makes eq_op lint on assert families, but when users choose to implement PartialEq for a custom type, they may use assert_eq to test if it's correctly implemented, which leads to the lint error.

@camsteffen camsteffen changed the title eq_op false positive eq_op false positive with floats Nov 13, 2021
@camsteffen camsteffen added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Nov 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

4 participants