From a9e5c4c99cd4a2aeda0b5d9d65f5fd7dd059c1ac Mon Sep 17 00:00:00 2001 From: Phil Werli Date: Tue, 9 Nov 2021 18:41:12 +0100 Subject: [PATCH 1/3] Add Refaster rule for `Flux#concat` for a single Mono or Flux --- .../refastertemplates/ReactorTemplates.java | 26 +++++++++++++++++++ .../ReactorTemplatesTestInput.java | 8 ++++++ .../ReactorTemplatesTestOutput.java | 8 ++++++ 3 files changed, 42 insertions(+) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java index f4ad0d81b6..079adaec3e 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java @@ -161,6 +161,32 @@ Flux after(Mono mono) { } } + /** Don't use {@link Flux#concat(Publisher)} with only a single {@link Mono}. */ + abstract static class FluxConcatOfOneMono { + @BeforeTemplate + Flux before(Mono mono) { + return Flux.concat(mono); + } + + @AfterTemplate + Flux after(Mono mono) { + return mono.flux(); + } + } + + /** Don't use {@link Flux#concat(Publisher)} with only a single {@link Flux}. */ + abstract static class FluxConcatOfOneFlux { + @BeforeTemplate + Flux before(Flux flux) { + return Flux.concat(flux); + } + + @AfterTemplate + Flux after(Flux flux) { + return flux; + } + } + /** * Prefer a collection using {@link MoreCollectors#toOptional()} over more contrived alternatives. */ diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ReactorTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ReactorTemplatesTestInput.java index 818b6d6372..dccf3a1956 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ReactorTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ReactorTemplatesTestInput.java @@ -49,6 +49,14 @@ Flux testMonoFlatMapToFlux() { return Mono.just("foo").flatMapMany(s -> Mono.just(s + s)); } + Flux testFluxConcatOfOneMono() { + return Flux.concat(Mono.just("foo")); + } + + Flux testFluxConcatOfOneFlux() { + return Flux.concat(Flux.just("foo", "bar")); + } + ImmutableSet>> testMonoCollectToOptional() { return ImmutableSet.of( Mono.just("foo").map(Optional::of).defaultIfEmpty(Optional.empty()), diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ReactorTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ReactorTemplatesTestOutput.java index b144223c61..f53a218796 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ReactorTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ReactorTemplatesTestOutput.java @@ -50,6 +50,14 @@ Flux testMonoFlatMapToFlux() { return Mono.just("foo").flatMap(s -> Mono.just(s + s)).flux(); } + Flux testFluxConcatOfOneMono() { + return Mono.just("foo").flux(); + } + + Flux testFluxConcatOfOneFlux() { + return Flux.just("foo", "bar"); + } + ImmutableSet>> testMonoCollectToOptional() { return ImmutableSet.of( Mono.just("foo").flux().collect(toOptional()), From eb681a999f1240fcf38a6e5cbeb3f78b16926014 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Wed, 10 Nov 2021 09:09:17 +0100 Subject: [PATCH 2/3] Suggestions --- .../picnic/errorprone/refastertemplates/ReactorTemplates.java | 4 ++-- .../errorprone/bugpatterns/ReactorTemplatesTestInput.java | 4 ++-- .../errorprone/bugpatterns/ReactorTemplatesTestOutput.java | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java index 079adaec3e..1c4d6cc2dc 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java @@ -162,7 +162,7 @@ Flux after(Mono mono) { } /** Don't use {@link Flux#concat(Publisher)} with only a single {@link Mono}. */ - abstract static class FluxConcatOfOneMono { + static final class FluxConcatMono { @BeforeTemplate Flux before(Mono mono) { return Flux.concat(mono); @@ -175,7 +175,7 @@ Flux after(Mono mono) { } /** Don't use {@link Flux#concat(Publisher)} with only a single {@link Flux}. */ - abstract static class FluxConcatOfOneFlux { + static final class FluxConcatFlux { @BeforeTemplate Flux before(Flux flux) { return Flux.concat(flux); diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ReactorTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ReactorTemplatesTestInput.java index dccf3a1956..9458cbcf77 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ReactorTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ReactorTemplatesTestInput.java @@ -49,11 +49,11 @@ Flux testMonoFlatMapToFlux() { return Mono.just("foo").flatMapMany(s -> Mono.just(s + s)); } - Flux testFluxConcatOfOneMono() { + Flux testFluxConcatMono() { return Flux.concat(Mono.just("foo")); } - Flux testFluxConcatOfOneFlux() { + Flux testFluxConcatFlux() { return Flux.concat(Flux.just("foo", "bar")); } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ReactorTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ReactorTemplatesTestOutput.java index f53a218796..a0ea763c9b 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ReactorTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ReactorTemplatesTestOutput.java @@ -50,11 +50,11 @@ Flux testMonoFlatMapToFlux() { return Mono.just("foo").flatMap(s -> Mono.just(s + s)).flux(); } - Flux testFluxConcatOfOneMono() { + Flux testFluxConcatMono() { return Mono.just("foo").flux(); } - Flux testFluxConcatOfOneFlux() { + Flux testFluxConcatFlux() { return Flux.just("foo", "bar"); } From 5f2efeff8ff620861efeac63de8fa9c5dd1772c9 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sat, 13 Nov 2021 16:38:43 +0100 Subject: [PATCH 3/3] Suggestions --- .../errorprone/refastertemplates/ReactorTemplates.java | 8 ++++---- .../errorprone/bugpatterns/ReactorTemplatesTestInput.java | 6 +++--- .../bugpatterns/ReactorTemplatesTestOutput.java | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java index 1c4d6cc2dc..b70f1c75d6 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ReactorTemplates.java @@ -161,8 +161,8 @@ Flux after(Mono mono) { } } - /** Don't use {@link Flux#concat(Publisher)} with only a single {@link Mono}. */ - static final class FluxConcatMono { + /** Prefer {@link Mono#flux()}} over more contrived alternatives. */ + static final class MonoFlux { @BeforeTemplate Flux before(Mono mono) { return Flux.concat(mono); @@ -174,8 +174,8 @@ Flux after(Mono mono) { } } - /** Don't use {@link Flux#concat(Publisher)} with only a single {@link Flux}. */ - static final class FluxConcatFlux { + /** Don't unnecessarily invoke {@link Flux#concat(Publisher)}. */ + static final class FluxIdentity { @BeforeTemplate Flux before(Flux flux) { return Flux.concat(flux); diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ReactorTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ReactorTemplatesTestInput.java index 9458cbcf77..16a3351dd9 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ReactorTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ReactorTemplatesTestInput.java @@ -49,12 +49,12 @@ Flux testMonoFlatMapToFlux() { return Mono.just("foo").flatMapMany(s -> Mono.just(s + s)); } - Flux testFluxConcatMono() { + Flux testMonoFlux() { return Flux.concat(Mono.just("foo")); } - Flux testFluxConcatFlux() { - return Flux.concat(Flux.just("foo", "bar")); + Flux testFluxIdentity() { + return Flux.concat(Flux.just("foo")); } ImmutableSet>> testMonoCollectToOptional() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ReactorTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ReactorTemplatesTestOutput.java index a0ea763c9b..38911f6fa1 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ReactorTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ReactorTemplatesTestOutput.java @@ -50,12 +50,12 @@ Flux testMonoFlatMapToFlux() { return Mono.just("foo").flatMap(s -> Mono.just(s + s)).flux(); } - Flux testFluxConcatMono() { + Flux testMonoFlux() { return Mono.just("foo").flux(); } - Flux testFluxConcatFlux() { - return Flux.just("foo", "bar"); + Flux testFluxIdentity() { + return Flux.just("foo"); } ImmutableSet>> testMonoCollectToOptional() {