7

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?

golddove
  • 1,165
  • 2
  • 14
  • 32
  • 1
    is that the actual code? – Mitch Wheat Feb 22 '13 at 01:53
  • 2
    Do a clean and rebuild the code, Eclipse bugs out once in a while. – Jesus Ramos Feb 22 '13 at 01:55
  • 1
    I think the only time this can happen is if it would be impossible for root to be null. Maybe look at where this variable is used. – austin Feb 22 '13 at 01:55
  • When I place this code in Eclipse Indigo, I get no warnings. ([Here is the code I used for comparison; I filled in the rest of the class with garbage.](http://ideone.com/giMWVJ)) – Cat Feb 22 '13 at 02:14
  • I am not sure why it is like that. Maybe it is cause I am using it differently. I have a BST class that has this private field and public add method and then a Node class inside this class. The add method is not in the Node class. Although I don't see how that makes a difference. – golddove Feb 22 '13 at 02:33
  • Did'nt you get warnings that you should use curly braces? – AlexWien Feb 22 '13 at 02:39
  • @austin it may be true that root could never be null, but n will not always point to root as this method is recursive. It is highly possible that n will be null several times. – golddove Mar 10 '13 at 03:30

4 Answers4

2

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.

Philip Tenn
  • 6,003
  • 8
  • 48
  • 85
2

There are possibly two issues going on: first: The issue is that the line root is not "used" for anything. In findbugs the same error is referred to as a "dead store" which, per findbugs means:

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

note that the keyword here is "Often".

I would check and make sure that root is being used as you expect, and if it is it could be, as mentioned by Philip Tenn, a false positive.

Second: Your issue may be related to this issue.

Community
  • 1
  • 1
Robert H
  • 11,520
  • 18
  • 68
  • 110
1

Since the source is not sufficient to find the exact cause I guess its related to the issues in eclipse related to Dead code. Some of the issues you can check here and here.

Jayamohan
  • 12,734
  • 2
  • 27
  • 41
0

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 have just created a new instance of Node<E> on the previous line (unconditionally). A new object will never be null.

efritz
  • 5,125
  • 4
  • 24
  • 33