From 4e2ceeb25231b01d00e8a5f6e5731c1bfcc343e3 Mon Sep 17 00:00:00 2001 From: Vincent Koeman Date: Wed, 11 Oct 2023 17:52:52 +0200 Subject: [PATCH] Introduce `StreamOf{1,2,3,4,5}` Refaster rules (#814) --- .../errorprone/refasterrules/StreamRules.java | 71 +++++++++++++++++++ .../refasterrules/StreamRulesTestInput.java | 22 ++++++ .../refasterrules/StreamRulesTestOutput.java | 22 ++++++ 3 files changed, 115 insertions(+) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/StreamRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/StreamRules.java index 800fdb6386..2d302236d1 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/StreamRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/StreamRules.java @@ -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; @@ -668,4 +669,74 @@ Stream after(T seed, Predicate hasNext, UnaryOperator 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 { + @BeforeTemplate + Stream before(T e1) { + return ImmutableList.of(e1).stream(); + } + + @AfterTemplate + Stream 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 { + @BeforeTemplate + Stream before(T e1, T e2) { + return ImmutableList.of(e1, e2).stream(); + } + + @AfterTemplate + Stream 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 { + @BeforeTemplate + Stream before(T e1, T e2, T e3) { + return ImmutableList.of(e1, e2, e3).stream(); + } + + @AfterTemplate + Stream 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 { + @BeforeTemplate + Stream before(T e1, T e2, T e3, T e4) { + return ImmutableList.of(e1, e2, e3, e4).stream(); + } + + @AfterTemplate + Stream 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 { + @BeforeTemplate + Stream before(T e1, T e2, T e3, T e4, T e5) { + return ImmutableList.of(e1, e2, e3, e4, e5).stream(); + } + + @AfterTemplate + Stream after(T e1, T e2, T e3, T e4, T e5) { + return Stream.of(e1, e2, e3, e4, e5); + } + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StreamRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StreamRulesTestInput.java index 46b9cae01c..a03da8b93c 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StreamRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StreamRulesTestInput.java @@ -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; @@ -36,6 +37,7 @@ final class StreamRulesTest implements RefasterRuleCollectionTestCase { @Override public ImmutableSet elidedTypesAndStaticImports() { return ImmutableSet.of( + ImmutableList.class, Objects.class, Streams.class, counting(), @@ -266,4 +268,24 @@ Stream testStreamTakeWhile() { Stream testStreamIterate() { return Stream.iterate(0, i -> i + 1).takeWhile(i -> i < 10); } + + Stream testStreamOf1() { + return ImmutableList.of(1).stream(); + } + + Stream testStreamOf2() { + return ImmutableList.of(1, 2).stream(); + } + + Stream testStreamOf3() { + return ImmutableList.of(1, 2, 3).stream(); + } + + Stream testStreamOf4() { + return ImmutableList.of(1, 2, 3, 4).stream(); + } + + Stream testStreamOf5() { + return ImmutableList.of(1, 2, 3, 4, 5).stream(); + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StreamRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StreamRulesTestOutput.java index 1db1d1a5e7..d26a8c589f 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StreamRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StreamRulesTestOutput.java @@ -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; @@ -38,6 +39,7 @@ final class StreamRulesTest implements RefasterRuleCollectionTestCase { @Override public ImmutableSet elidedTypesAndStaticImports() { return ImmutableSet.of( + ImmutableList.class, Objects.class, Streams.class, counting(), @@ -266,4 +268,24 @@ Stream testStreamTakeWhile() { Stream testStreamIterate() { return Stream.iterate(0, i -> i < 10, i -> i + 1); } + + Stream testStreamOf1() { + return Stream.of(1); + } + + Stream testStreamOf2() { + return Stream.of(1, 2); + } + + Stream testStreamOf3() { + return Stream.of(1, 2, 3); + } + + Stream testStreamOf4() { + return Stream.of(1, 2, 3, 4); + } + + Stream testStreamOf5() { + return Stream.of(1, 2, 3, 4, 5); + } }