From 457c855f5dfae3fbc09333317cd0b6307f7024d1 Mon Sep 17 00:00:00 2001 From: Petr Portnov Date: Thu, 28 Aug 2025 16:22:42 +0300 Subject: [PATCH] fix: support implicit `@MethodSource` for `UnusedMethod` --- .../errorprone/bugpatterns/UnusedMethod.java | 12 ++++--- .../bugpatterns/UnusedMethodTest.java | 36 +++++++++++++++++-- 2 files changed, 41 insertions(+), 7 deletions(-) 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 522579538e2..ccfe6d9dd72 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/UnusedMethod.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/UnusedMethod.java @@ -303,10 +303,14 @@ 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")) - .map( - y -> asStrings(y).map(state::getName).map(Name::toString).collect(toImmutableSet())) + // get the annotation value array as a set of Names, + // normalizing unset value to the empty value + .map(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() ? Set.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 0608003ab8d..e919e647cc7 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() {} + + 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(