diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/UnusedMethod.java b/core/src/main/java/com/google/errorprone/bugpatterns/UnusedMethod.java index f1a4f4636cc..4fa5633984a 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/UnusedMethod.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/UnusedMethod.java @@ -329,10 +329,20 @@ private void handleMethodSource(MethodTree tree) { sym.getRawAttributes().stream() .filter(a -> a.type.tsym.getQualifiedName().equals(name)) .findAny() - // get the annotation value array as a set of Names - .flatMap(a -> getAnnotationValue(a, "value")) + // get the annotation value array as a set of Names, + // normalizing unset value to the empty value .map( - y -> asStrings(y).map(state::getName).map(Name::toString).collect(toImmutableSet())) + a -> + getAnnotationValue(a, "value") + .map( + y -> + asStrings(y) + .map(state::getName) + .map(Name::toString) + .collect(toImmutableSet())) + .orElse(ImmutableSet.of())) + // if no explicit method sources were specified, use method name instead + .map(names -> names.isEmpty() ? ImmutableSet.of(sym.name.toString()) : names) // remove all potentially unused methods referenced by the @MethodSource .ifPresent( referencedNames -> diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/UnusedMethodTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/UnusedMethodTest.java index f90464a32b8..c3c10744629 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/UnusedMethodTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/UnusedMethodTest.java @@ -318,7 +318,7 @@ public void methodSource() { package org.junit.jupiter.params.provider; public @interface MethodSource { - String[] value(); + String[] value() default ""; } """) .addSourceLines( @@ -339,6 +339,36 @@ private static Stream parameters() { .doTest(); } + @Test + public void implicitMethodSource() { + helper + .addSourceLines( + "MethodSource.java", + """ + package org.junit.jupiter.params.provider; + + public @interface MethodSource { + String[] value() default ""; + } + """) + .addSourceLines( + "Test.java", + """ + import java.util.stream.Stream; + import org.junit.jupiter.params.provider.MethodSource; + + class Test { + @MethodSource + void test(String s) {} + + private static Stream test() { + return Stream.of(); + } + } + """) + .doTest(); + } + @Test public void qualifiedMethodSource() { helper @@ -348,7 +378,7 @@ public void qualifiedMethodSource() { package org.junit.jupiter.params.provider; public @interface MethodSource { - String[] value(); + String[] value() default ""; } """) .addSourceLines( @@ -378,7 +408,7 @@ public void nestedQualifiedMethodSource() { package org.junit.jupiter.params.provider; public @interface MethodSource { - String[] value(); + String[] value() default ""; } """) .addSourceLines(