diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableListRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableListRules.java index 1848c268b9e..409f2915970 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableListRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableListRules.java @@ -263,4 +263,84 @@ ImmutableList after(T e1, T e2, T e3, T e4, T e5) { return ImmutableList.of(e1, e2, e3, e4, e5); } } + + /** + * Prefer {@link Stream#of(Object[])} over constructing a list and then directly calling {@link + * ImmutableList#stream()} on it. + */ + static final class StreamImmutableListOf1 { + @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 constructing a list and then directly calling {@link + * ImmutableList#stream()} on it. + */ + static final class StreamImmutableListOf2 { + @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 constructing a list and then directly calling {@link + * ImmutableList#stream()} on it. + */ + static final class StreamImmutableListOf3 { + @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 constructing a list and then directly calling {@link + * ImmutableList#stream()} on it. + */ + static final class StreamImmutableListOf4 { + @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 constructing a list and then directly calling {@link + * ImmutableList#stream()} on it. + */ + static final class StreamImmutableListOf5 { + @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/ImmutableListRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableListRulesTestInput.java index 84882105be8..e8f0cbaa01b 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableListRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableListRulesTestInput.java @@ -88,4 +88,24 @@ List testImmutableListOf4() { List testImmutableListOf5() { return List.of(1, 2, 3, 4, 5); } + + Stream testStreamImmutableListOf1() { + return ImmutableList.of(1).stream(); + } + + Stream testStreamImmutableListOf2() { + return ImmutableList.of(1, 2).stream(); + } + + Stream testStreamImmutableListOf3() { + return ImmutableList.of(1, 2, 3).stream(); + } + + Stream testStreamImmutableListOf4() { + return ImmutableList.of(1, 2, 3, 4).stream(); + } + + Stream testStreamImmutableListOf5() { + return ImmutableList.of(1, 2, 3, 4, 5).stream(); + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableListRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableListRulesTestOutput.java index b152693d25a..0f5288853c4 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableListRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableListRulesTestOutput.java @@ -83,4 +83,24 @@ List testImmutableListOf4() { List testImmutableListOf5() { return ImmutableList.of(1, 2, 3, 4, 5); } + + Stream testStreamImmutableListOf1() { + return Stream.of(1); + } + + Stream testStreamImmutableListOf2() { + return Stream.of(1, 2); + } + + Stream testStreamImmutableListOf3() { + return Stream.of(1, 2, 3); + } + + Stream testStreamImmutableListOf4() { + return Stream.of(1, 2, 3, 4); + } + + Stream testStreamImmutableListOf5() { + return Stream.of(1, 2, 3, 4, 5); + } }