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.

Bug 245201

Summary: Unnecessary test for null
Product: java Reporter: zgabi
Component: HintsAssignee: Svata Dedic <sdedic>
Status: RESOLVED FIXED    
Severity: normal CC: jo.navy, rosa_t
Priority: P3    
Version: 8.0.1   
Hardware: PC   
OS: Windows 8 x64   
Issue Type: DEFECT Exception Reporter:
Bug Depends on: 249320    
Bug Blocks:    

Description zgabi 2014-06-23 21:10:20 UTC
"Unnecessary test for null" hint is shown for the following code (at "a != null"):

    public void xxx(long[] a, long[] b) {
        
        if ((a == null) ^ (b == null)) {
            throw new Error("a and b should be both null or not null.");
        }
        
        if (a != null && a.length != b.length) {
            throw new Error("a.length != b.length");
        }
    }
Comment 1 Jiri Prox 2014-06-24 08:23:00 UTC
reprducible
Comment 2 soft_syreg 2014-06-27 10:31:31 UTC
The same error occures for the following code

public void aFunction (Object a, Object b) {
  
  if (a == null ? b == null : b != null) {
    return;
  }

  if (a == null) { // unnecessary test for null appears here
    // do something
  }
}

If a is null and b not, the first test is false and the second is necessary.

If I rewrite the first test as

  if ((a == null && b == null) || (a != null && b != null)) {

The warning disappears but the value of a is tested twice
Comment 3 Svata Dedic 2015-09-10 13:28:09 UTC
*** Bug 251969 has been marked as a duplicate of this bug. ***
Comment 4 Svata Dedic 2015-09-10 13:30:48 UTC
*** Bug 247446 has been marked as a duplicate of this bug. ***
Comment 5 Svata Dedic 2015-09-11 09:33:27 UTC
The case in the original description is not possible to solve with the current algoritm.

The 2nd case could be fixed though.
Comment 6 Svata Dedic 2015-11-18 13:38:45 UTC
Fixed in the experimental impl; wait for the umbrella issue to close.