views:

1916

answers:

4

FindBugs has found a potential bug in my code. But it is not a bug.

Is it possible to mark this occurrence as 'not a bug' AND have it removed from the bug list?

I have documented quite clearly why for each case it is not a bug.

For example. A class implements the comparable interface. it has the compareTo method. I have however not overridden the equals method.

FindBugs does not like this as the JavaDocs state that it is recommended that

(x.compareTo(y)==0) == (x.equals(y))

Although in my case the above condition is and always will be true.

+4  A: 

Probably by adding a filter as parameter of findbugs

Match clauses can only match information that is actually contained in the bug instances

<Match>
   <Class name="com.foobar.MyClass" />
   <Method name="myMethod" />
   <Bug pattern="EQ_COMPARETO_USE_OBJECT_EQUALS" />
</Match>
VonC
+17  A: 

Instead of using filters, you can also use the SuppressWarnings annotation. You must use the annotation out of the findbugs package, meaning you either need an import or use the fully qualified name of it. This is because other than the SuppressWarnings from the JDK it has retention "Class", which is needed because findbugs operates on the compiled bytecode instead of source code.

Example:

@edu.umd.cs.findbugs.annotations.SuppressWarnings(
    value="EQ_COMPARETO_USE_OBJECT_EQUALS", 
    justification="because I know better")

There's one corner case where you probably should not be using the annotation: If your code is library code that ends up in a jar, that could be used by other projects and you're still on Java5. The reason for this is a bug in the JDK which crashes javac if the annotation is not in the classpath.

WMR
I find your solution more adapted to the initial question. +1
VonC
This was exactly what I was looking for, thank you. I'm curious if there exists any plugin to Eclipse to generate these annotations automatically?
dimo414
I don't know of any eclipse plugin, that can generate these annotations for you
WMR
A: 

on another hand - if you are using such automated code review tool that highlights potential problems according to widely known recommendations, maybe you should adhere to it's recommendations? think of people who will be maintaining code after you.

what if the code changes after time?

miceuz
the question is specifically about what to do when you know the reported error is wrong, thus 'adhering to the recommendation' is probably no longer useful/possible
pvgoddijn
A: 

You've -got- to be able to suppress warnings. Code tools like that aren't perfect, and no recommendation is universal.