简体   繁体   English

Eclipse死代码警告-为什么我得到它?

[英]Eclipse dead code warning - why am I getting it?

In the following loop, listeners is array list. 在以下循环中, listeners为数组列表。 Eclipse shows me warning i++ being dead code. Eclipse向我显示了警告i++为无效代码。 I have now added full for loop at its entirety as it exists in the code. 我现在添加了完整的for循环,因为它存在于代码中。 Listeners are passed to the function and I don't think there is a way for Eclipse to detect if the list is empty 侦听器被传递给该函数,我认为Eclipse没有办法检测列表是否为空

for (int i = 0; i < listeners.size(); i++) {
  listener = getMessageListener(i);
  while (listener.isMessageAvailable()) {
    fireJob(listener);
  }
  log.info("Placed all the pending jobs because of termination signal");
}

However following simple loop does not generate same warning 但是,以下简单循环不会生成相同的警告

for (int y = 0; y < 10; y++) {
    System.out.println(y);
}

To me, both look functionally same - standard for loop. 在我看来,两者在功能上是相同的-循环的标准。 Eclipse is very specific about highlighting i++ area and fix is to remove i++ . Eclipse对于突出显示i++区域非常具体,而解决方法是删除i++

Another observation is if I copy same loop to somewhere else, I don't get warning. 另一个观察结果是,如果我将同一循环复制到其他地方,则不会得到警告。 So one possibility is eclipse could be thinking for whatever reasons loop won't be executed. 因此,一种可能性是日食可能会出于某种原因考虑将不会执行循环。 But code in the block gets executed. 但是块中的代码被执行了。

One thought: if you comment out the log.info(... line does the warning go away? Because if Eclipse is determining that something is causing the loop to terminate early it must be because of one of the following: 一个想法:如果您注释掉log.info(...行,警告会消失吗?因为如果Eclipse确定某些原因导致循环提前终止,则一定是由于以下原因之一:

  1. the condition is always false. 条件始终为假。 But that would make the body unreachable, so an Unreachable code error on the body. 但这会使身体无法触及,因此在身体上出现了无法触及的代码错误。 Not your case 不是你的情况

    eg: in this example y++ is Dead code , but body is Unreachable code . 例如:在这个例子中y++Dead代码 ,但是body是Unreachable代码

     for (int y = 0; false; y++) { System.out.println(y); } 
  2. some statement before the last statement is terminating your loop. 最后一条语句之前的一些语句终止了您的循环。 But then some of the body would be unreachable or dead. 但随后,某些身体将无法到达或死亡。 Not your case 不是你的情况

    eg: in this example y++ is Dead code , and the println in Unreachable code . 例如:在此示例中, y++Dead代码 ,而printlnUnreachable代码

     for (int y = 0; y < 10; y++) { break; System.out.println(y); } 

    in this example y++ and println is Dead code . 在此示例中, y++printlnDead代码

     for (int y = 0; y < 10; y++) { if (true) { break; } System.out.println(y); } 
  3. last statement terminates the loop. 最后一条语句终止循环。 That would make only the i++ Dead code . 那只会使i++ Dead代码成为可能 Not your case , but should be testable that it isn't your case by commenting out the last statement. 不是您的情况 ,但应通过注释掉最后一条语句来证明不是您的情况。

    eg: in this example only y++ is Dead code , but commenting out the break means there is no Dead code . 例如:在这个例子中,只有y++Dead代码 ,但是注释掉break意味着没有Dead代码

     for (int y = 0; y < 10; y++) { System.out.println(y); break; } 

So, I believe, if commenting out the last statement ( log line in your case) in the loop does not make the Dead code warning go away you must have found a bug in the warnings generator/compiler. 因此,我相信,如果注释掉循环中的最后一条语句(在您的情况下为log行)并不能使Dead代码警告消失,那么您一定在警告生成器/编译器中发现了一个错误。 If so, I hope you can reduce it for all our sakes so that a proper bug report can be submitted. 如果是这样,我希望您能为我们所有的缘故减少它,以便可以提交适当的错误报告。

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

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