IntelliJ IDEA @SuppressWarnings用于检查工具名称 [英] IntelliJ IDEA @SuppressWarnings for inspection with name of tool

查看:2067
本文介绍了IntelliJ IDEA @SuppressWarnings用于检查工具名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以抑制来自IntelliJ IDEA检查的警告:

I know I can suppress warnings from IntelliJ IDEA inspections like that:

@SuppressWarnings("CollectionDeclaredAsConcreteClass")
public PropertiesExpander(Properties properties) {
    this.properties.putAll(properties);
}

对于来自外部的人,可能不清楚哪个工具需要这种压制。

For person from outside it may not be clear for which tool this suppression is needed.

PMD使用前缀:

@SuppressWarnings("PMD.UnusedLocalVariable")

FindBugs使用专用注释:

FindBugs uses dedicated annotation:

@SuppressFBWarnings("NP_NONNULL_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR")

有没有办法清楚地表明这种抑制仅适用于IntelliJ IDEA?

推荐答案

抑制方式对于方法参数,这是使用javadoc标签:

A way of suppressing this for method arguments is to use a javadoc tag instead:

/**
 * @noinspection CollectionDeclaredAsConcreteClass
 */
public PropertiesExpander(Properties properties) {
    this.properties.putAll(properties);
}

这个javadoc标签是特定于IntelliJ的,所以没有其他工具应该有任何麻烦它。您可以通过识别它或通过添加一些注释来轻松识别它:

This javadoc tag is very IntelliJ specific so no other tool should have any troubles with it. And you can easily identify it by just recognize it as it is or by adding some comment to it:

/**
 * @noinspection IntelliJ CollectionDeclaredAsConcreteClass
 */
public PropertiesExpander(Properties properties) {
    this.properties.putAll(properties);
}

当然,你可以进一步缩短它:

And of course you can shorten it up even more:

/** @noinspection CollectionDeclaredAsConcreteClass */
public PropertiesExpander(Properties properties) {
    this.properties.putAll(properties);
}






一般方法

可以使用特殊注释而不是 @SuppressWarnings来逐个禁止某些警告注释。

It is possible to suppress some warnings on a one-by-one basis with special comments instead of the @SuppressWarnings annotation.

以下是一个包​​含大量警告的示例:

Here's an example with lots of warnings:

如果您在上按 Alt + Enter notUsedLocalVariable 然后你可以在上下文菜单中选择抑制带注释的语句(在选择去除变量后右转... )。

If you press Alt+Enter on notUsedLocalVariable you can then choose Suppress for statement with comment on the context menu (after going right when choosing Remove variable ...).

在某些变量/字段/方法中,所选抑制仅为抑制语句

On some variables/fields/methods the selected suppression is just Suppress for statement.

现在,如果你查看右侧字段,大多数(不是全部)警告都被禁止了:

And now most (not all) of the warnings has been suppressed if you look in the right-hand field:

正如您所看到的,在同一评论行上可能存在多次抑制。

As you can see there can be several suppressions on the same comment line.

这看起来有点单调乏味,但我认为这是你能做的最好的。

This can seem to be a bit tedious but I think it's the best you can do.

这篇关于IntelliJ IDEA @SuppressWarnings用于检查工具名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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