简体   繁体   English

为什么是死代码警告?

[英]Why Dead Code Warning?

i have:我有:

  if (Constants.IS_LIVE_MOD == false)
        account = Constants.OM_ACCOUNT;
  else
        account = "abc";

i am getting a dead code warning on 'else'.我在“其他”上收到死代码警告。 Why is it so and wat is the solution for it.为什么会这样,而 wat 是它的解决方案。 Please help.请帮忙。

I assume the IS_LIVE_MOD constant is a final variable that is declared as false, if so then the variable is always false and can't be changed, and so the else statement will never be invoked.我假设IS_LIVE_MOD常量是一个声明为 false 的最终变量,如果是,则该变量始终为 false 并且无法更改,因此永远不会调用 else 语句。 Therefore it is dead code.因此它是死代码。

Eg例如

private static final boolean MY_VAR = false;

if(MY_VAR == false) {
    System.out.println("Always does this");
}
else {
    System.out.println("Dead code");
}

If IS_LIVE_MOD is a compile-time constant with the value false , the compiler knows that the "else" path will never be taken, so gives you a warning.如果IS_LIVE_MOD是值为false的编译时常量,则编译器知道永远不会采用“else”路径,因此会发出警告。 (The Java language specification guarantees that it won't be an error , but it's reasonable to warn you.) (Java 语言规范保证不会出错,但警告你是合理的。)

if Constants.IS_LIVE_MOD is a constant and its value is false than the compiler knows that else would never be executed.如果Constants.IS_LIVE_MOD是一个常量并且它的值为 false,那么编译器就知道 else 永远不会被执行。

You can't have it as a constant.你不能把它当作一个常数。

You're are comparing two constants ( Constants.IS_LIVE_MOD and false ) with each other.您正在相互比较两个常量( Constants.IS_LIVE_MODfalse )。 As they are constant, the result can be determined at compile time.由于它们是恒定的,因此可以在编译时确定结果。 So the compiler can tell which part will never be executed.所以编译器可以知道哪个部分永远不会被执行。

The condition if (Constants.IS_LIVE_MOD == false) always evaluates as true .条件if (Constants.IS_LIVE_MOD == false)始终评估为true Therefore else is never reached.因此永远不会达到else

It implies that Constants.IS_LIVE_MOD is always equal to false;这意味着 Constants.IS_LIVE_MOD 始终等于 false; so the else clause is never executed;所以 else 子句永远不会被执行; hence the dead code warning.因此死代码警告。

The meaning of the warning should be clear: the code will not be executed - is dead - since IS_LIVE_MOD is a constant, but here is one solution (workaround):警告的含义应该很清楚:代码不会被执行 - 已经死了 - 因为 IS_LIVE_MOD 是一个常量,但这里有一个解决方案(解决方法):

if (!Constants.IS_LIVE_MOD)
    account = Constants.OM_ACCOUNT;
else
    account = "abc";

or或者

if (Constants.IS_LIVE_MOD)
    account = "abc";
else
    account = Constants.OM_ACCOUNT;

Details JLS 14.21: Unreachable Statements详细信息 JLS 14.21: 无法访问的语句

If you remove the comparison, the compiler recognizes that as a "conditional compilation" and does not show that warning.如果删除比较,编译器会将其识别为“条件编译”并且不会显示该警告。

Rationale (JLS):理由(JLS):

in order to allow the if statement to be used conveniently for "conditional compilation" purposes为了让 if 语句方便地用于“条件编译”目的

If Constants.IS_LIVE_MOD is constant (as its name implies) and is false, then the else clause can never possibly run;如果Constants.IS_LIVE_MOD是常量(顾名思义)并且为 false,则 else 子句永远不可能运行; this makes it dead code.这使它成为死代码。

Since IS_LIVE_MOD is a constant, the compiler figures out that it will also be false, and that the else part will never be used (until you change your Constants, then it will be the other way around).由于 IS_LIVE_MOD 是一个常量,编译器发现它也将是错误的,并且永远不会使用 else 部分(除非您更改常量,否则它会反过来)。

Don't worry about it.别担心。 Ignore or suppress the warning ( @SuppressWarnings("all") on the method, unfortunately has to be all, I think).忽略或抑制警告(方法上的@SuppressWarnings("all") ,不幸的是必须全部,我认为)。

If you have "real" dead code, it will make an error.如果你有“真正的”死代码,它会出错。

How is Constants.IS_LIVE_MOD declared? Constants.IS_LIVE_MOD是如何声明的? If it's a final field, it's a good chance that the compiler optimizes this expression to be always true.如果它是一个final字段,那么编译器很可能会将此表达式优化为始终为真。

I'd say the most efficient way of writing this would be like this:我想说写这个最有效的方法是这样的:

account = Constants.IS_LIVE_MOD ? "abc"
                                : Constants.OM_ACCOUNT;

I guess Constants.IS_LIVE_MOD is a compile time constant with the value set to false.我猜 Constants.IS_LIVE_MOD 是一个编译时常量,其值设置为 false。 iow it becomes它变成了

if (false == false)
    account = Constants.OM_ACCOUNT;
else
    account = "abc";

You can just ignore the warning since Java doesn't seem to support conditional compilation Java conditional compilation: how to prevent code chunks from being compiled?您可以忽略警告,因为 Java 似乎不支持条件编译Java 条件编译:如何防止编译代码块?

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

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