diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java index 1f97c40fd3..fca783f9d8 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java @@ -41,6 +41,7 @@ import java.util.function.Predicate; import java.util.function.Supplier; import java.util.stream.Collector; +import java.util.stream.Stream; import org.jspecify.annotations.Nullable; import org.reactivestreams.Publisher; import reactor.core.publisher.Flux; @@ -1913,7 +1914,7 @@ Duration after(StepVerifier.LastStep step, Duration duration) { /** * Prefer {@link Mono#fromFuture(Supplier)} over {@link Mono#fromFuture(CompletableFuture)}, as - * the former may defer initiation of the asynchornous computation until subscription. + * the former may defer initiation of the asynchronous computation until subscription. */ static final class MonoFromFutureSupplier { // XXX: Constrain the `future` parameter using `@NotMatches(IsIdentityOperation.class)` once @@ -1947,4 +1948,20 @@ Mono after(CompletableFuture future, boolean suppressCancel) { return Mono.fromFuture(() -> future, suppressCancel); } } + + /** + * Prefer {@link Flux#fromStream(Supplier)} over {@link Flux#fromStream(Stream)}, as the former + * may defer initiation of the asynchronous computation until subscription. + */ + static final class FluxFromStreamSupplier { + @BeforeTemplate + Flux before(Stream stream) { + return Flux.fromStream(stream); + } + + @AfterTemplate + Flux after(Stream stream) { + return Flux.fromStream(() -> stream); + } + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java index 3702be49e9..54c0f00775 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java @@ -23,6 +23,7 @@ import java.util.concurrent.Callable; import java.util.concurrent.CompletableFuture; import java.util.function.Supplier; +import java.util.stream.Stream; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.math.MathFlux; @@ -660,4 +661,8 @@ Mono testMonoFromFutureSupplier() { Mono testMonoFromFutureSupplierBoolean() { return Mono.fromFuture(CompletableFuture.completedFuture(null), true); } + + Flux testFluxFromStreamSupplier() { + return Flux.fromStream(Stream.of(1)); + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java index 7cb8a551b2..df9762b4f4 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java @@ -25,6 +25,8 @@ import java.util.concurrent.Callable; import java.util.concurrent.CompletableFuture; import java.util.function.Supplier; +import java.util.random.RandomGenerator; +import java.util.stream.Stream; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.function.TupleUtils; @@ -641,4 +643,8 @@ Mono testMonoFromFutureSupplier() { Mono testMonoFromFutureSupplierBoolean() { return Mono.fromFuture(() -> CompletableFuture.completedFuture(null), true); } + + Flux testFluxFromStreamSupplier() { + return Flux.fromStream(() -> RandomGenerator.StreamableGenerator.of(1)); + } }