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

ObjectEqualsForPrimitives should not suggest to convert boxed equals on double or float values #4390

Closed
findepi opened this issue May 10, 2024 · 3 comments

Comments

@findepi
Copy link
Contributor

findepi commented May 10, 2024

https://errorprone.info/bugpattern/ObjectEqualsForPrimitives check is described as "performance" related, so if I follow it, I would not expect the semantics to change.
Currently, ObjectEqualsForPrimitives applies to all primitives and enums.
However, double and float values should be exempted from the check:

double a = Double.NaN;
double b = Double.NaN;

System.out.println(Objects.equals(a, b)); // true; flagged by ObjectEqualsForPrimitives check
System.out.println(a == b); // false

In current master of https://github.com/trinodb/trino, the ObjectEqualsForPrimitives triggers typically in equals() implementations. While most double/float values are unlikely to be NaN, this is not something and equals() implementation should be concerned about.

It's probably worth noting that equals generated for Java record classes also compares NaN values as equal (as if Objects.equals was used)

@ksobolew
Copy link

Is Double.compare(a, b) == 0 a valid substitute? No boxing, but perhaps a more expensive operation.

@findepi
Copy link
Contributor Author

findepi commented May 10, 2024

@ksobolew you're right.
So the check is probably valid also for float/double, but it maybe its message could be tailored down to prevent people like me from improving the code incorrectly.

@findepi findepi closed this as not planned Won't fix, can't repro, duplicate, stale May 10, 2024
@findepi
Copy link
Contributor Author

findepi commented May 10, 2024

the request here is invalid.
the idea to improve error message logged as new issue -- #4392

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants