Bug 305590 - Redundant null check false-positive
Summary: Redundant null check false-positive
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.6   Edit
Hardware: PC Linux
: P3 minor (vote)
Target Milestone: 3.6 M7   Edit
Assignee: Ayushman Jain CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 242131 (view as bug list)
Depends on:
Blocks:
 
Reported: 2010-03-11 15:44 EST by Boris CLA
Modified: 2011-02-28 07:10 EST (History)
2 users (show)

See Also:
Olivier_Thomann: review+


Attachments
proposed fix v1.0 + regression tests (3.64 KB, patch)
2010-03-12 05:36 EST, Ayushman Jain CLA
Olivier_Thomann: iplog+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Boris CLA 2010-03-11 15:44:21 EST
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.
Comment 1 Olivier Thomann CLA 2010-03-11 21:29:20 EST
Ayushman, could you please have a look?
Comment 2 Ayushman Jain CLA 2010-03-12 05:36:54 EST
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.
Comment 3 Olivier Thomann CLA 2010-03-16 10:28:32 EDT
Patch looks good.
Comment 4 Olivier Thomann CLA 2010-03-16 10:35:55 EDT
Released for 3.6M7.
Regression test added in:
org.eclipse.jdt.core.tests.compiler.regression.NullReferenceTest#testBug305590
Comment 5 Olivier Thomann CLA 2010-04-26 14:40:49 EDT
Verified for 3.6M7 using I20100425-2000
Comment 6 Stephan Herrmann CLA 2011-02-28 07:10:34 EST
*** Bug 242131 has been marked as a duplicate of this bug. ***