简体   繁体   English

Eclipse中不需要的死代码警告

[英]Unwanted Dead Code Warning in Eclipse

The following code gives me a 'Dead Code' warning in Eclipse: 以下代码在Eclipse中给出了一个“死代码”警告:

private void add(Node<E> n, E element) {
        Node<E> e = new Node<E>(element);
        if (n == null)
            root = e;
        else if (n.compareTo(e) > 0)
            if (n.hasLeft())
                add(n.getLeft(), element);
            else
                n.setLeft(e);
        else if (n.hasRight())
            add(n.getRight(), element);
        else
            n.setRight(e);
        balance(e);
    }

The warning appears at the line that says root = e; 警告出现在root = e;的行上root = e; .

I looked up dead code and found that it is code hat has no effect and will therefore be ignored by the java compiler. 我查了死代码,发现代码帽没有效果,因此会被java编译器忽略。

However, this root is a private field in my class and therefore it is necessary for the function of my program that I do this. 但是,这个root是我班级中的私有字段,因此我的程序功能是必要的。

Is the compiler really going to ignore this? 编译器真的会忽略这个吗? How can I stop that? 我怎么能阻止它? Why does it think it is dead code? 为什么它认为它是死代码?

If root is a private field in your class that contains the add method you posted, then, as you said, the line root = e; 如果root是你的类中包含你发布的add方法的私有字段,那么,如你所说,行root = e; should not be considered dead code by the Eclipse IDE. 不应该被Eclipse IDE认为是死代码。

The compiler should work fine ... it's just an IDE warning. 编译器应该正常工作......它只是一个IDE警告。

My guess would be that Eclipse does some sort of code walking (similar to Cyclomatic complexity tools) to determine code paths and find "dead code" and "unreachable code". 我的猜测是Eclipse会执行某种代码行走(类似于Cyclomatic复杂性工具)来确定代码路径并找到“死代码”和“无法访问的代码”。

I would try refreshing, then doing a clean and build in the IDE. 我会尝试刷新,然后干净并在IDE中构建。 If that doesn't resolve it, Eclipse may just have a "false positive" on warning on dead code. 如果这不能解决它,Eclipse可能会对死代码的警告产生“误报”。 Wouldn't be the first time ... I use both Eclipse and IntelliJ IDEA and have seen both IDEs incorrectly warn on code before. 这不是第一次......我同时使用Eclipse和IntelliJ IDEA,并且看到两个IDE之前都错误地警告过代码。 However, my code still compiles fine despite the IDE warning. 但是,尽管有IDE警告,我的代码仍可正常编译。

There are possibly two issues going on: first: The issue is that the line root is not "used" for anything. 可能存在两个问题:第一:问题是线根不是“用于”任何东西。 In findbugs the same error is referred to as a "dead store" which, per findbugs means: 在findbugs中,同样的错误被称为“死存储”,每个findbugs意味着:

This instruction assigns a value to a local variable, but the value is not read or used in any subsequent instruction. 此指令为局部变量赋值,但不会在任何后续指令中读取或使用该值。 Often, this indicates an error, because the value computed is never used. 通常,这表示错误,因为从未使用计算的值。

note that the keyword here is "Often". 请注意,此处的关键字是“常常”。

I would check and make sure that root is being used as you expect, and if it is it could be, as mentioned by Philip Tenn, a false positive. 我会检查并确保root正在按照您的预期使用,如果可能的话,正如Philip Tenn所提到的那样,是误报。

Second: Your issue may be related to this issue . 第二:您的问题可能与此问题有关

Since the source is not sufficient to find the exact cause I guess its related to the issues in eclipse related to Dead code. 由于源不足以找到确切原因,我猜它与eclipse中与死代码相关的问题有关。 Some of the issues you can check here and here . 您可以在此处此处查看一些问题。

Look at your if condition. 看看你的条件。

Node<E> e = new Node<E>(element);
if (n == null) {
    // dead code here
}

This code is dead because n == null will always be false. 此代码已死,因为n == null始终为false。 You have just created a new instance of Node<E> on the previous line (unconditionally). 您刚刚在上一行创建了一个新的Node<E>实例(无条件)。 A new object will never be null. 新对象永远不会为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM