2

The following code causes Eclipse to display a dead code warning although the code is reachable. Am I missing something here, or is this an Eclipse/javac bug?

import java.util.ArrayList;

public class DeadCodeDemo {

    public static class SomeClosable implements AutoCloseable {
        @Override
        public void close() throws Exception {
        }
    }

    public static ArrayList<String> throwRuntime() {
        throw new RuntimeException();
    }

    public static void main(String[] args) {
        ArrayList<String> list = null;
        try {
            try (SomeClosable c = new SomeClosable()) {
                list = throwRuntime();
            }
            try (SomeClosable c = new SomeClosable()) {
                list.clear();
            }
        } catch (Exception e) {
            if (list != null) { // Warning: Redundant null check: The variable list cannot be null at this location
                System.out.println("List is not null");
            } else {
                System.out.println("List is null"); // Warning: Dead code
            }
        }
    }
}

The code prints List is null

I'm using Eclipse 4.7.3a (Oxygen.3a) and JDK 8_162

Malt
  • 28,965
  • 9
  • 65
  • 105
  • 2
    eclipse issue looks like – Eugene May 15 '18 at 13:49
  • Definitely a weird interaction. Commenting out the `list.clear()` and the message disappears. Also you can indeed replace the `ArrayList` with any class and do the same (just call any method on it) and the warning is shown. Remove the method call and it works. – Ben May 15 '18 at 13:53
  • 2
    @Ben I think it's this one, btw https://bugs.eclipse.org/bugs/show_bug.cgi?id=366277 – Eugene May 15 '18 at 13:56
  • Definitely sounds like it. Funny to still have that around after so many years. – Ben May 15 '18 at 13:56
  • In that case related: https://stackoverflow.com/questions/39598715/dead-code-warning-in-try-with-resources-but-not-in-translated-try-catch-finally – Ben May 15 '18 at 13:58
  • @Ben about removing `list.clear()` removing the warning: it's because Eclipse thinks "list has already been derefenced before, so it can't be `null` or it would have thrown an NPE"; I've noticed that before. – daniu May 15 '18 at 13:58
  • 1
    I asked about this back in 2016: https://stackoverflow.com/questions/39598715/dead-code-warning-in-try-with-resources-but-not-in-translated-try-catch-finally – Joshua Taylor May 15 '18 at 15:11

1 Answers1

4

I think it's this issue, still open.

Just remember that this is Eclipse warning, not javac - and that is pretty much all you should care until that issue is resolved (even if it 7 years old now)

Eugene
  • 117,005
  • 15
  • 201
  • 306
  • Good find. Sadly the issue is from Dec 2011 (!), a few months after Java 7 was released. So they have this issue for as long as Java has try-with-resources. – Malt May 15 '18 at 13:59
  • @Malt well read the last comment, it was still reproducible lately – Eugene May 15 '18 at 14:00
  • The issue indeed seems likely to be related to the try-with-resources context, but this case seems even simpler than the one in the issue report. The catch block can be reached with `list` still `null` by `throwRuntime()` throwing an unchecked exception (as in fact it will do every time). There's no need to be concerned with the implicit `close()` invocation. – John Bollinger May 15 '18 at 14:01
  • @Eugene I saw. That's why I wrote "sadly". – Malt May 15 '18 at 14:02