Skip to content

Commit e74fbe1

Browse files
committed
fix: support implicit @MethodSource for UnusedMethod
1 parent db62c8c commit e74fbe1

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

core/src/main/java/com/google/errorprone/bugpatterns/UnusedMethod.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,14 @@ private void handleMethodSource(MethodTree tree) {
303303
sym.getRawAttributes().stream()
304304
.filter(a -> a.type.tsym.getQualifiedName().equals(name))
305305
.findAny()
306-
// get the annotation value array as a set of Names
307-
.flatMap(a -> getAnnotationValue(a, "value"))
308-
.map(
309-
y -> asStrings(y).map(state::getName).map(Name::toString).collect(toImmutableSet()))
306+
// get the annotation value array as a set of Names,
307+
// normalizing unset value to the empty value.
308+
.map(a -> getAnnotationValue(a, "value")
309+
.map(y -> asStrings(y).map(state::getName).map(Name::toString).collect(toImmutableSet()))
310+
.orElse(ImmutableSet.of())
311+
)
312+
// If no explicit method sources were specified, use method name instead.
313+
.map(names -> names.isEmpty() ? Set.of(sym.name.toString()) : names)
310314
// remove all potentially unused methods referenced by the @MethodSource
311315
.ifPresent(
312316
referencedNames ->

core/src/test/java/com/google/errorprone/bugpatterns/UnusedMethodTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,36 @@ private static Stream<String> parameters() {
339339
.doTest();
340340
}
341341

342+
@Test
343+
public void implicitMethodSource() {
344+
helper
345+
.addSourceLines(
346+
"MethodSource.java",
347+
"""
348+
package org.junit.jupiter.params.provider;
349+
350+
public @interface MethodSource {
351+
String[] value();
352+
}
353+
""")
354+
.addSourceLines(
355+
"Test.java",
356+
"""
357+
import java.util.stream.Stream;
358+
import org.junit.jupiter.params.provider.MethodSource;
359+
360+
class Test {
361+
@MethodSource
362+
void test() {}
363+
364+
private static Stream<String> test() {
365+
return Stream.of();
366+
}
367+
}
368+
""")
369+
.doTest();
370+
}
371+
342372
@Test
343373
public void qualifiedMethodSource() {
344374
helper

0 commit comments

Comments
 (0)