Skip to content

Commit af37d35

Browse files
graememorganError Prone Team
authored andcommitted
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
1 parent 297019c commit af37d35

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

core/src/main/java/com/google/errorprone/bugpatterns/ImpossibleNullComparison.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,13 @@ private static boolean isNull(ExpressionTree tree) {
144144
"com.google.protobuf.GeneratedMessageLite", "com.google.protobuf.GeneratedMessage");
145145

146146
private final boolean matchTestAssertions;
147+
private final boolean emitEmptyFixes;
147148

148149
@Inject
149150
ImpossibleNullComparison(ErrorProneFlags flags) {
150151
this.matchTestAssertions =
151152
flags.getBoolean("ProtoFieldNullComparison:MatchTestAssertions").orElse(true);
153+
this.emitEmptyFixes = flags.getBoolean("ImmutableNullComparison:EmitEmptyFixes").orElse(true);
152154
}
153155

154156
@Override
@@ -248,7 +250,7 @@ public Void visitMethodInvocation(MethodInvocationTree node, Void unused) {
248250
}
249251
getFixer(argument, subState)
250252
.map(f -> problemType.fix(f, node, subState))
251-
.filter(f -> !f.isEmpty())
253+
.filter(f -> emitEmptyFixes || !f.isEmpty())
252254
.map(f -> describeMatch(node, f))
253255
.ifPresent(state::reportMatch);
254256

core/src/test/java/com/google/errorprone/bugpatterns/ImpossibleNullComparisonTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,15 @@ public void extendableMessageGetExtension1param() {
173173
compilationHelper
174174
.addSourceLines(
175175
"Test.java",
176+
"import static org.junit.Assert.assertNotNull;",
176177
"import com.google.protobuf.ExtensionLite;",
177178
"import com.google.errorprone.bugpatterns.proto.ProtoTest.TestProtoMessage;",
178179
"public class Test {",
179180
" public void test(TestProtoMessage e, ExtensionLite extensionLite) {",
180181
" // BUG: Diagnostic contains:",
181182
" boolean a = e.getExtension(extensionLite) == null;",
183+
" // BUG: Diagnostic contains:",
184+
" assertNotNull(e.getExtension(extensionLite));",
182185
" }",
183186
"}")
184187
.doTest();

0 commit comments

Comments
 (0)