mirror of
https://github.com/jlengrand/error-prone-support.git
synced 2026-03-10 08:11:25 +00:00
Introduce MonoIdentity and MonoThen Refaster rules (#405)
The `MonoIdentity` rule is a generalization of the existing `MonoSwitchIfEmptyOfEmptyPublisher` rule.
This commit is contained in:
committed by
GitHub
parent
0153c1495f
commit
2cbd48ec47
@@ -365,13 +365,21 @@ final class ReactorRules {
|
||||
}
|
||||
}
|
||||
|
||||
/** Don't unnecessarily pass an empty publisher to {@link Mono#switchIfEmpty(Mono)}. */
|
||||
static final class MonoSwitchIfEmptyOfEmptyPublisher<T> {
|
||||
/** Don't unnecessarily transform a {@link Mono} to an equivalent instance. */
|
||||
static final class MonoIdentity<T> {
|
||||
@BeforeTemplate
|
||||
Mono<T> before(Mono<T> mono) {
|
||||
return mono.switchIfEmpty(Mono.empty());
|
||||
}
|
||||
|
||||
// XXX: Review the suppression once NullAway has better support for generics. Keep an eye on
|
||||
// https://github.com/uber/NullAway/issues?q=is%3Aopen+generics.
|
||||
@BeforeTemplate
|
||||
@SuppressWarnings("NullAway" /* False positive. */)
|
||||
Mono<@Nullable Void> before2(Mono<@Nullable Void> mono) {
|
||||
return mono.then();
|
||||
}
|
||||
|
||||
@AfterTemplate
|
||||
Mono<T> after(Mono<T> mono) {
|
||||
return mono;
|
||||
@@ -675,6 +683,19 @@ final class ReactorRules {
|
||||
}
|
||||
}
|
||||
|
||||
/** Prefer direct invocation of {@link Mono#then()}} over more contrived alternatives. */
|
||||
static final class MonoThen<T> {
|
||||
@BeforeTemplate
|
||||
Mono<@Nullable Void> before(Mono<T> mono) {
|
||||
return mono.flux().then();
|
||||
}
|
||||
|
||||
@AfterTemplate
|
||||
Mono<@Nullable Void> after(Mono<T> mono) {
|
||||
return mono.then();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prefer a collection using {@link MoreCollectors#toOptional()} over more contrived alternatives.
|
||||
*/
|
||||
|
||||
@@ -115,8 +115,8 @@ final class ReactorRulesTest implements RefasterRuleCollectionTestCase {
|
||||
Flux.just("baz").switchIfEmpty(Flux.just("qux")));
|
||||
}
|
||||
|
||||
Mono<Integer> testMonoSwitchIfEmptyOfEmptyPublisher() {
|
||||
return Mono.just(1).switchIfEmpty(Mono.empty());
|
||||
ImmutableSet<Mono<?>> testMonoIdentity() {
|
||||
return ImmutableSet.of(Mono.just(1).switchIfEmpty(Mono.empty()), Mono.<Void>empty().then());
|
||||
}
|
||||
|
||||
ImmutableSet<Flux<Integer>> testFluxSwitchIfEmptyOfEmptyPublisher() {
|
||||
@@ -221,6 +221,10 @@ final class ReactorRulesTest implements RefasterRuleCollectionTestCase {
|
||||
Flux.concat(Mono.just("baz")));
|
||||
}
|
||||
|
||||
Mono<Void> testMonoThen() {
|
||||
return Mono.just("foo").flux().then();
|
||||
}
|
||||
|
||||
Mono<Optional<String>> testMonoCollectToOptional() {
|
||||
return Mono.just("foo").map(Optional::of).defaultIfEmpty(Optional.empty());
|
||||
}
|
||||
|
||||
@@ -120,8 +120,8 @@ final class ReactorRulesTest implements RefasterRuleCollectionTestCase {
|
||||
Flux.just("foo").defaultIfEmpty("bar"), Flux.just("baz").defaultIfEmpty("qux"));
|
||||
}
|
||||
|
||||
Mono<Integer> testMonoSwitchIfEmptyOfEmptyPublisher() {
|
||||
return Mono.just(1);
|
||||
ImmutableSet<Mono<?>> testMonoIdentity() {
|
||||
return ImmutableSet.of(Mono.just(1), Mono.<Void>empty());
|
||||
}
|
||||
|
||||
ImmutableSet<Flux<Integer>> testFluxSwitchIfEmptyOfEmptyPublisher() {
|
||||
@@ -220,6 +220,10 @@ final class ReactorRulesTest implements RefasterRuleCollectionTestCase {
|
||||
Mono.just("foo").flux(), Mono.just("bar").flux(), Mono.just("baz").flux());
|
||||
}
|
||||
|
||||
Mono<Void> testMonoThen() {
|
||||
return Mono.just("foo").then();
|
||||
}
|
||||
|
||||
Mono<Optional<String>> testMonoCollectToOptional() {
|
||||
return Mono.just("foo").flux().collect(toOptional());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user