Is there a way to ignore a single FindBugs warning?

后端 未结 7 1814
误落风尘
误落风尘 2020-11-28 21:40

With PMD, if you want to ignore a specific warning, you can use // NOPMD to have that line be ignored.

Is there something similar for FindBugs?

7条回答
  •  执笔经年
    2020-11-28 22:06

    While other answers on here are valid, they're not a full recipe for solving this.

    In the spirit of completeness:

    You need to have the findbugs annotations in your pom file - they're only compile time, so you can use the provided scope:

    
      com.google.code.findbugs
      findbugs-annotations
      3.0.1
      provided
    
    

    This allows the use of @SuppressFBWarnings there is another dependency which provides @SuppressWarnings. However, the above is clearer.

    Then you add the annotation above your method:

    E.g.

    @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))) { ... }
    

    This includes both the name of the bug and also a reason why you're disabling the scan for it.

提交回复
热议问题