Community
Participate
Working Groups
Build Identifier: 20090619-0625 With this piece of code and "Redundant null check" from the Java Compiler Errors and warnings enabled I get the warning "instanceof always yields false: The variable str can only be null at this location" Object str = null; if ((str = "str") instanceof String) { System.out.println(str); } However, as you can see, str can never be null at the instanceof-check. In this case it is even the opposite: The instanceof will always be true! For other classes the conclusion is right, but the reason-message is wrong. Object str = null; if ((str = "str") instanceof Number) { System.out.println(str); } This also yields the error "instanceof always yields false: The variable str can only be null at this location". It's true that the instanceof will always be false, but its because a String is never an instance of Number and not because str is null. Reproducible: Always Steps to Reproduce: 1. enable "Redundant null check" 2. Use the code mentioned above 3.
Ayushman, could you please have a look?
Created attachment 161856 [details] proposed fix v1.0 + regression tests Its a case where we were recording the null reference (via FlowContext#recordUsingNullReference()) before the assignment expression was analyzed. And hence at the time of giving out the warning, the flowInfo was still unaware of the assignment. The above fix simply makes sure that the expression inside instanceof statement gets analyzed first, the null info and initializations saved into flowInfo, and then only the null references are recorded.
Patch looks good.
Released for 3.6M7. Regression test added in: org.eclipse.jdt.core.tests.compiler.regression.NullReferenceTest#testBug305590
Verified for 3.6M7 using I20100425-2000
*** Bug 242131 has been marked as a duplicate of this bug. ***