Flag non-relational operations and unary negation on compareTo results in `CompareToZero`.
The `CompareToZero` bug pattern previously only flagged addition, subtraction, and specific comparisons on `compareTo` results. This change updates the check to flag all non-relational binary operations (including arithmetic, string concatenation, and bitwise operations) as well as unary negation (`-x.compareTo(y)`).
Specific diagnostic messages are added for arithmetic operations (clarifying `{-1, 0, +1}` range assumptions and overflow risks), unary negation, and general non-relational operations, recommending `Integer.signum`.
The check is temporarily disabled via Error Prone flags until depot violations are cleaned up.
PiperOrigin-RevId: 970134346
Error Prone is a static analysis tool for Java that catches common programming mistakes at compile-time.
public class ShortSet { public static void main (String[] args) { Set<Short> s = new HashSet<>(); for (short i = 0; i < 100; i++) { s.add(i); s.remove(i - 1); } System.out.println(s.size()); } }
error: [CollectionIncompatibleType] Argument 'i - 1' should not be passed to this method;
its type int is not compatible with its collection's type argument Short
s.remove(i - 1);
^
(see https://errorprone.info/bugpattern/CollectionIncompatibleType)
1 error
Our documentation is at errorprone.info.
Error Prone works with Bazel, Maven, Ant, and Gradle. See our installation instructions for details.
Developing and building Error Prone is documented on the wiki.