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

Add Refaster rule for Flux#concat for a single Mono or Flux #20

Merged
merged 3 commits into from
Nov 13, 2021
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 @@ -161,6 +161,32 @@ Flux<S> after(Mono<T> mono) {
}
}

/** Prefer {@link Mono#flux()}} over more contrived alternatives. */
static final class MonoFlux<T> {
@BeforeTemplate
Flux<T> before(Mono<T> mono) {
return Flux.concat(mono);
}

@AfterTemplate
Flux<T> after(Mono<T> mono) {
return mono.flux();
}
}

/** Don't unnecessarily invoke {@link Flux#concat(Publisher)}. */
static final class FluxIdentity<T> {
@BeforeTemplate
Flux<T> before(Flux<T> flux) {
return Flux.concat(flux);
}

@AfterTemplate
Flux<T> after(Flux<T> flux) {
return flux;
}
}
Comment on lines +177 to +188
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rickie This template could be folded into the "vacuous conversion" bug checker we discussed a while back.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Will add it to the list @Stephan202


/**
* 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 @@ -49,6 +49,14 @@ Flux<String> testMonoFlatMapToFlux() {
return Mono.just("foo").flatMapMany(s -> Mono.just(s + s));
}

Flux<String> testMonoFlux() {
return Flux.concat(Mono.just("foo"));
}

Flux<String> testFluxIdentity() {
return Flux.concat(Flux.just("foo"));
}

ImmutableSet<Mono<Optional<String>>> testMonoCollectToOptional() {
return ImmutableSet.of(
Mono.just("foo").map(Optional::of).defaultIfEmpty(Optional.empty()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ Flux<String> testMonoFlatMapToFlux() {
return Mono.just("foo").flatMap(s -> Mono.just(s + s)).flux();
}

Flux<String> testMonoFlux() {
return Mono.just("foo").flux();
}

Flux<String> testFluxIdentity() {
return Flux.just("foo");
}

ImmutableSet<Mono<Optional<String>>> testMonoCollectToOptional() {
return ImmutableSet.of(
Mono.just("foo").flux().collect(toOptional()),
Expand Down