From 0dbf04a0bfbb3398ca06f20856ef0dcb4ef595ff Mon Sep 17 00:00:00 2001 From: Petr Portnov | PROgrm_JARvis Date: Wed, 18 Mar 2026 10:16:51 -0700 Subject: [PATCH] fix: support implicit `@MethodSource` to `UnusedMethod` Currently, `UnusedMethod` check does not recognize implicitly referenced JUnit `@MethodSource`, e.g. the ones which are derived from test method name when none are specified explicitly. This PR fixes this issue. Fixes #5289 Fixes #5210 COPYBARA_INTEGRATE_REVIEW=https://github.com/google/error-prone/pull/5210 from JarvisCraft:junit-implicit-method-source 457c855f5dfae3fbc09333317cd0b6307f7024d1 PiperOrigin-RevId: 885662324 --- .../errorprone/bugpatterns/UnusedMethod.java | 16 +++++++-- .../bugpatterns/UnusedMethodTest.java | 36 +++++++++++++++++-- 2 files changed, 46 insertions(+), 6 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 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(