From af37d35d208d37e9658038127d35afdd41325da4 Mon Sep 17 00:00:00 2001 From: ghm Date: Mon, 26 Feb 2024 09:35:44 -0800 Subject: [PATCH] ImpossibleNullComparison: emit empty fixes. There's a subtle logic error here: empty fixes are meant to be emitted, there's just no fix. PiperOrigin-RevId: 610434295 --- .../errorprone/bugpatterns/ImpossibleNullComparison.java | 4 +++- .../errorprone/bugpatterns/ImpossibleNullComparisonTest.java | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/ImpossibleNullComparison.java b/core/src/main/java/com/google/errorprone/bugpatterns/ImpossibleNullComparison.java index 5066cc54281..a8120f2dfc5 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/ImpossibleNullComparison.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/ImpossibleNullComparison.java @@ -144,11 +144,13 @@ private static boolean isNull(ExpressionTree tree) { "com.google.protobuf.GeneratedMessageLite", "com.google.protobuf.GeneratedMessage"); private final boolean matchTestAssertions; + private final boolean emitEmptyFixes; @Inject ImpossibleNullComparison(ErrorProneFlags flags) { this.matchTestAssertions = flags.getBoolean("ProtoFieldNullComparison:MatchTestAssertions").orElse(true); + this.emitEmptyFixes = flags.getBoolean("ImmutableNullComparison:EmitEmptyFixes").orElse(true); } @Override @@ -248,7 +250,7 @@ public Void visitMethodInvocation(MethodInvocationTree node, Void unused) { } getFixer(argument, subState) .map(f -> problemType.fix(f, node, subState)) - .filter(f -> !f.isEmpty()) + .filter(f -> emitEmptyFixes || !f.isEmpty()) .map(f -> describeMatch(node, f)) .ifPresent(state::reportMatch); diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/ImpossibleNullComparisonTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/ImpossibleNullComparisonTest.java index 556cd876d4a..28bd983d00e 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/ImpossibleNullComparisonTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/ImpossibleNullComparisonTest.java @@ -173,12 +173,15 @@ public void extendableMessageGetExtension1param() { compilationHelper .addSourceLines( "Test.java", + "import static org.junit.Assert.assertNotNull;", "import com.google.protobuf.ExtensionLite;", "import com.google.errorprone.bugpatterns.proto.ProtoTest.TestProtoMessage;", "public class Test {", " public void test(TestProtoMessage e, ExtensionLite extensionLite) {", " // BUG: Diagnostic contains:", " boolean a = e.getExtension(extensionLite) == null;", + " // BUG: Diagnostic contains:", + " assertNotNull(e.getExtension(extensionLite));", " }", "}") .doTest();