From e7d61769c1151c756274a8db36283300a72bbe7f Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sat, 7 Oct 2023 15:51:25 +0200 Subject: [PATCH] Suggestions --- .../refasterrules/ImmutableListRules.java | 80 ------------------- .../errorprone/refasterrules/StreamRules.java | 71 ++++++++++++++++ .../ImmutableListRulesTestInput.java | 20 ----- .../ImmutableListRulesTestOutput.java | 20 ----- .../refasterrules/StreamRulesTestInput.java | 22 +++++ .../refasterrules/StreamRulesTestOutput.java | 22 +++++ 6 files changed, 115 insertions(+), 120 deletions(-) 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 409f2915970..1848c268b9e 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,84 +263,4 @@ 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/main/java/tech/picnic/errorprone/refasterrules/StreamRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/StreamRules.java index 800fdb63861..2d302236d1d 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/ImmutableListRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableListRulesTestInput.java index e8f0cbaa01b..84882105be8 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,24 +88,4 @@ 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 0f5288853c4..b152693d25a 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,24 +83,4 @@ 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); - } } 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 46b9cae01cf..a03da8b93c7 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 1db1d1a5e7e..d26a8c589fd 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); + } }