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 247989 - "Unnecessary test for null" warning is wrong
Summary: "Unnecessary test for null" warning is wrong
Status: RESOLVED DUPLICATE of bug 249320
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 8.0.1
Hardware: PC Windows 7
: P3 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-10-16 21:03 UTC by tln
Modified: 2015-11-13 09:35 UTC (History)
0 users

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 tln 2014-10-16 21:03:30 UTC
Nonsense example class:

public class Bug
{
	public Object getObject()
	{
		return null;
	}
	
	public void bug()
	{
		Object obj=getObject();
		if (obj==null)
		{
			switch ("abc")
			{
			case "abc":
				break;
			default:
				throw new AssertionError();
			}
		}
		
		if (obj==null)	// (*)
		{
			getClass();
		}
	}
}


NetBeans reports an "Unnecessary test for null" hint at the line marked as (*), however this hint is wrong.
Of course in this nonsense code you instantly ask why I have two if statements for the same test, but in real life you might have other code between them.

Anyway, if I remove the line
throw new AssertionError();
then the false alarm warning disappears, so obviously NetBeans fails to see that there is a path through the "if (obj==null)" branch that does *not* throw an AssertionError. I'd guess there is no designated treatment of switch statements with breaks while checking whether this warning needs to be displayed.
Comment 1 Svata Dedic 2014-12-12 12:19:10 UTC

*** This bug has been marked as a duplicate of bug 249320 ***
Comment 2 Svata Dedic 2015-11-13 09:35:23 UTC
Implemented in new version; wait for umbrella issue to close