Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,33 +122,12 @@ public final class ImpossibleNullComparison extends BugChecker
private static final ImmutableSet<Kind> COMPARISON_OPERATORS =
Sets.immutableEnumSet(Kind.EQUAL_TO, Kind.NOT_EQUAL_TO);

private static final Matcher<ExpressionTree> EXTENSION_METHODS_WITH_FIX_LEGACY =
instanceMethod()
.onDescendantOf(EXTENDABLE_MESSAGE_CLASS)
.named("getExtension")
.withParameters(EXTENSION_LITE_CLASS);

private static final Matcher<ExpressionTree> EXTENSION_METHODS_WITH_FIX =
instanceMethod()
.onDescendantOfAny(EXTENDABLE_MESSAGE_CLASS, EXTENDABLE_MESSAGE_LITE_CLASS)
.named("getExtension")
.withParameters(EXTENSION_LITE_CLASS);

private static final Matcher<ExpressionTree> EXTENSION_METHODS_WITH_NO_FIX_LEGACY =
anyOf(
instanceMethod()
.onDescendantOf(MESSAGE_OR_BUILDER_CLASS)
.named("getRepeatedField")
.withParameters(FIELD_DESCRIPTOR_CLASS, "int"),
instanceMethod()
.onDescendantOf(EXTENDABLE_MESSAGE_CLASS)
.named("getExtension")
.withParameters(EXTENSION_LITE_CLASS, "int"),
instanceMethod()
.onDescendantOf(MESSAGE_OR_BUILDER_CLASS)
.named("getField")
.withParameters(FIELD_DESCRIPTOR_CLASS));

private static final Matcher<ExpressionTree> EXTENSION_METHODS_WITH_NO_FIX =
anyOf(
instanceMethod()
Expand Down Expand Up @@ -593,12 +572,10 @@ private String generateFix(
if (!protoReceiver(checkOrBuilder).matches(tree, state)) {
return null;
}
if ((checkOrBuilder ? EXTENSION_METHODS_WITH_NO_FIX : EXTENSION_METHODS_WITH_NO_FIX_LEGACY)
.matches(tree, state)) {
if (EXTENSION_METHODS_WITH_NO_FIX.matches(tree, state)) {
return GetterTypes::emptyFix;
}
if ((checkOrBuilder ? EXTENSION_METHODS_WITH_FIX : EXTENSION_METHODS_WITH_FIX_LEGACY)
.matches(tree, state)) {
if (EXTENSION_METHODS_WITH_FIX.matches(tree, state)) {
// If the extension represents a repeated field (i.e.: it's an ExtensionLite<T, List<R>>),
// the suggested fix from get->has isn't appropriate,so we shouldn't suggest a replacement

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -689,48 +689,4 @@ public void test(TestProtoMessageOrBuilder orBld) {
""")
.doTest();
}

@Test
public void liteExtendableMessage_flagEnabled() {
compilationHelper
.addSourceLines(
"Test.java",
"""
import com.google.protobuf.ExtensionLite;
import com.google.protobuf.GeneratedMessageLite;

public class Test {
public static <MessageType extends GeneratedMessageLite.ExtendableMessage<MessageType, ?>, Type>
void test(
GeneratedMessageLite.ExtendableMessage<MessageType, ?> msg,
ExtensionLite<MessageType, Type> extension) {
// BUG: Diagnostic contains: msg.hasExtension(extension)
if (msg.getExtension(extension) != null) {}
}
}
""")
.doTest();
}

@Test
public void liteExtendableMessage_flagDisabled() {
compilationHelper
.setArgs("-XepOpt:ImpossibleNullComparison:CheckOrBuilder=false")
.addSourceLines(
"Test.java",
"""
import com.google.protobuf.ExtensionLite;
import com.google.protobuf.GeneratedMessageLite;

public class Test {
public static <MessageType extends GeneratedMessageLite.ExtendableMessage<MessageType, ?>, Type>
void test(
GeneratedMessageLite.ExtendableMessage<MessageType, ?> msg,
ExtensionLite<MessageType, Type> extension) {
if (msg.getExtension(extension) != null) {}
}
}
""")
.doTest();
}
}
Loading