Bug 227745 - Incorrect reporting of "Unnecessary test for null" hint
Incorrect reporting of "Unnecessary test for null" hint
Product: java
Classification: Unclassified
Component: Hints
PC Windows 7
: P3 (vote)
: 7.3.1
Assigned To: Jan Lahoda
: 229490 (view as bug list)
Depends on:
  Show dependency treegraph
Reported: 2013-03-20 21:17 UTC by esmithbss
Modified: 2013-05-15 14:37 UTC (History)
3 users (show)

See Also:
Issue Type: DEFECT

IDE log (697.75 KB, text/plain)
2013-03-20 21:17 UTC, esmithbss

Note You need to log in before you can comment on or make changes to this bug.
Description esmithbss 2013-03-20 21:17:08 UTC
Product Version = NetBeans IDE 7.3 (Build 201302132200)
Operating System = Windows 7 version 6.1 running on amd64
Java; VM; Vendor = 1.6.0_34
Runtime = Java HotSpot(TM) 64-Bit Server VM 20.9-b04

Within my code I have the following structure:

if ( null == a) { return; }

for (ClassType c : listofClases) {
   // placed here as common code block.
   if (null == a) {            // <===== Reported as unnecessary test for null.

  // 15 separate tests of this nature which could each set a to null.
   if (c.getId().getValue() > compareAgainst.longValue()) {
      a = (aList.hasNext()) ? (AType)aList.next() : null;          // <========== Incorrect report due to this possible assignment.

Admittedly, this may not be the cleanest way to accomplish this; however, it works and as we all know, "If it ain't broke.  Don't mess with it!"

The hint for the null at the beginning of the loop is correct if the code is viewed linearly without the loop.  It is impossible to enter the loop with a being null.

However, based on the following 15 queries which can result in a being set to null, the hint is incorrectly applied.
Comment 1 esmithbss 2013-03-20 21:17:13 UTC
Created attachment 132875 [details]
IDE log
Comment 2 ban.nobuhiro 2013-04-03 14:57:51 UTC
I have a similar problem.

Here is the code for reproducing the bug.

---- begin
package netbeans;

import java.util.List;

public class NullHint {

    public void foo(List<Object> objs) {
        Object bar = null;
        for (Object obj : objs) {
            if (bar == null) {
                bar = new Object();
        // if objs is empty, bar is null here.
        if (bar != null) { // NB7.3 says this check is unnecessary
---- end
Comment 3 ricktw 2013-04-15 08:39:29 UTC
Another example:

// code is completely dummy; just to demo the "Unnecessary test for null" warning 
private String demoNullHint(Object object, String argument, String defaultValue) {
	if (object==null && (argument==null || argument.equals("test"))) {
		return defaultValue;

	// warning here: "Unnecessary test for null"
	if (object!=null) {
		// if
	else {
		// else

	return "some value";
Comment 4 Jan Lahoda 2013-04-15 09:34:41 UTC
I'll push fixes for the first two usecases into the trunk soon. The last usecase is a duplicate of bug #227954.
Comment 5 Jan Lahoda 2013-04-15 13:34:46 UTC
Comment 6 Quality Engineering 2013-04-18 02:21:51 UTC
Integrated into 'main-golden', will be available in build *201304172301* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main-golden/rev/d7c433595615
User: Jan Lahoda <jlahoda@netbeans.org>
Log: #227745: NPECheck hints must properly handle enhanced for loops.
Comment 7 Jiri Prox 2013-05-02 12:18:47 UTC
Comment 8 Jan Lahoda 2013-05-03 07:32:55 UTC
Backported to release73 branch:
Comment 9 Quality Engineering 2013-05-05 00:27:22 UTC
Integrated into 'releases', will be available in build *201305042200* or newer. Wait for official and publicly available build.
Changeset: http://hg.netbeans.org/releases/rev/58284df11288
User: Jan Lahoda <jlahoda@netbeans.org>
Log: #227745: NPECheck hints must properly handle enhanced for loops.
Comment 10 Jiri Prox 2013-05-15 14:37:10 UTC
*** Bug 229490 has been marked as a duplicate of this bug. ***

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2014, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo