Eclipse给出了可到达代码的死代码警告 [英] Eclipse gives dead code warning for reachable code

查看:71
本文介绍了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 warning,而不是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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆