diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/AbstractMockChecker.java b/core/src/main/java/com/google/errorprone/bugpatterns/AbstractMockChecker.java index d7a827470f58..3cd390b603ce 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/AbstractMockChecker.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/AbstractMockChecker.java @@ -168,10 +168,13 @@ public static TypeExtractor extractType(Matcher m) { public static TypeExtractor extractFirstArg( Matcher 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()); }; } diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/DirectInvocationOnMock.java b/core/src/main/java/com/google/errorprone/bugpatterns/DirectInvocationOnMock.java index b8c51c412068..87f98aa14703 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/DirectInvocationOnMock.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/DirectInvocationOnMock.java @@ -22,6 +22,7 @@ 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; @@ -167,7 +168,12 @@ public Void visitAssignment(AssignmentTree tree, Void unused) { } private static final Matcher 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").withNoParameters()); private static final Matcher DO_CALL_REAL_METHOD = allOf( diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/MockNotUsedInProduction.java b/core/src/main/java/com/google/errorprone/bugpatterns/MockNotUsedInProduction.java index fab61e95b5ce..4b1a8c54fc6e 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/MockNotUsedInProduction.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/MockNotUsedInProduction.java @@ -180,12 +180,7 @@ private boolean annotatedAtMostMock(Symbol symbol) { } private static final Matcher 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 MOCK_OR_SPY_ANNOTATED = anyOf(hasAnnotation("org.mockito.Mock"), hasAnnotation("org.mockito.Spy")); diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/DirectInvocationOnMockTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/DirectInvocationOnMockTest.java index 4799062d0e2b..d5ee33e6698e 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/DirectInvocationOnMockTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/DirectInvocationOnMockTest.java @@ -47,6 +47,22 @@ public void directInvocationOnMock() { .doTest(); } + @Test + public void directInvocationOnMock_createdUsingArgumentlessMock() { + helper + .addSourceLines( + "Test.java", + "import static org.mockito.Mockito.mock;", + "class Test {", + " public void test() {", + " Test test = mock();", + " // BUG: Diagnostic contains: test", + " test.test();", + " }", + "}") + .doTest(); + } + @Test public void directInvocationOnMockAssignment() { helper