Reports subtraction in compareTo() methods and methods implementing java.util.Comparator.compare(). While it is a common idiom to use the results of integer subtraction as the result of a compareTo() method, this construct may cause subtle and difficult bugs in cases of integer overflow. Comparing the integer values directly and returning -1, 0, or 1 is better practice in most cases.

However subtraction will never overflow when both operands are byte or short values, because the result will always fit inside an int. Additionally subtraction on int numbers greater than or equal to 0 will never overflow. Therefore this inspection tries not to warn in those cases. Methods that always return zero or greater can be specified in this inspection's settings.

Use the table below to specify methods which are safe to use inside a subtraction. Methods are safe when they return an int value that is always greater than or equal to 0.