WIP: Matching fails, not sure why

This commit is contained in:
Stephan Schroevers
2023-12-10 11:08:02 +01:00
parent da3ec2ce90
commit cffff17c28
3 changed files with 73 additions and 0 deletions

View File

@@ -728,6 +728,27 @@ final class ReactorRules {
}
}
/**
* Prefer {@link Mono#map(Function)} over alternatives that unnecessarily create and collect a
* {@link Flux}.
*/
// XXX: This rule assumes that any matched `Collector` does not filter or reorder elements after
// application of the matched `Function`.
// XXX: The `function` parameter is not matched, unless `I` is changed to `Iterable<? extends S>`,
// which would make the rule incorrect.
static final class MonoMapToIterable<T, S, I extends Iterable<? extends S>> {
@BeforeTemplate
Mono<I> before(
Mono<T> mono, Function<? super T, ? extends I> function, Collector<S, ?, I> collector) {
return mono.flatMapIterable(function).collect(collector);
}
@AfterTemplate
Mono<I> after(Mono<T> mono, Function<? super T, ? extends I> function) {
return mono.map(function);
}
}
/**
* Prefer {@link Flux#map(Function)} over alternatives that unnecessarily require an inner
* subscription.
@@ -1694,6 +1715,26 @@ final class ReactorRules {
}
}
/**
* Prefer {@link Flux#singleOrEmpty()} over {@link Flux#next()} when the {@link Flux} emits at
* most one element.
*/
// XXX: This is a special case of a more general rule. Consider introducing an Error Prone check
// for this.
// XXX: The `transformer` parameter isn't matched, unless the signature is changed to `? extends
// Publisher<S>`, which would make the rule incorrect.
static final class FluxTransformToMonoSingleOrEmpty<T, S> {
@BeforeTemplate
Mono<S> before(Flux<T> flux, Function<? super Flux<T>, ? extends Mono<S>> transformer) {
return flux.transform(transformer).next();
}
@AfterTemplate
Mono<S> after(Flux<T> flux, Function<? super Flux<T>, ? extends Mono<S>> transformer) {
return flux.transform(transformer).singleOrEmpty();
}
}
/** Prefer {@link reactor.util.context.Context#empty()}} over more verbose alternatives. */
// XXX: Introduce Refaster rules or a `BugChecker` that maps `(Immutable)Map.of(k, v)` to
// `Context.of(k, v)` and likewise for multi-pair overloads.

View File

@@ -1,6 +1,8 @@
package tech.picnic.errorprone.refasterrules;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet;
import static com.google.common.collect.MoreCollectors.toOptional;
import static java.util.Comparator.naturalOrder;
import static java.util.Comparator.reverseOrder;
@@ -14,8 +16,10 @@ import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
@@ -262,6 +266,16 @@ final class ReactorRulesTest implements RefasterRuleCollectionTestCase {
Mono.just("bar").flatMap(s -> Mono.just(s.substring(1))));
}
ImmutableSet<Mono<Iterable<String>>> testMonoMapToIterable() {
return ImmutableSet.of(
Mono.just("foo").flatMapIterable(ImmutableSet::of).collect(toImmutableSet()),
Mono.just("bar").flatMapIterable(ImmutableSortedSet::of).collect(toImmutableSet()),
Mono.just("baz")
.flatMapIterable(ImmutableSet::of)
.collect(toImmutableSortedSet(naturalOrder())),
Mono.just("qux").flatMapIterable(Arrays::asList).collect(toCollection(ArrayList::new)));
}
ImmutableSet<Flux<Integer>> testFluxMap() {
return ImmutableSet.of(
Flux.just(1).concatMap(n -> Mono.just(n)),
@@ -571,6 +585,10 @@ final class ReactorRulesTest implements RefasterRuleCollectionTestCase {
MathFlux.min(Flux.just(1), reverseOrder()), MathFlux.max(Flux.just(2), naturalOrder()));
}
Mono<String> testFluxTransformToMonoSingleOrEmpty() {
return Flux.just("foo").transform(Flux::next).next();
}
ImmutableSet<Context> testContextEmpty() {
return ImmutableSet.of(Context.of(ImmutableMap.of()), Context.of(ImmutableMap.of(1, 2)));
}

View File

@@ -16,8 +16,10 @@ import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
@@ -263,6 +265,14 @@ final class ReactorRulesTest implements RefasterRuleCollectionTestCase {
return ImmutableSet.of(Mono.just("foo").map(s -> s), Mono.just("bar").map(s -> s.substring(1)));
}
ImmutableSet<Mono<Iterable<String>>> testMonoMapToIterable() {
return ImmutableSet.of(
Mono.just("foo").map(ImmutableSet::of),
Mono.just("bar").map(ImmutableSortedSet::of),
Mono.just("baz").map(ImmutableSet::of),
Mono.just("qux").map(Arrays::asList));
}
ImmutableSet<Flux<Integer>> testFluxMap() {
return ImmutableSet.of(
Flux.just(1).map(n -> n),
@@ -560,6 +570,10 @@ final class ReactorRulesTest implements RefasterRuleCollectionTestCase {
return ImmutableSet.of(MathFlux.max(Flux.just(1)), MathFlux.max(Flux.just(2)));
}
Mono<String> testFluxTransformToMonoSingleOrEmpty() {
return Flux.just("foo").transform(Flux::next).singleOrEmpty();
}
ImmutableSet<Context> testContextEmpty() {
return ImmutableSet.of(Context.empty(), Context.of(ImmutableMap.of(1, 2)));
}