![](/img/trans.png)
[英]IntelliJ IDEA: What is 'Unnecessary local variable' inspection name?
[英]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 + Enter , notUsedLocalVariable
可以選擇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.