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

RFE: issue (optional) warning when '==' is used on wrapper types #2176

Open
dashorst opened this issue Mar 18, 2024 · 5 comments · May be fixed by #2188
Open

RFE: issue (optional) warning when '==' is used on wrapper types #2176

dashorst opened this issue Mar 18, 2024 · 5 comments · May be fixed by #2188
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@dashorst
Copy link

dashorst commented Mar 18, 2024

I would like that Eclipse (similar to IDEA) would issue an optional warning when one uses the following construct:

Long x1 = 12345L;
Long x2 = 67890L;
if(x1 == x2)
    System.out.println("Equal");
else
    System.out.println("Not equal");

Because (as apparently we don't all know) Long is a wrapper class and using == only works for Long values between -127 and 127 due to caching (similarly for the other wrapper types).

This would at least uncover one such instance in our code base, of which I suspect to find a greater number alas.

@iloveeclipse iloveeclipse added enhancement New feature or request needinfo Further information is requested labels Mar 18, 2024
@iloveeclipse
Copy link
Member

iloveeclipse commented Mar 18, 2024

What does javac report?
Note, static code analysis of semantically correct and compilable code is not necessarily what compiler is supposed to do.

@dashorst dashorst changed the title RFE: issue warning when '==' is used on wrapper types RFE: issue (optional) warning when '==' is used on wrapper types Mar 18, 2024
@dashorst
Copy link
Author

While I sympathise with the "don't bloat the compiler with too much work" (that would make it slower etc), I doubt that the ECJ "Method can be static" optional problem is something a compiler should do as well, but causes fewer programming errors.

In any case, javac (17) doesn't issue a warning for either using == on Longs nor Strings.

I don't propose to emit a warning always, but make it an option similar to x == x (which isn't flagged by javac either).

@iloveeclipse iloveeclipse added help wanted Extra attention is needed and removed needinfo Further information is requested labels Mar 18, 2024
@iloveeclipse
Copy link
Member

I don't propose to emit a warning always, but make it an option similar to x == x (which isn't flagged by javac either).

Sure. If you (or anyone else) is interested to provide this feature, feel free to contribute.

dashorst added a commit to dashorst/eclipse.jdt.core that referenced this issue Mar 20, 2024
When comparing wrapper types or Strings using '==' this is done by reference instead of value.
Usually this is in error (e.g. Long only caches values between -127 and +127). By flagging this
as an optional warning or even error (configurable) these cases can be easily weeded out.

Fixes eclipse-jdt#2176
@dashorst dashorst linked a pull request Mar 20, 2024 that will close this issue
3 tasks
dashorst added a commit to dashorst/eclipse.jdt.core that referenced this issue Mar 20, 2024
When comparing wrapper types or Strings using '==' this is done by
reference instead of value. Usually this is in error (e.g. Long only
caches values between -127 and +127). By flagging this as an optional
warning or even error (configurable) these cases can be easily weeded
out.

Fixes eclipse-jdt#2176
dashorst added a commit to dashorst/eclipse.jdt.core that referenced this issue Mar 20, 2024
When comparing wrapper types or Strings using '==' this is done by
reference instead of value. Usually this is in error (e.g. Long only
caches values between -127 and +127). By flagging this as an optional
warning or even error (configurable) these cases can be easily weeded
out.

Fixes eclipse-jdt#2176
dashorst added a commit to dashorst/eclipse.jdt.core that referenced this issue Mar 20, 2024
When comparing wrapper types or Strings using '==' this is done by
reference instead of value. Usually this is in error (e.g. Long only
caches values between -127 and +127). By flagging this as an optional
warning or even error (configurable) these cases can be easily weeded
out.

Fixes eclipse-jdt#2176
@dashorst
Copy link
Author

Well, here you go!

dashorst added a commit to dashorst/eclipse.jdt.core that referenced this issue Mar 20, 2024
When comparing wrapper types or Strings using '==' this is done by
reference instead of value. Usually this is in error (e.g. Long only
caches values between -127 and +127). By flagging this as an optional
warning or even error (configurable) these cases can be easily weeded
out.

Fixes eclipse-jdt#2176
dashorst added a commit to dashorst/eclipse.jdt.core that referenced this issue Mar 27, 2024
When comparing wrapper types or Strings using '==' this is done by
reference instead of value. Usually this is in error (e.g. Long only
caches values between -127 and +127). By flagging this as an optional
warning or even error (configurable) these cases can be easily weeded
out.

Fixes eclipse-jdt#2176
@dashorst
Copy link
Author

The PR builds correctly, I'd love some feedback on whether it is complete, hits the correct spots and suitability for adoption.

stephan-herrmann pushed a commit to dashorst/eclipse.jdt.core that referenced this issue Apr 1, 2024
When comparing wrapper types or Strings using '==' this is done by
reference instead of value. Usually this is in error (e.g. Long only
caches values between -127 and +127). By flagging this as an optional
warning or even error (configurable) these cases can be easily weeded
out.

Fixes eclipse-jdt#2176
dashorst added a commit to dashorst/eclipse.jdt.core that referenced this issue Apr 2, 2024
When comparing wrapper types or Strings using '==' this is done by
reference instead of value. Usually this is in error (e.g. Long only
caches values between -127 and +127). By flagging this as an optional
warning or even error (configurable) these cases can be easily weeded
out.

Fixes eclipse-jdt#2176
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants