如何抑制 Eclipse 3.5 的死代码警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1085520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 13:51:27  来源:igfitidea点击:

How do I suppress Eclipse 3.5's warnings of dead code

javaeclipsewarningssuppress-warnings

提问by Peter Becker

I use a class for detecting email addresseswhich uses static final booleans to configure the matching behavior. Since I upgraded to Eclipse 3.5 I get warnings about dead code, since Eclipse notices that one branch in this can not be reached:

我使用一个类来检测电子邮件地址该类使用静态最终布尔值来配置匹配行为。自从我升级到 Eclipse 3.5 后,我收到了关于死代码的警告,因为 Eclipse 注意到无法访问其中的一个分支:

private static final boolean ALLOW_DOMAIN_LITERALS = false;
private static final String domain = ALLOW_DOMAIN_LITERALS ? rfc2822Domain : rfc1035DomainName;

Oddly enough it is happy with this:

奇怪的是,它对此很满意:

private static final String domain;
static {
    if(ALLOW_DOMAIN_LITERALS) {
        domain = rfc2822Domain;
    } else {
        domain= rfc1035DomainName;
    }
}

since it seems to recognize the common if(DEBUG)pattern, but the ternary operator doesn't seem to count.

因为它似乎可以识别常见if(DEBUG)模式,但三元运算符似乎并不重要。

Since I'd rather not fork the class too much just to keep Eclipse happy, I'd prefer putting an @SuppressWarningsat the top instead of changing the code. Unfortunately I can't find a matching one apart from the brute-force "all". Is there a value just for the dead code detection?

因为我不想为了让 Eclipse 开心而过多地 fork 类,所以我更喜欢将 放在@SuppressWarnings顶部而不是更改代码。不幸的是,除了 brute-force 之外,我找不到匹配的"all"。是否有仅用于死代码检测的值?

回答by Csaba_H

UPDATE: from Adam's comment:

更新:来自亚当的评论:

In Eclipse 3.6 and newer Eclipse versions @SuppressWarnings("unused")can now be used to suppress 'dead code' warnings. See Christopher Stock's answer.

在 Eclipse 3.6 和更新的 Eclipse 版本@SuppressWarnings("unused")中,现在可用于抑制“死代码”警告。参见Christopher Stock 的回答

See also Eclipse 4.4(Luna) helpfor @SuppressWarnings.

另请参阅Eclipse 4.4(Luna) 帮助以了解 @SuppressWarnings。

Original answer:

原答案:

All SuppressWarnings values Eclipse 3.5 "knows" are listed in this page. It seems that there is no value for suppressing only the new dead-code detection. But you can use the @SuppressWarnings("all")just before the domaindeclaration so it will suppress warnings for only that line not for the whole class:

此页面中列出了 Eclipse 3.5“知道”的所有 SuppressWarnings 值。似乎只抑制新的死码检测没有任何价值。但是您可以@SuppressWarnings("all")domain声明之前使用 ,这样它就会抑制仅针对该行而不是针对整个类的警告:

private static final boolean ALLOW_DOMAIN_LITERALS = false;
@SuppressWarnings("all") 
private static final String domain = ALLOW_DOMAIN_LITERALS ? rfc2822Domain : rfc1035DomainName;

Because dead code check is a new one you can also suggest an enchancement in the Eclipse bug databasefor supporting the ternary operation as well.

因为死代码检查是一种新的检查,您还可以建议在Eclipse 错误数据库中进行增强以支持三元操作。

回答by J-16 SDiZ

Select Ignorein Windows -> Preferences > Java > Compiler > Errors/Warningsunder Potential programming problemssection

Ignore在部分Windows -> Preferences > Java > Compiler > Errors/Warnings下选择Potential programming problems

回答by Christopher Stock

You can disable the 'dead code' warnings using

您可以使用禁用“死代码”警告

@SuppressWarnings( "unused" )

See the eclipse documentation for more Information:

有关更多信息,请参阅 eclipse 文档:

http://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-suppress_warnings.htm

http://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-suppress_warnings.htm

"unused" to suppress warnings relative to unused code and dead code

“未使用”以抑制与未使用代码和死代码相关的警告

Greetings

你好

Christopher

克里斯托弗