From 5b56e10d84e0fb671e74b0309826696272cb0a68 Mon Sep 17 00:00:00 2001 From: ghm Date: Thu, 10 Nov 2022 08:17:53 -0800 Subject: [PATCH] Rename ProtoFieldNullComparison to something less intrinsically proto-ish, in preparation for generalizing it a bit. PiperOrigin-RevId: 487541689 --- ...ison.java => ImpossibleNullComparison.java} | 16 ++++++++++------ .../scanner/BuiltInCheckerSuppliers.java | 4 ++-- ....java => ImpossibleNullComparisonTest.java} | 18 +++++++++--------- ...mparison.md => ImpossibleNullComparison.md} | 0 4 files changed, 21 insertions(+), 17 deletions(-) rename core/src/main/java/com/google/errorprone/bugpatterns/{ProtoFieldNullComparison.java => ImpossibleNullComparison.java} (97%) rename core/src/test/java/com/google/errorprone/bugpatterns/{ProtoFieldNullComparisonTest.java => ImpossibleNullComparisonTest.java} (96%) rename docs/bugpattern/{ProtoFieldNullComparison.md => ImpossibleNullComparison.md} (100%) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/ProtoFieldNullComparison.java b/core/src/main/java/com/google/errorprone/bugpatterns/ImpossibleNullComparison.java similarity index 97% rename from core/src/main/java/com/google/errorprone/bugpatterns/ProtoFieldNullComparison.java rename to core/src/main/java/com/google/errorprone/bugpatterns/ImpossibleNullComparison.java index 599bcf7ef8b..14f88d40903 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/ProtoFieldNullComparison.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/ImpossibleNullComparison.java @@ -70,8 +70,12 @@ import javax.annotation.Nullable; /** Matches comparison of proto fields to {@code null}. */ -@BugPattern(summary = "Protobuf fields cannot be null.", severity = ERROR) -public class ProtoFieldNullComparison extends BugChecker implements CompilationUnitTreeMatcher { +@BugPattern( + summary = "Protobuf fields cannot be null.", + altNames = "ProtoFieldNullComparison", + severity = ERROR) +public final class ImpossibleNullComparison extends BugChecker + implements CompilationUnitTreeMatcher { // TODO(b/111109484): Try to consolidate these with NullnessPropagationTransfer. private static final Matcher CHECK_NOT_NULL = @@ -138,23 +142,23 @@ private static boolean isNull(ExpressionTree tree) { private final boolean matchTestAssertions; - public ProtoFieldNullComparison(ErrorProneFlags flags) { + public ImpossibleNullComparison(ErrorProneFlags flags) { this.matchTestAssertions = flags.getBoolean("ProtoFieldNullComparison:MatchTestAssertions").orElse(true); } @Override public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) { - ProtoNullComparisonScanner scanner = new ProtoNullComparisonScanner(state); + NullComparisonScanner scanner = new NullComparisonScanner(state); scanner.scan(state.getPath(), null); return Description.NO_MATCH; } - private class ProtoNullComparisonScanner extends TreePathScanner { + private class NullComparisonScanner extends TreePathScanner { private final Map effectivelyFinalValues = new HashMap<>(); private final VisitorState state; - private ProtoNullComparisonScanner(VisitorState state) { + private NullComparisonScanner(VisitorState state) { this.state = state; } diff --git a/core/src/main/java/com/google/errorprone/scanner/BuiltInCheckerSuppliers.java b/core/src/main/java/com/google/errorprone/scanner/BuiltInCheckerSuppliers.java index c9e65e207a7..a36f0c3c94d 100644 --- a/core/src/main/java/com/google/errorprone/scanner/BuiltInCheckerSuppliers.java +++ b/core/src/main/java/com/google/errorprone/scanner/BuiltInCheckerSuppliers.java @@ -156,6 +156,7 @@ import com.google.errorprone.bugpatterns.ImmutableMemberCollection; import com.google.errorprone.bugpatterns.ImmutableSetForContains; import com.google.errorprone.bugpatterns.ImplementAssertionWithChaining; +import com.google.errorprone.bugpatterns.ImpossibleNullComparison; import com.google.errorprone.bugpatterns.Incomparable; import com.google.errorprone.bugpatterns.IncompatibleModifiersChecker; import com.google.errorprone.bugpatterns.InconsistentCapitalization; @@ -285,7 +286,6 @@ import com.google.errorprone.bugpatterns.PrivateSecurityContractProtoAccess; import com.google.errorprone.bugpatterns.ProtectedMembersInFinalClass; import com.google.errorprone.bugpatterns.ProtoBuilderReturnValueIgnored; -import com.google.errorprone.bugpatterns.ProtoFieldNullComparison; import com.google.errorprone.bugpatterns.ProtoRedundantSet; import com.google.errorprone.bugpatterns.ProtoStringFieldReferenceEquality; import com.google.errorprone.bugpatterns.ProtoTruthMixedDescriptors; @@ -669,6 +669,7 @@ public static ScannerSupplier errorChecks() { IdentityHashMapBoxing.class, IgnoredPureGetter.class, ImmutableChecker.class, + ImpossibleNullComparison.class, Incomparable.class, IncompatibleArgumentType.class, IncompatibleModifiersChecker.class, @@ -736,7 +737,6 @@ public static ScannerSupplier errorChecks() { PreconditionsInvalidPlaceholder.class, PrivateSecurityContractProtoAccess.class, ProtoBuilderReturnValueIgnored.class, - ProtoFieldNullComparison.class, ProtoStringFieldReferenceEquality.class, ProtoTruthMixedDescriptors.class, ProtocolBufferOrdinal.class, diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/ProtoFieldNullComparisonTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/ImpossibleNullComparisonTest.java similarity index 96% rename from core/src/test/java/com/google/errorprone/bugpatterns/ProtoFieldNullComparisonTest.java rename to core/src/test/java/com/google/errorprone/bugpatterns/ImpossibleNullComparisonTest.java index db81b06f823..af2ad8c8102 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/ProtoFieldNullComparisonTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/ImpossibleNullComparisonTest.java @@ -26,10 +26,10 @@ */ @RunWith(JUnit4.class) @Ignore("b/130670448") -public final class ProtoFieldNullComparisonTest { +public final class ImpossibleNullComparisonTest { private final CompilationTestHelper compilationHelper = - CompilationTestHelper.newInstance(ProtoFieldNullComparison.class, getClass()); + CompilationTestHelper.newInstance(ImpossibleNullComparison.class, getClass()); @Test public void scalarCases() { @@ -144,7 +144,7 @@ public void testMessageOrBuilderGetField() { "import com.google.errorprone.bugpatterns.proto.ProtoTest.TestProtoMessage;", "public class Test {", " public boolean doIt(TestProtoMessage mob, FieldDescriptor f) {", - " // BUG: Diagnostic contains: ProtoFieldNullComparison", + " // BUG: Diagnostic contains:", " return mob.getField(f) == null;", " }", "}") @@ -161,7 +161,7 @@ public void testMessageOrBuilderGetFieldCast() { "public class Test {", " public boolean doIt(TestProtoMessage mob, FieldDescriptor f) {", " String s = ((String) mob.getField(f));", - " // BUG: Diagnostic contains: ProtoFieldNullComparison", + " // BUG: Diagnostic contains:", " return s == null;", " }", "}") @@ -177,7 +177,7 @@ public void testExtendableMessageGetExtension1param() { "import com.google.errorprone.bugpatterns.proto.ProtoTest.TestProtoMessage;", "public class Test {", " public void test(TestProtoMessage e, ExtensionLite extensionLite) {", - " // BUG: Diagnostic contains: ProtoFieldNullComparison", + " // BUG: Diagnostic contains:", " boolean a = e.getExtension(extensionLite) == null;", " }", "}") @@ -193,7 +193,7 @@ public void testMessageOrBuilderGetRepeatedField() { "import com.google.errorprone.bugpatterns.proto.ProtoTest.TestProtoMessage;", "public class Test {", " public void doIt(TestProtoMessage mob, FieldDescriptor f) {", - " // BUG: Diagnostic contains: ProtoFieldNullComparison", + " // BUG: Diagnostic contains:", " boolean a = mob.getRepeatedField(f, 0) == null;", " }", "}") @@ -209,7 +209,7 @@ public void testExtendableMessageGetExtension2param() { "import com.google.errorprone.bugpatterns.proto.ProtoTest.TestProtoMessage;", "public class Test {", " public void test(TestProtoMessage e, ExtensionLite extensionLite) {", - " // BUG: Diagnostic contains: ProtoFieldNullComparison", + " // BUG: Diagnostic contains:", " boolean a = e.getExtension(extensionLite, 0) == null;", " }", "}") @@ -228,7 +228,7 @@ public void repeated() { "public class Test {", " public void test(ExtensionLite> e, TestProtoMessage message) {", - " // BUG: Diagnostic contains: ProtoFieldNullComparison", + " // BUG: Diagnostic contains:", " boolean y = message.getExtension(e) == null;", " }", "}") @@ -255,7 +255,7 @@ public void repeated2() { " ImmutableList.Builder extensionList = ImmutableList.builder();", " int extensionCount = mob.getExtensionCount(extension);", " for (int extensionIndex = 0; extensionIndex < extensionCount; ++extensionIndex) {", - " // BUG: Diagnostic contains: ProtoFieldNullComparison", + " // BUG: Diagnostic contains:", " boolean y = mob.getExtension(extension) == null;", " extensionList.add(mob.getExtension(extension));", " }", diff --git a/docs/bugpattern/ProtoFieldNullComparison.md b/docs/bugpattern/ImpossibleNullComparison.md similarity index 100% rename from docs/bugpattern/ProtoFieldNullComparison.md rename to docs/bugpattern/ImpossibleNullComparison.md