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 @@ -31,7 +31,9 @@
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.IdentifierTree;
import com.sun.source.tree.MemberSelectTree;
import com.sun.source.tree.Tree.Kind;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import org.immutables.value.Value.Immutable;

Expand All @@ -58,8 +60,11 @@ public Description matchBinary(BinaryTree tree, VisitorState state) {

EqualsZeroExpression equalsZeroExpression = maybeEqualsZeroExpression.get();
ExpressionTree operand = equalsZeroExpression.operand();
ExpressionTree collectionInstance = ASTHelpers.getReceiver(operand);
if (!Objects.equals(operand.getKind(), Kind.METHOD_INVOCATION)) {
return Description.NO_MATCH;
}

ExpressionTree collectionInstance = ASTHelpers.getReceiver(operand);
if (collectionInstance == null || isExpressionThis(collectionInstance)) {
return Description.NO_MATCH;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,28 @@ public void test_size_on_non_collection() {
.doTest();
}

@Test
public void test_equals_on_non_collection() {
fix().addInputLines(
"TestNonCollection.java",
"class TestNonCollection {",
" public int size() {",
" return 0;",
" }",
" public boolean foo(String key) {",
" String current = \"x\";",
" int comparisonResult = current.compareTo(key);",
" if (comparisonResult == 0) {",
" return true;",
" } else {",
" return false;",
" }",
" }",
"}")
.expectUnchanged()
.doTest();
}

@Test
public void test_qualified_this() {
fix().addInputLines(
Expand Down
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-2544.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: 'Fix #2543 CardinalityEqualsZero equality comparison check'
links:
- https://github.com/palantir/gradle-baseline/pull/2544