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 - Unnecessary test for null
Summary: Unnecessary test for null
Status: RESOLVED FIXED
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 8.0.1
Hardware: PC Windows 8 x64
: P3 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
: 247446 251969 (view as bug list)
Depends on: 249320
Blocks:
  Show dependency tree
 
Reported: 2014-06-23 21:10 UTC by zgabi
Modified: 2015-11-18 13:38 UTC (History)
2 users (show)

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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.