Eclipse 为可访问的代码发出死代码警告
Eclipse gives dead code warning for reachable code
以下代码会导致 Eclipse 显示无效代码警告,尽管该代码是可访问的。我是不是遗漏了什么,或者这是一个 Eclipse/javac 错误?
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
}
}
}
}
代码打印 List is null
我正在使用 Eclipse 4.7.3a (Oxygen.3a) 和 JDK 8_162
我认为它是this issue,仍然开放。
请记住,这是 Eclipse warning
,而不是 javac
- 在该问题得到解决之前,这几乎是您应该关心的全部内容(即使它现在已经 7 岁了)
以下代码会导致 Eclipse 显示无效代码警告,尽管该代码是可访问的。我是不是遗漏了什么,或者这是一个 Eclipse/javac 错误?
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
}
}
}
}
代码打印 List is null
我正在使用 Eclipse 4.7.3a (Oxygen.3a) 和 JDK 8_162
我认为它是this issue,仍然开放。
请记住,这是 Eclipse warning
,而不是 javac
- 在该问题得到解决之前,这几乎是您应该关心的全部内容(即使它现在已经 7 岁了)