Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce MonoIdentity and MonoThen Refaster rules #405

Merged
merged 2 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -365,13 +365,21 @@ Flux<T> after(Flux<T> flux, T object) {
}
}

/** 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;
Expand Down Expand Up @@ -675,6 +683,19 @@ Flux<T> after(Mono<T> mono) {
}
}

/** 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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ ImmutableSet<Flux<String>> testFluxDefaultIfEmpty() {
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() {
Expand Down Expand Up @@ -221,6 +221,10 @@ ImmutableSet<Flux<String>> testMonoFlux() {
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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ ImmutableSet<Flux<String>> testFluxDefaultIfEmpty() {
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() {
Expand Down Expand Up @@ -220,6 +220,10 @@ ImmutableSet<Flux<String>> testMonoFlux() {
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());
}
Expand Down