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 2d9cd67bf9..34fcc11e03 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 @@ -639,6 +639,58 @@ Flux after(Flux flux) { } } + /** Prefer {@link Mono#flatMap(Function)} over more contrived alternatives. */ + static final class MonoFlatMapIdentity { + @BeforeTemplate + Mono before(Mono mono, Function> function) { + return mono.map(function).flatMap(identity()); + } + + @AfterTemplate + Mono after(Mono mono, Function> function) { + return mono.flatMap(function); + } + } + + /** Prefer {@link Mono#flatMapMany(Function)} over more contrived alternatives. */ + static final class FlatMapManyIdentity { + @BeforeTemplate + Flux before(Mono mono, Function> function) { + return mono.map(function).flatMapMany(identity()); + } + + @AfterTemplate + Flux after(Mono mono, Function> function) { + return mono.flatMapMany(function); + } + } + + /** Prefer {@link Flux#concatMap(Function)} over more contrived alternatives. */ + static final class ConcatMapIdentity { + @BeforeTemplate + Flux before(Flux flux, Function> function) { + return flux.map(function).concatMap(identity()); + } + + @AfterTemplate + Flux after(Flux flux, Function> function) { + return flux.concatMap(function); + } + } + + /** Prefer {@link Flux#concatMap(Function, int)} over more contrived alternatives. */ + static final class ConcatMapIdentityWithPrefetch { + @BeforeTemplate + Flux before(Flux flux, Function> function, int prefetch) { + return flux.map(function).concatMap(identity(), prefetch); + } + + @AfterTemplate + Flux after(Flux flux, Function> function, int prefetch) { + return flux.concatMap(function, prefetch); + } + } + /** * Prefer {@link Flux#concatMapIterable(Function)} over alternatives that require an additional * subscription. 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 61b258003b..f7c5543000 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 @@ -1,5 +1,6 @@ package tech.picnic.errorprone.refasterrules; +import static java.util.function.Function.identity; import static org.assertj.core.api.Assertions.assertThat; import com.google.common.collect.ImmutableList; @@ -207,6 +208,28 @@ Flux testFluxCast() { return Flux.just(1).map(Number.class::cast); } + Mono testMonoFlatMapIdentity() { + return Mono.just("foo").map(Mono::just).flatMap(identity()); + } + + ImmutableSet> testFlatMapManyIdentity() { + return ImmutableSet.of( + Mono.just("foo").map(Mono::just).flatMapMany(identity()), + Mono.just("foo").map(Flux::just).flatMapMany(identity())); + } + + ImmutableSet> testConcatMapIdentity() { + return ImmutableSet.of( + Flux.just("foo", "bar").map(Mono::just).concatMap(identity()), + Flux.just("foo", "bar").map(Flux::just).concatMap(identity())); + } + + ImmutableSet> testConcatMapIdentityWithPrefetch() { + return ImmutableSet.of( + Flux.just("foo", "bar").map(Mono::just).concatMap(identity(), 1), + Flux.just("foo", "bar").map(Flux::just).concatMap(identity(), 1)); + } + ImmutableSet> testConcatMapIterableIdentity() { return ImmutableSet.of( Flux.just(ImmutableList.of("foo")).concatMap(list -> Flux.fromIterable(list)), 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 55092ba2d5..ddf1bc5fb6 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 @@ -206,6 +206,27 @@ Flux testFluxCast() { return Flux.just(1).cast(Number.class); } + Mono testMonoFlatMapIdentity() { + return Mono.just("foo").flatMap(Mono::just); + } + + ImmutableSet> testFlatMapManyIdentity() { + return ImmutableSet.of( + Mono.just("foo").flatMapMany(Mono::just), Mono.just("foo").flatMapMany(Flux::just)); + } + + ImmutableSet> testConcatMapIdentity() { + return ImmutableSet.of( + Flux.just("foo", "bar").concatMap(Mono::just), + Flux.just("foo", "bar").concatMap(Flux::just)); + } + + ImmutableSet> testConcatMapIdentityWithPrefetch() { + return ImmutableSet.of( + Flux.just("foo", "bar").concatMap(Mono::just, 1), + Flux.just("foo", "bar").concatMap(Flux::just, 1)); + } + ImmutableSet> testConcatMapIterableIdentity() { return ImmutableSet.of( Flux.just(ImmutableList.of("foo")).concatMapIterable(identity()),