繁体   English   中英

IntelliJ IDEA @SuppressWarnings用于检查工具名称

[英]IntelliJ IDEA @SuppressWarnings for inspection with name of tool

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

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

对于来自外部的人,可能不清楚需要哪种工具进行抑制。

PMD使用前缀:

@SuppressWarnings("PMD.UnusedLocalVariable")

FindBugs使用专用注释:

@SuppressFBWarnings("NP_NONNULL_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR")

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

为方法参数抑制此方法的一种方法是使用javadoc标记:

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

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

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

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

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

一般的做法

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

这是一个有很多警告的例子:

在此输入图像描述

如果您在notUsedLocalVariable上按Alt + EnternotUsedLocalVariable可以选择Suppress for statement with comment在上下文菜单上Suppress for statement with comment (在选择Remove variable ...后右转)。

在某些变量/字段/方法上,所选的抑制只是Suppress for statement

现在,如果你查看右侧字段,大多数(并非所有)警告都会被抑制:

在此输入图像描述

正如您所看到的,在同一个注释行上可以有多个抑制。

这似乎有点乏味但我认为这是你能做的最好的。

我找到了一种如何使用@SuppressWarnings实现这一@SuppressWarnings

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

请注意, idea:之后必须有空格idea:否则它不起作用。

暂无
暂无

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

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