Eclipse 为可访问代码提供死代码警告 [英] Eclipse gives dead code warning for reachable code
本文介绍了Eclipse 为可访问代码提供死代码警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码会导致 Eclipse 显示死代码警告,尽管代码是可访问的.我在这里遗漏了什么,或者这是一个 Eclipse/javac 错误?
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
}
}
}
}
代码打印List is null
我使用的是 Eclipse 4.7.3a (Oxygen.3a) 和 JDK 8_162
I'm using Eclipse 4.7.3a (Oxygen.3a) and JDK 8_162
推荐答案
我认为是 这个问题,仍然存在.
请记住,这是 Eclipse 警告
,而不是 javac
- 在该问题得到解决之前,这几乎是您应该关心的所有内容(即使它现在已有 7 年历史))
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)
这篇关于Eclipse 为可访问代码提供死代码警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文