簡體   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