From e4ac9b20ffadaa7c26052d838b9f9b6c6ffce35c Mon Sep 17 00:00:00 2001 From: mlrprananta Date: Tue, 9 May 2023 12:42:43 +0200 Subject: [PATCH 1/4] Prefer `Stream#concat` over contrived alternative --- .../picnic/errorprone/refasterrules/StreamRules.java | 12 ++++++++++++ .../refasterrules/StreamRulesTestInput.java | 4 ++++ .../refasterrules/StreamRulesTestOutput.java | 4 ++++ 3 files changed, 20 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 04499a86f7..00bd580ab2 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 @@ -623,4 +623,16 @@ R after( return stream.flatMap(mapper).collect(collector); } } + + static final class StreamConcat { + @BeforeTemplate + Stream before(Stream s1, Stream s2) { + return Stream.of(s1, s2).flatMap(Function.identity()); + } + + @AfterTemplate + Stream after(Stream s1, Stream s2) { + return Stream.concat(s1, s2); + } + } } 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 48f0de13a7..433484b466 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 @@ -250,4 +250,8 @@ ImmutableSet testStreamMapCollect() { ImmutableSet testStreamFlatMapCollect() { return Stream.of(1).collect(flatMapping(n -> Stream.of(n, n), toImmutableSet())); } + + Stream testStreamConcat() { + return Stream.of(Stream.of(1), Stream.of(2)).flatMap(Function.identity()); + } } 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 7ed6bbd4ae..48a1662e12 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 @@ -251,4 +251,8 @@ ImmutableSet testStreamMapCollect() { ImmutableSet testStreamFlatMapCollect() { return Stream.of(1).flatMap(n -> Stream.of(n, n)).collect(toImmutableSet()); } + + Stream testStreamConcat() { + return Stream.concat(Stream.of(1), Stream.of(2)); + } } From caf4e79ba9a3e6fa896f7bb0960a777ea518c210 Mon Sep 17 00:00:00 2001 From: mlrprananta Date: Tue, 9 May 2023 14:47:03 +0200 Subject: [PATCH 2/4] Prefer `Streams#concat` over contrived alternative --- .../errorprone/refasterrules/StreamRules.java | 13 +++++++++++++ .../refasterrules/StreamRulesTestInput.java | 5 +++++ .../refasterrules/StreamRulesTestOutput.java | 4 ++++ 3 files changed, 22 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 00bd580ab2..a55c102792 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 @@ -27,6 +27,7 @@ import com.google.errorprone.refaster.annotation.MayOptionallyUse; import com.google.errorprone.refaster.annotation.NotMatches; import com.google.errorprone.refaster.annotation.Placeholder; +import com.google.errorprone.refaster.annotation.Repeated; import com.google.errorprone.refaster.annotation.UseImportPolicy; import java.util.Arrays; import java.util.Collection; @@ -635,4 +636,16 @@ Stream after(Stream s1, Stream s2) { return Stream.concat(s1, s2); } } + + static final class StreamsConcat { + @BeforeTemplate + Stream before(@Repeated Stream stream) { + return Stream.of(Refaster.asVarargs(stream)).flatMap(Function.identity()); + } + + @AfterTemplate + Stream after(@Repeated Stream stream) { + return Streams.concat(Refaster.asVarargs(stream)); + } + } } 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 433484b466..e4aa2da0e9 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 @@ -254,4 +254,9 @@ ImmutableSet testStreamFlatMapCollect() { Stream testStreamConcat() { return Stream.of(Stream.of(1), Stream.of(2)).flatMap(Function.identity()); } + + Stream testStreamsConcat() { + return Stream.of(Stream.of(1), Stream.of(2), Stream.of(3), Stream.of(4)) + .flatMap(Function.identity()); + } } 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 48a1662e12..3c7260f371 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 @@ -255,4 +255,8 @@ ImmutableSet testStreamFlatMapCollect() { Stream testStreamConcat() { return Stream.concat(Stream.of(1), Stream.of(2)); } + + Stream testStreamsConcat() { + return Streams.concat(Stream.of(1), Stream.of(2), Stream.of(3), Stream.of(4)); + } } From 7b85413cc513046f6abe2c319eef302ef64c76c4 Mon Sep 17 00:00:00 2001 From: mlrprananta Date: Tue, 9 May 2023 16:49:04 +0200 Subject: [PATCH 3/4] Suggestion --- .../errorprone/refasterrules/StreamRules.java | 13 +------------ .../refasterrules/StreamRulesTestInput.java | 4 ---- .../refasterrules/StreamRulesTestOutput.java | 4 ---- 3 files changed, 1 insertion(+), 20 deletions(-) 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 a55c102792..1eaafc41e2 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 @@ -625,20 +625,9 @@ R after( } } - static final class StreamConcat { - @BeforeTemplate - Stream before(Stream s1, Stream s2) { - return Stream.of(s1, s2).flatMap(Function.identity()); - } - - @AfterTemplate - Stream after(Stream s1, Stream s2) { - return Stream.concat(s1, s2); - } - } - static final class StreamsConcat { @BeforeTemplate + @SuppressWarnings("StreamOfArray") Stream before(@Repeated Stream stream) { return Stream.of(Refaster.asVarargs(stream)).flatMap(Function.identity()); } 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 e4aa2da0e9..b3c3bfee94 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 @@ -251,10 +251,6 @@ ImmutableSet testStreamFlatMapCollect() { return Stream.of(1).collect(flatMapping(n -> Stream.of(n, n), toImmutableSet())); } - Stream testStreamConcat() { - return Stream.of(Stream.of(1), Stream.of(2)).flatMap(Function.identity()); - } - Stream testStreamsConcat() { return Stream.of(Stream.of(1), Stream.of(2), Stream.of(3), Stream.of(4)) .flatMap(Function.identity()); 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 3c7260f371..14066851e8 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 @@ -252,10 +252,6 @@ ImmutableSet testStreamFlatMapCollect() { return Stream.of(1).flatMap(n -> Stream.of(n, n)).collect(toImmutableSet()); } - Stream testStreamConcat() { - return Stream.concat(Stream.of(1), Stream.of(2)); - } - Stream testStreamsConcat() { return Streams.concat(Stream.of(1), Stream.of(2), Stream.of(3), Stream.of(4)); } From c937a5a7863612d99e4d63de8ea9509e68e8d4ca Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Thu, 11 May 2023 09:14:27 +0200 Subject: [PATCH 4/4] Suggestions --- .../picnic/errorprone/refasterrules/StreamRules.java | 4 ++-- .../errorprone/refasterrules/StreamRulesTestInput.java | 9 ++++++--- .../errorprone/refasterrules/StreamRulesTestOutput.java | 7 +++++-- 3 files changed, 13 insertions(+), 7 deletions(-) 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 1eaafc41e2..fdd5f2e77f 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 @@ -3,6 +3,7 @@ import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS; import static java.util.Comparator.naturalOrder; import static java.util.Comparator.reverseOrder; +import static java.util.function.Function.identity; import static java.util.function.Predicate.not; import static java.util.stream.Collectors.counting; import static java.util.stream.Collectors.filtering; @@ -627,9 +628,8 @@ R after( static final class StreamsConcat { @BeforeTemplate - @SuppressWarnings("StreamOfArray") Stream before(@Repeated Stream stream) { - return Stream.of(Refaster.asVarargs(stream)).flatMap(Function.identity()); + return Stream.of(Refaster.asVarargs(stream)).flatMap(Refaster.anyOf(identity(), s -> s)); } @AfterTemplate 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 b3c3bfee94..a9ec0e4ca1 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 @@ -3,6 +3,7 @@ import static com.google.common.collect.ImmutableSet.toImmutableSet; import static java.util.Comparator.comparingInt; import static java.util.Comparator.reverseOrder; +import static java.util.function.Function.identity; import static java.util.function.Predicate.not; import static java.util.stream.Collectors.counting; import static java.util.stream.Collectors.filtering; @@ -40,6 +41,7 @@ public ImmutableSet elidedTypesAndStaticImports() { counting(), filtering(null, null), flatMapping(null, null), + identity(), mapping(null, null), maxBy(null), minBy(null), @@ -251,8 +253,9 @@ ImmutableSet testStreamFlatMapCollect() { return Stream.of(1).collect(flatMapping(n -> Stream.of(n, n), toImmutableSet())); } - Stream testStreamsConcat() { - return Stream.of(Stream.of(1), Stream.of(2), Stream.of(3), Stream.of(4)) - .flatMap(Function.identity()); + ImmutableSet> testStreamsConcat() { + return ImmutableSet.of( + Stream.of(Stream.of(1), Stream.of(2)).flatMap(identity()), + Stream.of(Stream.of(3), Stream.of(4)).flatMap(v -> v)); } } 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 14066851e8..ab69c763bc 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 @@ -4,6 +4,7 @@ import static java.util.Comparator.comparingInt; import static java.util.Comparator.naturalOrder; import static java.util.Comparator.reverseOrder; +import static java.util.function.Function.identity; import static java.util.function.Predicate.not; import static java.util.stream.Collectors.counting; import static java.util.stream.Collectors.filtering; @@ -42,6 +43,7 @@ public ImmutableSet elidedTypesAndStaticImports() { counting(), filtering(null, null), flatMapping(null, null), + identity(), mapping(null, null), maxBy(null), minBy(null), @@ -252,7 +254,8 @@ ImmutableSet testStreamFlatMapCollect() { return Stream.of(1).flatMap(n -> Stream.of(n, n)).collect(toImmutableSet()); } - Stream testStreamsConcat() { - return Streams.concat(Stream.of(1), Stream.of(2), Stream.of(3), Stream.of(4)); + ImmutableSet> testStreamsConcat() { + return ImmutableSet.of( + Streams.concat(Stream.of(1), Stream.of(2)), Streams.concat(Stream.of(3), Stream.of(4))); } }