Skip to content

Commit

Permalink
Handle containsExactlyElementsIn, etc in TruthSelfEquals.
Browse files Browse the repository at this point in the history
Flume: unknown commit
PiperOrigin-RevId: 580526558
  • Loading branch information
graememorgan authored and Error Prone Team committed Nov 8, 2023
1 parent 962f5d1 commit 6eaf51d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ public final class TruthSelfEquals extends BugChecker implements MethodInvocatio
instanceMethod().anyClass().namedAnyOf("isNotEqualTo", "isNotSameInstanceAs"),
TruthSelfEquals::receiverSameAsParentsArgument);

private static final Matcher<MethodInvocationTree> OTHER_MATCHER =
allOf(
instanceMethod()
.anyClass()
.namedAnyOf("containsExactlyElementsIn", "containsAtLeastElementsIn", "areEqualTo"),
TruthSelfEquals::receiverSameAsParentsArgument);

private static final Matcher<ExpressionTree> ASSERT_THAT =
anyOf(
staticMethod().anyClass().named("assertThat"),
Expand All @@ -66,21 +73,20 @@ public final class TruthSelfEquals extends BugChecker implements MethodInvocatio
.named("that"));

@Override
public Description matchMethodInvocation(
MethodInvocationTree methodInvocationTree, VisitorState state) {
if (methodInvocationTree.getArguments().isEmpty()) {
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (tree.getArguments().isEmpty()) {
return NO_MATCH;
}
Description.Builder description = buildDescription(methodInvocationTree);
ExpressionTree toReplace = methodInvocationTree.getArguments().get(0);
if (EQUALS_MATCHER.matches(methodInvocationTree, state)) {
Description.Builder description = buildDescription(tree);
ExpressionTree toReplace = tree.getArguments().get(0);
if (EQUALS_MATCHER.matches(tree, state)) {
description
.setMessage(
generateSummary(getSymbol(methodInvocationTree).getSimpleName().toString(), "passes"))
.addFix(suggestEqualsTesterFix(methodInvocationTree, toReplace, state));
} else if (NOT_EQUALS_MATCHER.matches(methodInvocationTree, state)) {
description.setMessage(
generateSummary(getSymbol(methodInvocationTree).getSimpleName().toString(), "fails"));
.setMessage(generateSummary(getSymbol(tree).getSimpleName().toString(), "passes"))
.addFix(suggestEqualsTesterFix(tree, toReplace, state));
} else if (NOT_EQUALS_MATCHER.matches(tree, state)) {
description.setMessage(generateSummary(getSymbol(tree).getSimpleName().toString(), "fails"));
} else if (OTHER_MATCHER.matches(tree, state)) {
description.setMessage(generateSummary(getSymbol(tree).getSimpleName().toString(), "passes"));
} else {
return NO_MATCH;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,20 @@ public void customReceiver() {
"}")
.doTest();
}

@Test
public void iterables() {
compilationHelper
.addSourceLines(
"Test.java",
"import static com.google.common.truth.Truth.assertThat;",
"import java.util.List;",
"abstract class Test {",
" void test(List<String> xs) {",
" // BUG: Diagnostic contains:",
" assertThat(xs).containsExactlyElementsIn(xs);",
" }",
"}")
.doTest();
}
}

0 comments on commit 6eaf51d

Please sign in to comment.