Eclipse gives dead code warning for reachable code
Asked Answered
C

1

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

Catima answered 15/5, 2018 at 13:46 Comment(7)
eclipse issue looks likeSupernal
Definitely a weird interaction. Commenting out the list.clear() and the message disappears. Also you can indeed replace the ArrayList<String> 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.Timmie
@Timmie I think it's this one, btw bugs.eclipse.org/bugs/show_bug.cgi?id=366277Supernal
Definitely sounds like it. Funny to still have that around after so many years.Timmie
In that case related: #39599215Timmie
@Timmie 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.Tomasine
I asked about this back in 2016: #39599215Selfimmolation
S
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)

Supernal answered 15/5, 2018 at 13:55 Comment(4)
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.Catima
@Catima well read the last comment, it was still reproducible latelySupernal
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.Flaggy
@Supernal I saw. That's why I wrote "sadly".Catima

© 2022 - 2024 — McMap. All rights reserved.