Created
April 18, 2016 04:28
-
-
Save Wajihulhassan/a6a70dd6910a02a064b163f57a46c2f8 to your computer and use it in GitHub Desktop.
String Equality Bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Nicholas do you think the following code should not be a reference equality.I believe it should be a value equality using "isequals" | |
As both of them are different strings. You can open the file and check it yourself. Let me know what do you think. | |
/home/wajih/cs498/wajih-fork/che/plugins/plugin-java/che-plugin-java-ext-jdt/org-eclipse-jdt-ui/src/main/java/org/eclipse/jdt/internal/core/ClasspathEntry.java:1007: warning: [StringEquality] String comparison using reference equality instead of value equality | |
if (e.getStatus().getMessage() == Messages.status_IOException) { | |
^ | |
(see http://errorprone.info/bugpattern/StringEquality) | |
Did you mean 'if (Objects.equals(e.getStatus().getMessage(), Messages.status_IOException)) {'? | |
Nicholas |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've spent some time digging through some of the related classes including CoreException, Messages, and IStatus. My impression is that the messages are loaded from some file and the developer shouldn't ever use a hardcoded string since it could potentially change.
Changing == to .equals() is unlikely to change anything, but it can potentially let developers get away with using string literals when they should be using the constant. In that case, when the message changes, the method would wrongly return the VERIFIED_OK output instead of INVALID_CLASSPATH. For this reason, I believe that it is better to have == "occasionally?" reject string literals.
I think we should hold off on this warning for now and ask the mailing list once we have a few similar warnings.