Skip to content

Commit

Permalink
ImpossibleNullComparison: emit empty fixes.
Browse files Browse the repository at this point in the history
There's a subtle logic error here: empty fixes are meant to be emitted, there's just no fix.

PiperOrigin-RevId: 610434295
  • Loading branch information
graememorgan authored and Error Prone Team committed Feb 26, 2024
1 parent 297019c commit af37d35
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit af37d35

Please sign in to comment.