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 1428730a13..467c526f63 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 @@ -1042,12 +1042,15 @@ Mono after(Flux flux, Mono mono) { /** Prefer {@link Mono#singleOptional()} over more contrived alternatives. */ // XXX: Consider creating a plugin that flags/discourages `Mono>` method return // types, just as we discourage nullable `Boolean`s and `Optional`s. + // XXX: The `mono.transform(Mono::singleOptional)` replacement is a special case of a more general + // rule. Consider introducing an Error Prone check for this. static final class MonoSingleOptional { @BeforeTemplate Mono> before(Mono mono) { return Refaster.anyOf( mono.flux().collect(toOptional()), - mono.map(Optional::of).defaultIfEmpty(Optional.empty())); + mono.map(Optional::of).defaultIfEmpty(Optional.empty()), + mono.transform(Mono::singleOptional)); } @AfterTemplate 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 5a7dd66199..25e0f5144e 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 @@ -374,7 +374,8 @@ ImmutableSet> testFluxThenMono() { ImmutableSet>> testMonoSingleOptional() { return ImmutableSet.of( Mono.just("foo").flux().collect(toOptional()), - Mono.just("bar").map(Optional::of).defaultIfEmpty(Optional.empty())); + Mono.just("bar").map(Optional::of).defaultIfEmpty(Optional.empty()), + Mono.just("baz").transform(Mono::singleOptional)); } Mono testMonoCast() { 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 dbc020a119..dd25437fb6 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 @@ -367,7 +367,10 @@ ImmutableSet> testFluxThenMono() { } ImmutableSet>> testMonoSingleOptional() { - return ImmutableSet.of(Mono.just("foo").singleOptional(), Mono.just("bar").singleOptional()); + return ImmutableSet.of( + Mono.just("foo").singleOptional(), + Mono.just("bar").singleOptional(), + Mono.just("baz").singleOptional()); } Mono testMonoCast() {