Skip to content

Commit

Permalink
Audit EP checks for argumentless mock().
Browse files Browse the repository at this point in the history
DoNotMock is the only one that uses the arg, so I added a test and handled it.

DirectInvocationOnMockTest was sorta implicitly using it, in that it only wanted the one-arg version to exclude stubs with arguments.

PiperOrigin-RevId: 497253841
  • Loading branch information
graememorgan authored and Error Prone Team committed Dec 22, 2022
1 parent b92c9b1 commit 7340bdf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,13 @@ public static <T extends Tree> TypeExtractor<T> extractType(Matcher<T> m) {
public static TypeExtractor<MethodInvocationTree> extractFirstArg(
Matcher<MethodInvocationTree> m) {
return (tree, state) -> {
if (m.matches(tree, state)) {
if (!m.matches(tree, state)) {
return Optional.empty();
}
if (tree.getArguments().size() >= 1) {
return Optional.ofNullable(ASTHelpers.getType(tree.getArguments().get(0)));
}
return Optional.empty();
return Optional.ofNullable(ASTHelpers.targetType(state)).map(t -> t.type());
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@
import static com.google.errorprone.matchers.Description.NO_MATCH;
import static com.google.errorprone.matchers.Matchers.allOf;
import static com.google.errorprone.matchers.Matchers.anyMethod;
import static com.google.errorprone.matchers.Matchers.anyOf;
import static com.google.errorprone.matchers.Matchers.instanceMethod;
import static com.google.errorprone.matchers.Matchers.receiverOfInvocation;
import static com.google.errorprone.matchers.Matchers.staticMethod;
import static com.google.errorprone.suppliers.Suppliers.OBJECT_TYPE;
import static com.google.errorprone.suppliers.Suppliers.arrayOf;
import static com.google.errorprone.util.ASTHelpers.getReceiver;
import static com.google.errorprone.util.ASTHelpers.getSymbol;
import static com.google.errorprone.util.MoreAnnotations.getAnnotationValue;
import static java.lang.String.format;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
Expand Down Expand Up @@ -167,7 +171,15 @@ public Void visitAssignment(AssignmentTree tree, Void unused) {
}

private static final Matcher<ExpressionTree> MOCK =
staticMethod().onClass("org.mockito.Mockito").named("mock").withParameters("java.lang.Class");
anyOf(
staticMethod()
.onClass("org.mockito.Mockito")
.named("mock")
.withParameters("java.lang.Class"),
staticMethod()
.onClass("org.mockito.Mockito")
.named("mock")
.withParametersOfType(ImmutableList.of(arrayOf(OBJECT_TYPE))));

private static final Matcher<MethodInvocationTree> DO_CALL_REAL_METHOD =
allOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,7 @@ private boolean annotatedAtMostMock(Symbol symbol) {
}

private static final Matcher<ExpressionTree> MOCK =
anyOf(
staticMethod()
.onClass("org.mockito.Mockito")
.namedAnyOf("mock")
.withParameters("java.lang.Class"),
staticMethod().onClass("org.mockito.Mockito").namedAnyOf("spy"));
staticMethod().onClass("org.mockito.Mockito").namedAnyOf("mock", "spy");

private static final Matcher<VariableTree> MOCK_OR_SPY_ANNOTATED =
anyOf(hasAnnotation("org.mockito.Mock"), hasAnnotation("org.mockito.Spy"));
Expand Down

0 comments on commit 7340bdf

Please sign in to comment.