Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 337516 - Dead code warning missing if boolean variable is not final
Summary: Dead code warning missing if boolean variable is not final
Status: CLOSED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.7   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT Core Triaged CLA
QA Contact: Ayushman Jain CLA
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2011-02-18 01:27 EST by Deepak Azad CLA
Modified: 2019-12-08 12:38 EST (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Deepak Azad CLA 2011-02-18 01:27:08 EST
The sysout is flagged as dead code if 'b' is made final, but not when 'b' is not final.

-------------------------------------------------------------------------------
package p;
class A {
	void foo() {
		boolean b = true;
		if(b) {
			return;
		}
		System.out.println("coming here");//dead code if 'b' is final
	}
}

-------------------------------------------------------------------------------
Comment 1 Srikanth Sankaran CLA 2011-02-18 02:42:45 EST
I think what you are seeing is the right behavior. If statements are
handled in a special way to be compatible with JLS3.
See http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.21

"... One might expect the if statement to be handled in the following manner, but these are not the rules that the Java programming language actually uses: ..."

See that javac also does not complain here, though it does if the if is
changed into while.
Comment 2 Ayushman Jain CLA 2011-02-18 03:13:45 EST
(In reply to comment #1)
> I think what you are seeing is the right behavior. If statements are
> handled in a special way to be compatible with JLS3.
> See http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.21

That does apply to the "Unreachable code" warning, but I dont think it does to the dead code warning, which is jdt specific feature intended to raise warnings because of cases such as if (false) {} , that should not be flagged unreachable.

This difference in behaviour between when b is final and otherwise arises because of special treatment of final variables. An optimized boolean constant is set on all final variables during resolution, so that a compiler can do optimizations during code gen. We use this boolean constant to figure out if only one branch of the if-else construct is going to be executed, and so in the given case, we conclude that the 'then' branch will always execute, leading upt a return statement. Hence, sysout is marked dead code. Incase when b is not final, this constant cant be used and compiler is not able to figure out that sysout is dead code.

IMO, this behaviour is ok. In order to raise the warning for the non-final case, we will have to evaluate the value of b in if(b), but we never evaluate values of expressions in static analysis. (note that evaluating the null status and evaluating the value are different issues)
Comment 3 Ayushman Jain CLA 2011-02-18 03:49:46 EST
Will investigate if something can be done, time permitting
Comment 4 Eclipse Genie CLA 2019-12-08 12:38:32 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.