| Summary: | [null] Incorrect redundant null check warning on a String | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| Product: | [Eclipse Project] JDT | Reporter: | Ayushman Jain <amj87.iitr> | ||||||
| Component: | Core | Assignee: | Ayushman Jain <amj87.iitr> | ||||||
| Status: | VERIFIED FIXED | QA Contact: | |||||||
| Severity: | normal | ||||||||
| Priority: | P3 | CC: | Olivier_Thomann, satyam.kandula, srikanth_sankaran, stephan.herrmann, sualeh | ||||||
| Version: | 3.7 | Flags: | Olivier_Thomann:
review+
|
||||||
| Target Milestone: | 3.7 M6 | ||||||||
| Hardware: | All | ||||||||
| OS: | All | ||||||||
| Whiteboard: | |||||||||
| Attachments: |
|
||||||||
Note that: test = test + "... some text"; removes the dead code warning properly. So there is a missing reset of null info for the org.eclipse.jdt.internal.compiler.ast.CompoundAssignment. See also: bug #339276 Created attachment 190734 [details]
possible fix
I observed that this kind of behaviour can only occur in the case of a String. This is because anything can be added to a null String, even the null literal itself. So,
test = null; // test is null
test += null; // test has a nonnull value i.e. "nullnull"
test += "abc"; // test has a value "nullnullabc"
For base types, such as int, float, etc. we dont need the null info, and for Integer,Float, etc, the null info will never change in a compound assignment operator.
Hence, this fix takes care of compound assign. for a String variable, and makes it definitely non null.
Created attachment 190741 [details]
proposed fix v1.0 + tests
Same fix with added tests
Olivier, please review. Thanks!
Looks ok. Since it works fine, I would be tempted to release this as part of M6. Ayushman, what do you think ? *** Bug 339276 has been marked as a duplicate of this bug. *** Released for 3.7M6 on the behalf of Ayushman. Verified for 3.7M6. |
HEAD public class Snippet { public static void main(String[] args) { String test = null; test += "... some text"; if (test != null) { System.out.println("It worked!"); } } } Above case gives a bogus "null comparison always yields false" warning.