SuppressWarnings not working on FindBugs

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

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

When you run FindBugs in this class, you will receive a warning that the name = nm task has completed, indicating: Unread field: com.me.myorg.LogItem.name .

So I tried to add 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 (compilation) error in Eclipse by indicating:

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

So, I tried to add FindBugs' SuppressWarnings in the field itself, but after running FindBugs again in the FindBugs class, it still complains about the same line of code and for the same reason. I even tried adding the SuppressWarnings method to the setName method, and there is still no difference.

How (for sure!) Do I use this annotation for silent FindBugs?!?

+2
source share
2 answers

Put the annotation in the box and correct the error identifier. This works for me:

 public class LogItem { @edu.umd.cs.findbugs.annotations.SuppressWarnings("URF_UNREAD_FIELD") private String name; 
+2
source

I always used the built-in java.lang.SuppressWarnings , not FindBugs, and it has worked so far. In addition, for specific statements, you may need to store the statement in the same line immediately after the annotation. how

  @SuppressWarnings("NP") name = nm; 

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

+2
source

Source: https://habr.com/ru/post/1492815/


All Articles