diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/NonEmptyMono.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/NonEmptyMono.java index a6ae88cd542..37f530be56a 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/NonEmptyMono.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/NonEmptyMono.java @@ -17,6 +17,7 @@ import com.google.errorprone.util.ASTHelpers; import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.MethodInvocationTree; +import java.util.function.BiFunction; import reactor.core.publisher.Mono; import tech.picnic.errorprone.bugpatterns.util.SourceCode; @@ -62,6 +63,10 @@ public final class NonEmptyMono extends BugChecker implements MethodInvocationTr "last", "reduceWith", "single"), + instanceMethod() + .onDescendantOf("reactor.core.publisher.Flux") + .named("reduce") + .withParameters(Object.class.getName(), BiFunction.class.getName()), instanceMethod() .onDescendantOf("reactor.core.publisher.Mono") .namedAnyOf("defaultIfEmpty", "hasElement", "single")); diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/NonEmptyMonoTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/NonEmptyMonoTest.java index 820ed28a2ff..09b95549dee 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/NonEmptyMonoTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/NonEmptyMonoTest.java @@ -28,37 +28,48 @@ void identification() { "class A {", " void m() {", " // BUG: Diagnostic contains:", - " Flux.just().all(x -> true).defaultIfEmpty(true);", + " Flux.just(1).all(x -> true).defaultIfEmpty(true);", " // BUG: Diagnostic contains:", - " Flux.just().any(x -> true).single();", + " Flux.just(1).any(x -> true).single();", "", " // BUG: Diagnostic contains:", - " Flux.just().collect(toImmutableList()).switchIfEmpty(Mono.just(ImmutableList.of()));", + " Flux.just(1).collect(toImmutableList()).switchIfEmpty(Mono.just(ImmutableList.of()));", " // BUG: Diagnostic contains:", - " Flux.just().collectList().defaultIfEmpty(ImmutableList.of());", + " Flux.just(1).collectList().defaultIfEmpty(ImmutableList.of());", " // BUG: Diagnostic contains:", - " Flux.just().collectMap(identity()).single();", - " Flux.just()", + " Flux.just(1).collectMap(identity()).single();", + " Flux.just(1)", " .collectMultimap(identity(), identity(), ImmutableMap::of)", " // BUG: Diagnostic contains:", " .switchIfEmpty(Mono.just(ImmutableMap.of()));", " // BUG: Diagnostic contains:", - " Flux.just().collectSortedList((o1, o2) -> 1).defaultIfEmpty(ImmutableList.of());", + " Flux.just(1).collectSortedList((o1, o2) -> 1).defaultIfEmpty(ImmutableList.of());", "", " // BUG: Diagnostic contains:", - " Flux.just().count().single();", + " Flux.just(1).count().single();", + " // BUG: Diagnostic contains:", + " Flux.just(1).elementAt(0).defaultIfEmpty(1);", + " // BUG: Diagnostic contains:", + " Flux.just(1).hasElement(0).single();", + " // BUG: Diagnostic contains:", + " Flux.just(1).hasElements().switchIfEmpty(Mono.just(true));", " // BUG: Diagnostic contains:", - " Flux.just().elementAt(0).defaultIfEmpty(1);", + " Flux.just(1).last().defaultIfEmpty(1);", " // BUG: Diagnostic contains:", - " Flux.just().hasElement(0).single();", + " Flux.just(1).reduceWith(() -> 1, (x, y) -> x).single();", " // BUG: Diagnostic contains:", - " Flux.just().hasElements().switchIfEmpty(Mono.just(true));", + " Flux.just(1).single().switchIfEmpty(Mono.just(1));", + "", + " Flux.just(1).reduce(Integer::sum).single();", + " // BUG: Diagnostic contains:", + " Flux.just(1).reduce(2, Integer::sum).single();", + "", " // BUG: Diagnostic contains:", - " Flux.just().last().defaultIfEmpty(1);", + " Mono.just(1).defaultIfEmpty(1).defaultIfEmpty(2);", " // BUG: Diagnostic contains:", - " Flux.just().reduceWith(() -> 1, (x, y) -> x).single();", + " Mono.just(1).hasElement().single();", " // BUG: Diagnostic contains:", - " Flux.just().single().switchIfEmpty(Mono.just(1));", + " Mono.just(1).single().switchIfEmpty(Mono.just(2));", " }", "}") .doTest();