Skip to content

Commit

Permalink
Introduce StreamOf{1,2,3,4,5} Refaster rules (#814)
Browse files Browse the repository at this point in the history
  • Loading branch information
Venorcis committed Oct 11, 2023
1 parent 05809b1 commit 4e2ceeb
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;
Expand Down Expand Up @@ -668,4 +669,74 @@ Stream<T> after(T seed, Predicate<? super T> hasNext, UnaryOperator<T> next) {
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;
Expand All @@ -36,6 +37,7 @@ final class StreamRulesTest implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(
ImmutableList.class,
Objects.class,
Streams.class,
counting(),
Expand Down Expand Up @@ -266,4 +268,24 @@ Stream<Integer> testStreamTakeWhile() {
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;
Expand All @@ -38,6 +39,7 @@ final class StreamRulesTest implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(
ImmutableList.class,
Objects.class,
Streams.class,
counting(),
Expand Down Expand Up @@ -266,4 +268,24 @@ Stream<Integer> testStreamTakeWhile() {
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);
}
}

0 comments on commit 4e2ceeb

Please sign in to comment.