From d3f1a1dbb160cbee39b8f8a0b5d0c348abaac655 Mon Sep 17 00:00:00 2001 From: cpovirk Date: Wed, 6 Sep 2023 14:10:33 -0700 Subject: [PATCH] Make `CompareToZero` treat methods like `Integer.compare` similar to `compareTo`. PiperOrigin-RevId: 563211769 --- .../errorprone/bugpatterns/CompareToZero.java | 26 +++++++++++++++++++ .../bugpatterns/CompareToZeroTest.java | 14 ++++++++++ 2 files changed, 40 insertions(+) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/CompareToZero.java b/core/src/main/java/com/google/errorprone/bugpatterns/CompareToZero.java index 52863c0130d8..061672bacfe0 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/CompareToZero.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/CompareToZero.java @@ -20,6 +20,7 @@ import static com.google.errorprone.matchers.Description.NO_MATCH; import static com.google.errorprone.matchers.Matchers.anyOf; import static com.google.errorprone.matchers.method.MethodMatchers.instanceMethod; +import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod; import static com.google.errorprone.util.ASTHelpers.constValue; import com.google.common.collect.ImmutableMap; @@ -74,6 +75,31 @@ public final class CompareToZero extends BugChecker implements MethodInvocationT private static final Matcher COMPARE_TO = anyOf( + staticMethod() + .onClassAny( + "com.google.common.primitives.Booleans", + "com.google.common.primitives.Chars", + "com.google.common.primitives.Doubles", + "com.google.common.primitives.Floats", + "com.google.common.primitives.Ints", + "com.google.common.primitives.Longs", + "com.google.common.primitives.Shorts", + "com.google.common.primitives.SignedBytes", + "com.google.common.primitives.UnsignedBytes", + "com.google.common.primitives.UnsignedInts", + "com.google.common.primitives.UnsignedLongs", + "com.google.protobuf.util.Durations", + "com.google.protobuf.util.Timestamps", + "java.lang.Boolean", + "java.lang.Byte", + "java.lang.Character", + "java.lang.Double", + "java.lang.Float", + "java.lang.Integer", + "java.lang.Long", + "java.lang.Short", + "java.util.Objects") + .named("compare"), instanceMethod().onDescendantOf("java.lang.Comparable").named("compareTo"), instanceMethod().onDescendantOf("java.util.Comparator").named("compare")); diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/CompareToZeroTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/CompareToZeroTest.java index 7a54aa2e92cf..7fccb39599a9 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/CompareToZeroTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/CompareToZeroTest.java @@ -44,6 +44,20 @@ public void positive() { .doTest(); } + @Test + public void positiveStaticCompare() { + compilationHelper + .addSourceLines( + "Test.java", + "class Test {", + " boolean test(boolean x, boolean y) {", + " // BUG: Diagnostic contains: compared", + " return Boolean.compare(x, y) == -1;", + " }", + "}") + .doTest(); + } + @Test public void positiveSuggestionForConsistency() { compilationHelper