Search code examples
javaeclipsedead-codeunreachable-code

Unreachable code error vs. dead code warning in Java under Eclipse?


Does anyone know why:

public void foo()
{
    System.out.println("Hello");
    return;
    System.out.println("World!");
}

Would be reported as an "unreachable error" under Eclipse, but

public void foo()
{
    System.out.println("Hello");
    if(true) return;
    System.out.println("World!");
}

Only triggers a "Dead code" warning?

The only explanation I can think of is that the Java compiler only flags the first, and that some extra analysis in Eclipse figures out the second. However, if that is the case, why can't the Java compiler figure out this case at compile time?

Wouldn't the Java compiler figure out at compile time that the if(true) has no effect, thus yielding bytecode that is essentially identical? At what point is the reachable code analysis applied?

I guess a more general way to think of this question is: "when is the reachable code analysis applied"? In the transformation of the second Java code fragment to the final bytecode, I am sure that at some point the "if(true)" runtime equivalent is removed, and the representations of the two programs become identical. Wouldn't the Java compiler then apply its reachable code analysis again?


Solution

  • The first does not compile (you got an error), the second compiles (you just got a warning). That's the difference.

    As to why Eclipse detects dead code, well, that's just the convenience of an integrated development tool with a built-in compiler which can be finetuned more as opposed to JDK to detect this kind of code.

    Update: the JDK actually eliminates dead code.

    public class Test {
        public void foo() {
            System.out.println("foo");
            if(true)return;
            System.out.println("foo");
        }
        public void bar() {
            System.out.println("bar");
            if(false)return;
            System.out.println("bar");
        }
    }
    

    javap -c says:

    public class Test extends java.lang.Object{
    public Test();
      Code:
       0:   aload_0
       1:   invokespecial   #1; //Method java/lang/Object."":()V
       4:   return
    
    public void foo();
      Code:
       0:   getstatic       #2; //Field java/lang/System.out:Ljava/io/PrintStream;
       3:   ldc             #3; //String foo
       5:   invokevirtual   #4; //Method java/io/PrintStream.println:(Ljava/lang/StrV
       8:   return
    
    public void bar();
      Code:
       0:   getstatic       #2; //Field java/lang/System.out:Ljava/io/PrintStream;
       3:   ldc             #5; //String bar
       5:   invokevirtual   #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8:   getstatic       #2; //Field java/lang/System.out:Ljava/io/PrintStream;
       11:  ldc             #5; //String bar
       13:  invokevirtual   #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
       16:  return
    
    }
    

    As to why it (Sun) doesn't give a warning about that, I have no idea :) At least the JDK compiler has actually DCE (Dead Code Elimination) builtin.