This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.
It would be nice if the hint which warns of comparing Strings using == or != provided a fix which changed the expression to use the equals() method instead. Original expression: (str1 == str2) Fix: (str1.equals(str2)) A truly equivalent fix would actually ensure that the new expression always evaluates to the same value as the original (and avoids the potential NPE). Such a Fix could incorporate a ternary null check. For those who dislike ternary operators, a third Fix could be offered. Original expression: (str1 == str2) Fix 1 (no null check): (str1.equals(str2)) Fix 2 (ternary null check): ((str1 == null) ? (str2 == null) : str1.equals(str2)) Fix 3 (non-ternary null check): (((str1 == null) && (str2 == null)) || ((str1 != null) && str1.equals(str2))) Similarly, != would be handled thusly: Original expression: (str1 != str2) Fix 1 (no null check): (!str1.equals(str2)) Fix 2 (ternary null check): ((str1 == null) ? (str2 != null) : !str1.equals(str2)) Fix 3 (non-ternary null check): (((str1 == null) && (str2 != null)) || ((str1 != null) && !str1.equals(str2)))
I've already started on a patch. Am I allowed to assign this to myself?
Yes, please go ahead. Please introduce some tests and options for this functionality. Thanks.
Created attachment 82701 [details] Proposed patch
I've implemented the fix and introduced an option to control whether the fix uses a ternary condition for the null check (default is false). I don't know how to set up unit tests for it, though.
I've integrated the fix, made some minor adjustments, changed the default to ternary (to be aligned with 'generate equals()') and added some tests -- you can have a look in the changeset mentioned below in case you're interested how to set them up. Marking as FIXED. Thank you very much for your contribution. --- http://hg.netbeans.org/jet-main/rev/12e027c37200
Thanks!
Integrated into 'main-golden', will be available in build *200905260201* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress) Changeset: http://hg.netbeans.org/main-golden/rev/12e027c37200 User: Max Sauer <msauer@netbeans.org> Log: #158042: "Comparing Strings using == or !=" hint should offer a Fix