From e405086ddbd21fc7f3a23dd83a2e261303b6b9dd Mon Sep 17 00:00:00 2001 From: mohamedsamehsalah Date: Mon, 30 Sep 2024 13:47:24 +0200 Subject: [PATCH 1/2] Extend `ImmutableCollectionIterator` refaster rule By re-writing `Stream#iterator` statements. --- .../picnic/errorprone/refasterrules/CollectionRules.java | 7 ++----- .../errorprone/refasterrules/CollectionRulesTestInput.java | 5 +++-- .../refasterrules/CollectionRulesTestOutput.java | 4 ++-- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/CollectionRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/CollectionRules.java index 4ee37189e1..376a2c43ed 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/CollectionRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/CollectionRules.java @@ -365,14 +365,11 @@ S[] after(ImmutableCollection collection, IntFunction generator) { } } - /** - * Don't call {@link ImmutableCollection#asList()} if {@link ImmutableCollection#iterator()} is - * called on the result; call it directly. - */ + /** Prefer {@link ImmutableCollection#iterator()} over more contrived alternatives. */ static final class ImmutableCollectionIterator { @BeforeTemplate Iterator before(ImmutableCollection collection) { - return collection.asList().iterator(); + return Refaster.anyOf(collection.stream().iterator(), collection.asList().iterator()); } @AfterTemplate diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestInput.java index c667a80895..75d9472d57 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestInput.java @@ -119,8 +119,9 @@ Integer[] testImmutableCollectionToArrayWithGenerator() { return ImmutableSet.of(1).asList().toArray(Integer[]::new); } - Iterator testImmutableCollectionIterator() { - return ImmutableSet.of(1).asList().iterator(); + ImmutableSet> testImmutableCollectionIterator() { + return ImmutableSet.of( + ImmutableSet.of(1).stream().iterator(), ImmutableSet.of(1).asList().iterator()); } ImmutableSet> testOptionalFirstCollectionElement() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestOutput.java index 756135c889..359feab508 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestOutput.java @@ -109,8 +109,8 @@ Integer[] testImmutableCollectionToArrayWithGenerator() { return ImmutableSet.of(1).toArray(Integer[]::new); } - Iterator testImmutableCollectionIterator() { - return ImmutableSet.of(1).iterator(); + ImmutableSet> testImmutableCollectionIterator() { + return ImmutableSet.of(ImmutableSet.of(1).iterator(), ImmutableSet.of(1).iterator()); } ImmutableSet> testOptionalFirstCollectionElement() { From 012b5e4409f8df6d151689f2abd97dc565f68d37 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Mon, 30 Sep 2024 18:58:39 +0200 Subject: [PATCH 2/2] Suggestions --- .../errorprone/refasterrules/CollectionRules.java | 13 +++++++++---- .../refasterrules/CollectionRulesTestInput.java | 4 ++-- .../refasterrules/CollectionRulesTestOutput.java | 4 ++-- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/CollectionRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/CollectionRules.java index 376a2c43ed..3a02f6fe5e 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/CollectionRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/CollectionRules.java @@ -365,15 +365,20 @@ S[] after(ImmutableCollection collection, IntFunction generator) { } } - /** Prefer {@link ImmutableCollection#iterator()} over more contrived alternatives. */ - static final class ImmutableCollectionIterator { + /** Prefer {@link Collection#iterator()} over more contrived or less efficient alternatives. */ + static final class CollectionIterator { + @BeforeTemplate + Iterator before(Collection collection) { + return collection.stream().iterator(); + } + @BeforeTemplate Iterator before(ImmutableCollection collection) { - return Refaster.anyOf(collection.stream().iterator(), collection.asList().iterator()); + return collection.asList().iterator(); } @AfterTemplate - Iterator after(ImmutableCollection collection) { + Iterator after(Collection collection) { return collection.iterator(); } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestInput.java index 75d9472d57..6936af3223 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestInput.java @@ -119,9 +119,9 @@ Integer[] testImmutableCollectionToArrayWithGenerator() { return ImmutableSet.of(1).asList().toArray(Integer[]::new); } - ImmutableSet> testImmutableCollectionIterator() { + ImmutableSet> testCollectionIterator() { return ImmutableSet.of( - ImmutableSet.of(1).stream().iterator(), ImmutableSet.of(1).asList().iterator()); + ImmutableSet.of(1).stream().iterator(), ImmutableSet.of(2).asList().iterator()); } ImmutableSet> testOptionalFirstCollectionElement() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestOutput.java index 359feab508..59367ba255 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestOutput.java @@ -109,8 +109,8 @@ Integer[] testImmutableCollectionToArrayWithGenerator() { return ImmutableSet.of(1).toArray(Integer[]::new); } - ImmutableSet> testImmutableCollectionIterator() { - return ImmutableSet.of(ImmutableSet.of(1).iterator(), ImmutableSet.of(1).iterator()); + ImmutableSet> testCollectionIterator() { + return ImmutableSet.of(ImmutableSet.of(1).iterator(), ImmutableSet.of(2).iterator()); } ImmutableSet> testOptionalFirstCollectionElement() {