Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unwanted Dead Code Warning in Eclipse

The following code gives me a 'Dead Code' warning 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);
    }

The warning appears at the line that says root = e;.

I looked up dead code and found that it is code hat has no effect and will therefore be ignored by the java compiler.

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

Is the compiler really going to ignore this? How can I stop that? Why does it think it is dead code?

like image 639
golddove Avatar asked Feb 22 '13 01:02

golddove


People also ask

Why does Eclipse say dead code?

In Eclipse, "dead code" is code that will never be executed. Usually it's in a conditional branch that logically will never be entered. boolean x = true; if (x) { // do something } else { // this is dead code! }

What does error dead code mean?

In some areas of computer programming, dead code is a section in the source code of a program which is executed but whose result is never used in any other computation.

How do I show all warnings in eclipse?

Click on the small triangle in the upper right corner of the problems view and select "Configure Contents". In that dialog check "Show all items" and uncheck "Use item limits" to show all warnings.


1 Answers

If root is a private field in your class that contains the add method you posted, then, as you said, the line root = e; should not be considered dead code by the Eclipse IDE.

The compiler should work fine ... it's just an IDE warning.

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

I would try refreshing, then doing a clean and build in the IDE. If that doesn't resolve it, Eclipse may just have a "false positive" on warning on dead code. Wouldn't be the first time ... I use both Eclipse and IntelliJ IDEA and have seen both IDEs incorrectly warn on code before. However, my code still compiles fine despite the IDE warning.

like image 174
Philip Tenn Avatar answered Sep 28 '22 15:09

Philip Tenn