IAmYourFaja
IAmYourFaja

Reputation: 56944

SuppressWarnings not working on FindBugs

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?!?

Upvotes: 5

Views: 18375

Answers (2)

TimK
TimK

Reputation: 4845

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;

Upvotes: 4

Péter Török
Péter Török

Reputation: 116306

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.

Upvotes: 2

Related Questions