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 e7bea741423..ce4072a30b0 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 @@ -38,6 +38,7 @@ import java.util.Comparator; import java.util.DoubleSummaryStatistics; import java.util.IntSummaryStatistics; +import java.util.List; import java.util.LongSummaryStatistics; import java.util.Map; import java.util.Objects; @@ -122,6 +123,21 @@ Stream after(T[] array) { } } + /** + * Prefer {@link Arrays#asList(Object[])} over {@link Stream#toList()} as the former is clearer. + */ + static final class ArraysAsList { + @BeforeTemplate + List before(@NotMatches(IsRefasterAsVarargs.class) T[] array) { + return Refaster.anyOf(Arrays.stream(array).toList(), Stream.of(array).toList()); + } + + @AfterTemplate + List after(T[] array) { + return Arrays.asList(array); + } + } + /** Don't unnecessarily call {@link Streams#concat(Stream...)}. */ static final class ConcatOneStream { @BeforeTemplate 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 5126726c3bf..202068e4685 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 @@ -27,6 +27,7 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Streams; +import java.util.Arrays; import java.util.DoubleSummaryStatistics; import java.util.IntSummaryStatistics; import java.util.List; @@ -86,6 +87,12 @@ Stream testStreamOfArray() { return Stream.of(new String[] {"foo", "bar"}); } + ImmutableSet> testArraysAsList() { + return ImmutableSet.of( + Arrays.stream(new String[] {"foo", "bar"}).toList(), + Stream.of(new String[] {"foo", "bar"}).toList()); + } + Stream testConcatOneStream() { return Streams.concat(Stream.of(1)); } 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 a11b7ca644c..17957b2a8ec 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 @@ -87,6 +87,11 @@ Stream testStreamOfArray() { return Arrays.stream(new String[] {"foo", "bar"}); } + ImmutableSet> testArraysAsList() { + return ImmutableSet.of( + Arrays.asList(new String[] {"foo", "bar"}), Arrays.asList(new String[] {"foo", "bar"})); + } + Stream testConcatOneStream() { return Stream.of(1); }