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 255631 - "Unnecessary test for null" with asserts
Summary: "Unnecessary test for null" with asserts
Status: NEW
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 8.1
Hardware: PC Windows 7
: P3 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on:
Blocks: 249320
  Show dependency tree
 
Reported: 2015-09-30 11:49 UTC by terje7601
Modified: 2016-07-26 21:26 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 terje7601 2015-09-30 11:49:47 UTC
The class below gives an "Unnecessary test for null" hint on the if statement. This is because of the assert. However, the hint is only valid if assertions are actually enabled. And since assertions are not enabled by default, I believe the hint is inappropriate in this case.




class Names {

	public static void main(String[] args) {
		String name = getName();
		assert (name != null);
		if (name != null) {
			System.out.println("Hello, " + name);
		}
	}

	static String getName() {
		return null;
	}

}
Comment 1 Svata Dedic 2016-07-26 21:26:54 UTC
In general asserts are put into the code to declare invariants which the programmer thinks should be true.
By inserting an assert statement, the programmer explicitly says the variable should not be null after, and the IDE should work with that information. The actual runtime checks are irrelevant -- the code as it is written is broken.

One can use assert to trigger debugging code, like

boolean debug = false;
assert debug = true;

if (debug) {
...
}


or similar thing, but I think these are rather misuses of the assert statement.

In the experimental implementation of issue #249320, several modes to analyze asserts are available in the analysis engine, including your approach - but no UI :) so I'll keep this defect as a reminder to provide user option to tune the analysis.