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 StreamOf{1,2,3,4,5} Refaster rules #814

Merged
merged 2 commits into from
Oct 11, 2023
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 @@ -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);
}
}