Skip to content
Advertisement

How to suppress FindBugs warnings for fields or local variables?

I would like to suppress FindBugs warnings for specific fields or local variables. FindBugs documents that the Target can be Type, Field, Method, Parameter, Constructor, Package for its edu.umd.cs.findbugs.annotations.SuppressWarning annotation [1]. But it does not work for me to annotate the field, only when I annotate the method the warning gets suppressed.

Annotating a whole method seems to broad to me. Is there any way to suppress warnings on specific fields? There is another related question [2], but no answer.

[1] http://findbugs.sourceforge.net/manual/annotations.html

[2] Suppress FindBugs warnings in Eclipse

Demo code:

JavaScript

Advertisement

Answer

@SuppressFBWarnings on a field only suppresses findbugs warnings reported for that field declaration, not every warning associated with that field.

For example, this suppresses the “Field only ever set to null” warning:

JavaScript

I think the best you can do is isolate the code with the warning into the smallest method you can, then suppress the warning on the whole method.

Note: @SuppressWarnings was marked deprecated in favor of @SuppressFBWarnings

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement