5

I ran FindBugs on my Eclipse project and got a potential bug warning that I would like to suppress for a specific reason (outside the context of this question). Here's the code:

public class LogItem {
    private String name;

    private void setName(final String nm) {
        name = nm;
    }
}

When you run FindBugs on this class is gives you a warning on the name = nm assignment operation, stating: Unread field: com.me.myorg.LogItem.name.

So I tried adding this:

    private void setName(final String nm) {
        @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "NP", justification = "Because I can")
        name = nm;
    }

When I do this I get a syntax (compile) error in Eclipse, stating:

Duplicate local variable nm; name cannot be resolved to a type.

So then I tried adding the FindBugs' SuppressWarnings on the field itself, but after re-running FindBugs on the class, FindBugs is still complaining about the same line of code and for the same reason. I even tried adding the SuppressWarnings to the setName method, and still no difference.

How (exactly!) do I use this annotation to quiet FindBugs?!?

IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756

2 Answers2

4

Put the annotation on the field and fix the bug identifier. This works for me:

public class LogItem {
    @edu.umd.cs.findbugs.annotations.SuppressWarnings("URF_UNREAD_FIELD")
    private String name;
TimK
  • 4,635
  • 2
  • 27
  • 27
2

I have always used the built-in java.lang.SuppressWarnings, not that of FindBugs, and it worked so far. Also, for specific statements you may need to keep the statement on the same line right after the annotation. Like

    @SuppressWarnings("NP") name = nm;

Also, are you sure "NP" is a valid warning identifier here? I would try "unused" if nothing else seems to work.

Péter Török
  • 114,404
  • 31
  • 268
  • 329
  • Thanks @Peter but none of these suggestions are working. When I replace the FindBugs `SuppressWarnings` with the exact line of code your answer contains I still get a compiler error: `Multiple markers at this line - Duplicate local variable nm - name cannot be resolved to a type - Syntax error on token "=", delete this token - Unread field: com.me.myorg.LogItem.name`. Any thoughts? – IAmYourFaja Jun 04 '12 at 17:52
  • "Duplicate local variable" suggests there's something in your code that isn't posted in your question...? – Louis Wasserman Jun 04 '12 at 18:55
  • @LouisWasserman, I got the same error from eclipse with just that class. The parser seems to make a bad guess about what you mean when the annotation is in with statements. – TimK Jun 04 '12 at 21:19
  • `nm` is only defined once; that shouldn't have anything to do with annotations, no? – Louis Wasserman Jun 04 '12 at 21:30
  • I think what's happening is that the eclipse compiler assumes that "name" is a type because that's what's expected after an annotation. Then "=" is not allowed so it ignores it. So it thinks you're declaring a local called "nm" with type "name". – TimK Jun 04 '12 at 21:36
  • `@java.lang.SuppressWarnings` cannot work with FindBugs, because it has `RetentionPolicy.SOURCE` (i.e. it's discarded by the compiler). FindBugs analyzes bytecode, which is why it has its own suppress annotation with `RetentionPolicy.CLASS`. – Andres F. Jul 27 '16 at 14:33