Introduce StreamOf{1,2,3,4,5} Refaster rules (#814)

This commit is contained in:
Vincent Koeman
2023-10-11 17:52:52 +02:00
committed by GitHub
parent 05809b1c85
commit 4e2ceeb252
3 changed files with 115 additions and 0 deletions

View File

@@ -20,6 +20,7 @@ import static java.util.stream.Collectors.summingDouble;
import static java.util.stream.Collectors.summingInt;
import static java.util.stream.Collectors.summingLong;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Streams;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.refaster.Refaster;
@@ -668,4 +669,74 @@ final class StreamRules {
return Stream.iterate(seed, hasNext, next);
}
}
/** Prefer {@link Stream#of(Object)} over more contrived alternatives. */
// XXX: Generalize this and similar rules using an Error Prone check.
static final class StreamOf1<T> {
@BeforeTemplate
Stream<T> before(T e1) {
return ImmutableList.of(e1).stream();
}
@AfterTemplate
Stream<T> after(T e1) {
return Stream.of(e1);
}
}
/** Prefer {@link Stream#of(Object[])} over more contrived alternatives. */
// XXX: Generalize this and similar rules using an Error Prone check.
static final class StreamOf2<T> {
@BeforeTemplate
Stream<T> before(T e1, T e2) {
return ImmutableList.of(e1, e2).stream();
}
@AfterTemplate
Stream<T> after(T e1, T e2) {
return Stream.of(e1, e2);
}
}
/** Prefer {@link Stream#of(Object[])} over more contrived alternatives. */
// XXX: Generalize this and similar rules using an Error Prone check.
static final class StreamOf3<T> {
@BeforeTemplate
Stream<T> before(T e1, T e2, T e3) {
return ImmutableList.of(e1, e2, e3).stream();
}
@AfterTemplate
Stream<T> after(T e1, T e2, T e3) {
return Stream.of(e1, e2, e3);
}
}
/** Prefer {@link Stream#of(Object[])} over more contrived alternatives. */
// XXX: Generalize this and similar rules using an Error Prone check.
static final class StreamOf4<T> {
@BeforeTemplate
Stream<T> before(T e1, T e2, T e3, T e4) {
return ImmutableList.of(e1, e2, e3, e4).stream();
}
@AfterTemplate
Stream<T> after(T e1, T e2, T e3, T e4) {
return Stream.of(e1, e2, e3, e4);
}
}
/** Prefer {@link Stream#of(Object[])} over more contrived alternatives. */
// XXX: Generalize this and similar rules using an Error Prone check.
static final class StreamOf5<T> {
@BeforeTemplate
Stream<T> before(T e1, T e2, T e3, T e4, T e5) {
return ImmutableList.of(e1, e2, e3, e4, e5).stream();
}
@AfterTemplate
Stream<T> after(T e1, T e2, T e3, T e4, T e5) {
return Stream.of(e1, e2, e3, e4, e5);
}
}
}

View File

@@ -20,6 +20,7 @@ import static java.util.stream.Collectors.summingDouble;
import static java.util.stream.Collectors.summingInt;
import static java.util.stream.Collectors.summingLong;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import java.util.DoubleSummaryStatistics;
@@ -36,6 +37,7 @@ final class StreamRulesTest implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(
ImmutableList.class,
Objects.class,
Streams.class,
counting(),
@@ -266,4 +268,24 @@ final class StreamRulesTest implements RefasterRuleCollectionTestCase {
Stream<Integer> testStreamIterate() {
return Stream.iterate(0, i -> i + 1).takeWhile(i -> i < 10);
}
Stream<Integer> testStreamOf1() {
return ImmutableList.of(1).stream();
}
Stream<Integer> testStreamOf2() {
return ImmutableList.of(1, 2).stream();
}
Stream<Integer> testStreamOf3() {
return ImmutableList.of(1, 2, 3).stream();
}
Stream<Integer> testStreamOf4() {
return ImmutableList.of(1, 2, 3, 4).stream();
}
Stream<Integer> testStreamOf5() {
return ImmutableList.of(1, 2, 3, 4, 5).stream();
}
}

View File

@@ -21,6 +21,7 @@ import static java.util.stream.Collectors.summingDouble;
import static java.util.stream.Collectors.summingInt;
import static java.util.stream.Collectors.summingLong;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import java.util.Arrays;
@@ -38,6 +39,7 @@ final class StreamRulesTest implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(
ImmutableList.class,
Objects.class,
Streams.class,
counting(),
@@ -266,4 +268,24 @@ final class StreamRulesTest implements RefasterRuleCollectionTestCase {
Stream<Integer> testStreamIterate() {
return Stream.iterate(0, i -> i < 10, i -> i + 1);
}
Stream<Integer> testStreamOf1() {
return Stream.of(1);
}
Stream<Integer> testStreamOf2() {
return Stream.of(1, 2);
}
Stream<Integer> testStreamOf3() {
return Stream.of(1, 2, 3);
}
Stream<Integer> testStreamOf4() {
return Stream.of(1, 2, 3, 4);
}
Stream<Integer> testStreamOf5() {
return Stream.of(1, 2, 3, 4, 5);
}
}