簡體   English   中英

有沒有辦法忽略單個 FindBugs 警告?

[英]Is there a way to ignore a single FindBugs warning?

使用 PMD,如果要忽略特定警告,可以使用// NOPMD忽略該行。

FindBugs 有類似的東西嗎?

FindBugs 最初的方法涉及 XML 配置文件又名過濾器 這確實不如 PMD 解決方案方便,但 FindBugs 適用於字節碼,而不是源代碼,因此注釋顯然不是一種選擇。 例子:

<Match>
   <Class name="com.mycompany.Foo" />
   <Method name="bar" />
   <Bug pattern="DLS_DEAD_STORE_OF_CLASS_LITERAL" />
</Match>

但是,為了解決這個問題,FindBugs 后來引入了另一種基於注解的解決方案(參見SuppressFBWarnings ),您可以在類或方法級別使用(我認為比 XML 更方便)。 示例(可能不是最好的,但是,這只是一個示例):

@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
    value="HE_EQUALS_USE_HASHCODE", 
    justification="I know what I'm doing")

請注意,由於 FindBugs 3.0.0 SuppressWarnings已被棄用,取而代之的是@SuppressFBWarnings因為名稱與 Java 的SuppressWarnings沖突。

正如其他人提到的,您可以使用@SuppressFBWarnings注釋。 如果你不想或不能在你的代碼中添加另一個依賴,你可以自己在你的代碼中添加 Annotation,Findbugs 並不關心 Annotation 在哪個 Package 中。

@Retention(RetentionPolicy.CLASS)
public @interface SuppressFBWarnings {
    /**
     * The set of FindBugs warnings that are to be suppressed in
     * annotated element. The value can be a bug category, kind or pattern.
     *
     */
    String[] value() default {};

    /**
     * Optional documentation of the reason why the warning is suppressed
     */
    String justification() default "";
}

來源: https : //sourceforge.net/p/findbugs/feature-requests/298/#5e88

這是一個更完整的 XML 過濾器示例(上面的示例本身不起作用,因為它只顯示了一個片段並且缺少<FindBugsFilter>開始和結束標記):

<FindBugsFilter>
    <Match>
        <Class name="com.mycompany.foo" />
        <Method name="bar" />
        <Bug pattern="NP_BOOLEAN_RETURN_NULL" />
    </Match>
</FindBugsFilter>

如果您使用的是 Android Studio FindBugs 插件,請使用 File->Other Settings->Default Settings->Other Settings->FindBugs-IDEA->Filter->Exclude filter files->Add 瀏覽到您的 XML 過濾器文件。

更新 Gradle

dependencies {
    compile group: 'findbugs', name: 'findbugs', version: '1.0.0'
}

找到 FindBugs 報告

file:///Users/your_user/IdeaProjects/projectname/build/reports/findbugs/main.html

查找特定消息

發現錯誤

導入正確版本的注釋

import edu.umd.cs.findbugs.annotations.SuppressWarnings;

在違規代碼的正上方添加注釋

@SuppressWarnings("OUT_OF_RANGE_ARRAY_INDEX")

有關更多信息,請參見此處: findbugs Spring Annotation

在撰寫本文時(2018 年 5 月), FindBugs 似乎已被SpotBugs取代。 使用SuppressFBWarnings注釋需要使用 Java 8 或更高版本編譯您的代碼,並引入對spotbugs-annotations.jar的編譯時依賴性。

使用過濾器文件過濾 SpotBugs 規則沒有這樣的問題。 文檔在這里

雖然此處的其他答案是有效的,但它們並不是解決此問題的完整方法。

本着完整性的精神:

你需要在你的 pom 文件中有 findbugs 注釋——它們只是編譯時,所以你可以使用provided范圍:

<dependency>
  <groupId>com.google.code.findbugs</groupId>
  <artifactId>findbugs-annotations</artifactId>
  <version>3.0.1</version>
  <scope>provided</scope>
</dependency>

這允許使用@SuppressFBWarnings還有另一個提供@SuppressWarnings依賴@SuppressWarnings 不過,上面說的更清楚了。

然后在方法上方添加注釋:

例如

@SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE",
        justification = "Scanning generated code of try-with-resources")
@Override
public String get() {
    try (InputStream resourceStream =  owningType.getClassLoader().getResourceAsStream(resourcePath);
         BufferedReader reader = new BufferedReader(new InputStreamReader(resourceStream, UTF_8))) { ... }

這包括錯誤的名稱以及您禁用對其進行掃描的原因。

我要把這個留在這里: https : //stackoverflow.com/a/14509697/1356953

請注意,這適用於java.lang.SuppressWarnings因此無需使用單獨的注釋。

字段上的 @SuppressWarnings 僅抑制為該字段聲明報告的 findbugs 警告,而不是與該字段關聯的每個警告。

例如,這會抑制“Field only ever set to null”警告:

@SuppressWarnings("UWF_NULL_FIELD") String s = null; 我認為你能做的最好的事情就是將帶有警告的代碼隔離到最小的方法中,然后在整個方法上抑制警告。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM