From 006665ee6d56a3a58bce0b5eb7c6b351d956315d Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Wed, 5 Jul 2023 13:54:49 +0200 Subject: [PATCH] Introduce `MonoSingle` Refaster rule (#703) --- .../errorprone/refasterrules/ReactorRules.java | 13 +++++++++++++ .../refasterrules/ReactorRulesTestInput.java | 4 ++++ .../refasterrules/ReactorRulesTestOutput.java | 4 ++++ 3 files changed, 21 insertions(+) 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 f7c9d15eaa..c30bbd11df 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 @@ -418,6 +418,19 @@ Mono after(Mono mono) { } } + /** Don't unnecessarily transform a {@link Mono} to a {@link Flux} to expect exactly one item. */ + static final class MonoSingle { + @BeforeTemplate + Mono before(Mono mono) { + return mono.flux().single(); + } + + @AfterTemplate + Mono after(Mono mono) { + return mono.single(); + } + } + /** Don't unnecessarily pass an empty publisher to {@link Flux#switchIfEmpty(Publisher)}. */ static final class FluxSwitchIfEmptyOfEmptyPublisher { @BeforeTemplate 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 3a15f612b9..18ce1d1dab 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 @@ -145,6 +145,10 @@ ImmutableSet> testMonoIdentity() { Mono.>empty().map(ImmutableList::copyOf)); } + Mono testMonoSingle() { + return Mono.just(1).flux().single(); + } + ImmutableSet> testFluxSwitchIfEmptyOfEmptyPublisher() { return ImmutableSet.of( Flux.just(1).switchIfEmpty(Mono.empty()), Flux.just(2).switchIfEmpty(Flux.empty())); 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 67812db80f..c44d57e0ff 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 @@ -150,6 +150,10 @@ ImmutableSet> testMonoIdentity() { Mono.>empty()); } + Mono testMonoSingle() { + return Mono.just(1).single(); + } + ImmutableSet> testFluxSwitchIfEmptyOfEmptyPublisher() { return ImmutableSet.of(Flux.just(1), Flux.just(2)); }