Standard Analyses

SemmleCode Professional comes with a wide range of pre-packaged analyses, from architectural properties and metrics, to statement-level checks for likely bugs and violations of best practice.


Inconsistent compareTo

This query finds classes that override compareTo but not equals. Usually, both should be overridden to ensure that they are consistent, i.e. that e1.compareTo(e2) == 0 is true if and only if e1.equals(e2) is true, for any non-null e1 and e2.

How to Interpret the Query Results

The query flags any such class and provides a list of all found occurrences in the results view.

How to Address the Query Results

Closely examine the flagged classes to ensure that comparison and equals methods are consistent.

Source Code
import default

from Class c
where c.fromSource() and 
      not c.declaresMethod("equals") and 
      c.declaresMethod("compareTo")
select c, "This class declares compareTo but inherits equals; "
          + "the two could be inconsistent"
References

API documentation for Comparable