n, E element) { Node<...">

Eclipse Unwanted Dead Code Warning

The following code gives me the warning "Dead Code" in Eclipse:

private void add(Node<E> n, E element) { Node<E> e = new Node<E>(element); if (n == null) root = e; else if (n.compareTo(e) > 0) if (n.hasLeft()) add(n.getLeft(), element); else n.setLeft(e); else if (n.hasRight()) add(n.getRight(), element); else n.setRight(e); balance(e); } 

A warning appears on the line labeled root = e; .

I was looking for dead code and found that it has no effect and therefore will be ignored by the java compiler.

However, this root is a private field in my class, and therefore for my program it is necessary that I do this.

Will the compiler really ignore this? How can i stop this? Why does he think this is dead code?

+7
source share
4 answers

If root is a private field of your class containing the add method you published, then, as you said, the line root = e; should not be considered dead code using the Eclipse IDE.

The compiler should work fine ... this is just an IDE warning.

My guess would be that Eclipse takes some kind of code walk (similar to Cyclomatic complexity tools) to determine code paths and find "dead code" and "unreachable code."

I would try updating and then doing a clean and build in the IDE. If this does not resolve it, Eclipse may simply have a “false positive” when warning of dead code. It would not be the first time ... I use both Eclipse and IntelliJ IDEA and I saw that both IDEs incorrectly warn about the code earlier. However, my code is still compiling despite the IDE warning.

+2
source

Perhaps there are two problems: firstly: the problem is that the root file is not used for anything. In findbugs, the same error is called "dead storage", which on findbugs means:

This command assigns a value to a local variable, but the value is not read or used in any subsequent instruction. Often this indicates an error, since the calculated value is never used.

Please note that the keyword is often here.

I would check and make sure that root is used as you expect, and if possible, then, as Philip Tenn mentioned, the false result.

Secondly: Your problem may be related to this problem .

+2
source

Since the source is not enough to find the exact cause, I assume that this is due to eclipse problems associated with the Dead code. Some of the questions you can check here and here .

+1
source

Look at your if condition.

 Node<E> e = new Node<E>(element); if (n == null) { // dead code here } 

This code is dead because n == null will always be false. You just created a new instance of Node<E> in the previous line (unconditionally). A new object will never be empty.

0
source

All Articles