From 29a031dcdd34b1034e3320e039e43e6c31c645ad Mon Sep 17 00:00:00 2001 From: Vitor Mussatto Date: Fri, 18 Feb 2022 10:25:47 +0100 Subject: [PATCH 01/15] Introduce new templates to use Immutables --- .../CollectionTemplates.java | 41 +++++++++++++++++++ .../CollectionTemplatesTestInput.java | 30 +++++++++++++- .../CollectionTemplatesTestOutput.java | 31 +++++++++++++- 3 files changed, 100 insertions(+), 2 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/CollectionTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/CollectionTemplates.java index aea3761bb7..3eb7ce23f3 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/CollectionTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/CollectionTemplates.java @@ -2,6 +2,8 @@ import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.errorprone.refaster.Refaster; @@ -10,11 +12,14 @@ import com.google.errorprone.refaster.annotation.BeforeTemplate; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.NavigableSet; import java.util.Optional; import java.util.Queue; +import java.util.Set; import java.util.SortedSet; import java.util.function.IntFunction; import java.util.stream.Stream; @@ -402,4 +407,40 @@ Optional after(Queue queue) { // XXX: collection.stream().noneMatch(e -> e.equals(other)) // ^ This is !collection.contains(other). Do we already rewrite variations on this? + + static final class ImmutableSetOf { + @BeforeTemplate + Set before() { + return Collections.emptySet(); + } + + @AfterTemplate + ImmutableSet after() { + return ImmutableSet.of(); + } + } + + static final class ImmutableListOf { + @BeforeTemplate + List before() { + return Collections.emptyList(); + } + + @AfterTemplate + ImmutableList after() { + return ImmutableList.of(); + } + } + + static final class ImmutableMapOf { + @BeforeTemplate + Map before() { + return Collections.emptyMap(); + } + + @AfterTemplate + ImmutableMap after() { + return ImmutableMap.of(); + } + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/CollectionTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/CollectionTemplatesTestInput.java index 3e67f56412..f430dee397 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/CollectionTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/CollectionTemplatesTestInput.java @@ -6,16 +6,20 @@ import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import java.util.ArrayList; +import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; +import java.util.List; +import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.TreeSet; import java.util.stream.Stream; final class CollectionTemplatesTest implements RefasterTemplateTestCase { @Override public ImmutableSet elidedTypesAndStaticImports() { - return ImmutableSet.of(Iterables.class, Lists.class); + return ImmutableSet.of(Collections.class, Iterables.class, Lists.class); } ImmutableSet testCollectionIsEmpty() { @@ -184,4 +188,28 @@ ImmutableSet> testRemoveOptionalFirstQueueElement() { ? Optional.ofNullable(new LinkedList().remove()) : Optional.empty()); } + + Set testImmutableSetOf() { + return Collections.emptySet(); + } + + Set testImmutableSetOfTyped() { + return Collections.emptySet(); + } + + List testImmutableListOf() { + return Collections.emptyList(); + } + + List testImmutableListOfTyped() { + return Collections.emptyList(); + } + + Map testImmutableMapOf() { + return Collections.emptyMap(); + } + + Map testImmutableMapOfTyped() { + return Collections.emptyMap(); + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/CollectionTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/CollectionTemplatesTestOutput.java index 970e5e346d..6691976a76 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/CollectionTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/CollectionTemplatesTestOutput.java @@ -1,21 +1,26 @@ package tech.picnic.errorprone.bugpatterns; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSortedSet; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import java.util.ArrayList; +import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; +import java.util.List; +import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.TreeSet; import java.util.stream.Stream; final class CollectionTemplatesTest implements RefasterTemplateTestCase { @Override public ImmutableSet elidedTypesAndStaticImports() { - return ImmutableSet.of(Iterables.class, Lists.class); + return ImmutableSet.of(Collections.class, Iterables.class, Lists.class); } ImmutableSet testCollectionIsEmpty() { @@ -134,4 +139,28 @@ ImmutableSet> testRemoveOptionalFirstQueueElement() { Optional.ofNullable(new LinkedList().poll()), Optional.ofNullable(new LinkedList().poll())); } + + Set testImmutableSetOf() { + return ImmutableSet.of(); + } + + Set testImmutableSetOfTyped() { + return ImmutableSet.of(); + } + + List testImmutableListOf() { + return ImmutableList.of(); + } + + List testImmutableListOfTyped() { + return ImmutableList.of(); + } + + Map testImmutableMapOf() { + return ImmutableMap.of(); + } + + Map testImmutableMapOfTyped() { + return ImmutableMap.of(); + } } From 2904a750c2bc822080e6c6f3c9f4202fe15faf39 Mon Sep 17 00:00:00 2001 From: Vitor Mussatto Date: Fri, 18 Feb 2022 17:33:35 +0100 Subject: [PATCH 02/15] Adding listOf template --- .../CollectionTemplates.java | 40 ------------------- .../ImmutableListTemplates.java | 25 ++++++++++++ .../ImmutableMapTemplates.java | 25 ++++++++++++ .../ImmutableSetTemplates.java | 12 ++++++ .../CollectionTemplatesTestInput.java | 30 +------------- .../CollectionTemplatesTestOutput.java | 31 +------------- .../ImmutableListTemplatesTestInput.java | 14 +++++++ .../ImmutableListTemplatesTestOutput.java | 14 +++++++ .../ImmutableMapTemplatesTestInput.java | 8 ++++ .../ImmutableMapTemplatesTestOutput.java | 8 ++++ .../ImmutableSetTemplatesTestInput.java | 8 ++++ .../ImmutableSetTemplatesTestOutput.java | 8 ++++ 12 files changed, 124 insertions(+), 99 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/CollectionTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/CollectionTemplates.java index 3eb7ce23f3..91f89421de 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/CollectionTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/CollectionTemplates.java @@ -2,8 +2,6 @@ import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.errorprone.refaster.Refaster; @@ -12,14 +10,11 @@ import com.google.errorprone.refaster.annotation.BeforeTemplate; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.Iterator; import java.util.List; -import java.util.Map; import java.util.NavigableSet; import java.util.Optional; import java.util.Queue; -import java.util.Set; import java.util.SortedSet; import java.util.function.IntFunction; import java.util.stream.Stream; @@ -408,39 +403,4 @@ Optional after(Queue queue) { // XXX: collection.stream().noneMatch(e -> e.equals(other)) // ^ This is !collection.contains(other). Do we already rewrite variations on this? - static final class ImmutableSetOf { - @BeforeTemplate - Set before() { - return Collections.emptySet(); - } - - @AfterTemplate - ImmutableSet after() { - return ImmutableSet.of(); - } - } - - static final class ImmutableListOf { - @BeforeTemplate - List before() { - return Collections.emptyList(); - } - - @AfterTemplate - ImmutableList after() { - return ImmutableList.of(); - } - } - - static final class ImmutableMapOf { - @BeforeTemplate - Map before() { - return Collections.emptyMap(); - } - - @AfterTemplate - ImmutableMap after() { - return ImmutableMap.of(); - } - } } diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java index a7aaf0c316..84c30575dd 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java @@ -7,6 +7,7 @@ import static java.util.stream.Collectors.collectingAndThen; import static java.util.stream.Collectors.toList; +import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Streams; @@ -186,4 +187,28 @@ ImmutableList after(Stream stream) { return stream.collect(toImmutableSet()).asList(); } } + + static final class ImmutableListOf { + @BeforeTemplate + List before() { + return Collections.emptyList(); + } + + @AfterTemplate + ImmutableList after() { + return ImmutableList.of(); + } + } + + static final class ImmutableListOf1 { + @BeforeTemplate + List before(T item) { + return List.of(item); + } + + @AfterTemplate + ImmutableCollection after(T item) { + return ImmutableList.of(item); + } + } } diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java index 5c8aa28142..cfdb3ba4d5 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java @@ -242,6 +242,31 @@ ImmutableMap after(Map map) { } } + /** Don't unnecessarily copy an {@link ImmutableMap}. */ + static final class ImmutableMapCopyOfImmutableMap { + @BeforeTemplate + ImmutableMap before(ImmutableMap map) { + return ImmutableMap.copyOf(map); + } + + @AfterTemplate + ImmutableMap after(ImmutableMap map) { + return map; + } + } + + static final class ImmutableMapOf { + @BeforeTemplate + Map before() { + return Collections.emptyMap(); + } + + @AfterTemplate + ImmutableMap after() { + return ImmutableMap.of(); + } + } + // XXX: Add a template for this: // Maps.transformValues(streamOfEntries.collect(groupBy(fun)), ImmutableMap::copyOf) // -> diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java index 7e19377eaa..664e0878cf 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java @@ -138,4 +138,16 @@ ImmutableSet after(SetView set) { return set.immutableCopy(); } } + + static final class ImmutableSetOf { + @BeforeTemplate + Set before() { + return Collections.emptySet(); + } + + @AfterTemplate + ImmutableSet after() { + return ImmutableSet.of(); + } + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/CollectionTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/CollectionTemplatesTestInput.java index f430dee397..3e67f56412 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/CollectionTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/CollectionTemplatesTestInput.java @@ -6,20 +6,16 @@ import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import java.util.ArrayList; -import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; -import java.util.List; -import java.util.Map; import java.util.Optional; -import java.util.Set; import java.util.TreeSet; import java.util.stream.Stream; final class CollectionTemplatesTest implements RefasterTemplateTestCase { @Override public ImmutableSet elidedTypesAndStaticImports() { - return ImmutableSet.of(Collections.class, Iterables.class, Lists.class); + return ImmutableSet.of(Iterables.class, Lists.class); } ImmutableSet testCollectionIsEmpty() { @@ -188,28 +184,4 @@ ImmutableSet> testRemoveOptionalFirstQueueElement() { ? Optional.ofNullable(new LinkedList().remove()) : Optional.empty()); } - - Set testImmutableSetOf() { - return Collections.emptySet(); - } - - Set testImmutableSetOfTyped() { - return Collections.emptySet(); - } - - List testImmutableListOf() { - return Collections.emptyList(); - } - - List testImmutableListOfTyped() { - return Collections.emptyList(); - } - - Map testImmutableMapOf() { - return Collections.emptyMap(); - } - - Map testImmutableMapOfTyped() { - return Collections.emptyMap(); - } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/CollectionTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/CollectionTemplatesTestOutput.java index 6691976a76..970e5e346d 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/CollectionTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/CollectionTemplatesTestOutput.java @@ -1,26 +1,21 @@ package tech.picnic.errorprone.bugpatterns; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSortedSet; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import java.util.ArrayList; -import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; -import java.util.List; -import java.util.Map; import java.util.Optional; -import java.util.Set; import java.util.TreeSet; import java.util.stream.Stream; final class CollectionTemplatesTest implements RefasterTemplateTestCase { @Override public ImmutableSet elidedTypesAndStaticImports() { - return ImmutableSet.of(Collections.class, Iterables.class, Lists.class); + return ImmutableSet.of(Iterables.class, Lists.class); } ImmutableSet testCollectionIsEmpty() { @@ -139,28 +134,4 @@ ImmutableSet> testRemoveOptionalFirstQueueElement() { Optional.ofNullable(new LinkedList().poll()), Optional.ofNullable(new LinkedList().poll())); } - - Set testImmutableSetOf() { - return ImmutableSet.of(); - } - - Set testImmutableSetOfTyped() { - return ImmutableSet.of(); - } - - List testImmutableListOf() { - return ImmutableList.of(); - } - - List testImmutableListOfTyped() { - return ImmutableList.of(); - } - - Map testImmutableMapOf() { - return ImmutableMap.of(); - } - - Map testImmutableMapOfTyped() { - return ImmutableMap.of(); - } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java index f2d564e343..6b84e41d3c 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java @@ -9,6 +9,7 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Streams; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -19,6 +20,7 @@ final class ImmutableListTemplatesTest implements RefasterTemplateTestCase { public ImmutableSet elidedTypesAndStaticImports() { return ImmutableSet.of( Arrays.class, + Collection.class, Collections.class, Comparator.class, Streams.class, @@ -79,4 +81,16 @@ ImmutableSet> testImmutableListSortedCopyOfWithCustomCompa ImmutableList testStreamToDistinctImmutableList() { return Stream.of(1).distinct().collect(toImmutableList()); } + + List testImmutableListOf() { + return Collections.emptyList(); + } + + List testImmutableListOfTyped() { + return Collections.emptyList(); + } + + ImmutableSet> testImmutableList1() { + return ImmutableSet.of(List.of("1")); + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java index f13829aad0..fede952e20 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java @@ -10,6 +10,7 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Streams; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -20,6 +21,7 @@ final class ImmutableListTemplatesTest implements RefasterTemplateTestCase { public ImmutableSet elidedTypesAndStaticImports() { return ImmutableSet.of( Arrays.class, + Collection.class, Collections.class, Comparator.class, Streams.class, @@ -74,4 +76,16 @@ ImmutableSet> testImmutableListSortedCopyOfWithCustomCompa ImmutableList testStreamToDistinctImmutableList() { return Stream.of(1).collect(toImmutableSet()).asList(); } + + List testImmutableListOf() { + return ImmutableList.of(); + } + + List testImmutableListOfTyped() { + return ImmutableList.of(); + } + + ImmutableSet> testImmutableList1() { + return ImmutableSet.of(ImmutableList.of("1")); + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java index d119cd9763..723f993e71 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java @@ -86,4 +86,12 @@ ImmutableSet> testTransformMapValuesToImmutableMap ImmutableMap.of("bar", 2L).keySet(), k -> Math.toIntExact(ImmutableMap.of("bar", 2L).get(k)))); } + + Map testImmutableMapOf() { + return Collections.emptyMap(); + } + + Map testImmutableMapOfTyped() { + return Collections.emptyMap(); + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java index e1638de70e..0a61f5a75b 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java @@ -72,4 +72,12 @@ ImmutableSet> testTransformMapValuesToImmutableMap ImmutableMap.copyOf( Maps.transformValues(ImmutableMap.of("bar", 2L), v -> Math.toIntExact(v)))); } + + Map testImmutableMapOf() { + return ImmutableMap.of(); + } + + Map testImmutableMapOfTyped() { + return ImmutableMap.of(); + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java index 4fd3650179..147902b8ee 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java @@ -62,4 +62,12 @@ ImmutableSet> testStreamToImmutableSet() { ImmutableSet testImmutableSetCopyOfSetView() { return ImmutableSet.copyOf(Sets.difference(ImmutableSet.of(1), ImmutableSet.of(2))); } + + Set testImmutableSetOf() { + return Collections.emptySet(); + } + + Set testImmutableSetOfTyped() { + return Collections.emptySet(); + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java index b23aac8871..0fbc0fbccb 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java @@ -61,4 +61,12 @@ ImmutableSet> testStreamToImmutableSet() { ImmutableSet testImmutableSetCopyOfSetView() { return Sets.difference(ImmutableSet.of(1), ImmutableSet.of(2)).immutableCopy(); } + + Set testImmutableSetOf() { + return ImmutableSet.of(); + } + + Set testImmutableSetOfTyped() { + return ImmutableSet.of(); + } } From be9117d50b4b1e8e4076b5750a01f38fdcb8b9d4 Mon Sep 17 00:00:00 2001 From: Vitor Mussatto Date: Mon, 21 Feb 2022 15:00:19 +0100 Subject: [PATCH 03/15] Adding listOf templates for up to 5 items --- .../CollectionTemplates.java | 1 - .../ImmutableListTemplates.java | 48 +++++++++++++++ .../ImmutableMapTemplates.java | 59 ++++++++++++++++++ .../ImmutableSetTemplates.java | 60 +++++++++++++++++++ .../ImmutableListTemplatesTestInput.java | 20 ++++++- .../ImmutableListTemplatesTestOutput.java | 20 ++++++- .../ImmutableMapTemplatesTestInput.java | 12 +++- .../ImmutableMapTemplatesTestOutput.java | 12 +++- .../ImmutableSetTemplatesTestInput.java | 21 +++++++ .../ImmutableSetTemplatesTestOutput.java | 21 +++++++ 10 files changed, 263 insertions(+), 11 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/CollectionTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/CollectionTemplates.java index 91f89421de..aea3761bb7 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/CollectionTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/CollectionTemplates.java @@ -402,5 +402,4 @@ Optional after(Queue queue) { // XXX: collection.stream().noneMatch(e -> e.equals(other)) // ^ This is !collection.contains(other). Do we already rewrite variations on this? - } diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java index 84c30575dd..d88e3ad2c1 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java @@ -211,4 +211,52 @@ ImmutableCollection after(T item) { return ImmutableList.of(item); } } + + static final class ImmutableListOf2 { + @BeforeTemplate + List before(T item, T item2) { + return List.of(item, item2); + } + + @AfterTemplate + ImmutableCollection after(T item, T item2) { + return ImmutableList.of(item, item2); + } + } + + static final class ImmutableListOf3 { + @BeforeTemplate + List before(T i1, T i2, T i3) { + return List.of(i1, i2, i3); + } + + @AfterTemplate + ImmutableCollection after(T i1, T i2, T i3) { + return ImmutableList.of(i1, i2, i3); + } + } + + static final class ImmutableListOf4 { + @BeforeTemplate + List before(T i1, T i2, T i3, T i4) { + return List.of(i1, i2, i3, i4); + } + + @AfterTemplate + ImmutableCollection after(T i1, T i2, T i3, T i4) { + return ImmutableList.of(i1, i2, i3, i4); + } + } + + static final class ImmutableListOf5 { + @BeforeTemplate + List before(T i1, T i2, T i3, T i4, T i5) { + return List.of(i1, i2, i3, i4, i5); + } + + @AfterTemplate + ImmutableCollection after(T i1, T i2, T i3, T i4, T i5) { + return ImmutableList.of(i1, i2, i3, i4, i5); + } + } } diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java index cfdb3ba4d5..acaa80f7ac 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java @@ -267,6 +267,65 @@ ImmutableMap after() { } } + static final class ImmutableMapOf1 { + @BeforeTemplate + Map before(K k1, V v1) { + return Map.of(k1, v1); + } + + @AfterTemplate + ImmutableMap after(K k1, V v1) { + return ImmutableMap.of(k1, v1); + } + } + + static final class ImmutableMapOf2 { + @BeforeTemplate + Map before(K k1, V v1, K k2, V v2) { + return Map.of(k1, v1, k2, v2); + } + + @AfterTemplate + ImmutableMap after(K k1, V v1, K k2, V v2) { + return ImmutableMap.of(k1, v1, k2, v2); + } + } + + static final class ImmutableMapOf3 { + @BeforeTemplate + Map before(K k1, V v1, K k2, V v2, K k3, V v3) { + return Map.of(k1, v1, k2, v2, k3, v3); + } + + @AfterTemplate + ImmutableMap after(K k1, V v1, K k2, V v2, K k3, V v3) { + return ImmutableMap.of(k1, v1, k2, v2, k3, v3); + } + } + + static final class ImmutableMapOf4 { + @BeforeTemplate + Map before(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { + return Map.of(k1, v1, k2, v2, k3, v3, k4, v4); + } + + @AfterTemplate + ImmutableMap after(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { + return ImmutableMap.of(k1, v1, k2, v2, k3, v3, k4, v4); + } + } + + static final class ImmutableMapOf5 { + @BeforeTemplate + Map before(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) { + return Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5); + } + + @AfterTemplate + ImmutableMap after(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) { + return ImmutableMap.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5); + } + } // XXX: Add a template for this: // Maps.transformValues(streamOfEntries.collect(groupBy(fun)), ImmutableMap::copyOf) // -> diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java index 664e0878cf..90756a5bbf 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java @@ -150,4 +150,64 @@ ImmutableSet after() { return ImmutableSet.of(); } } + + static final class ImmutableSetOfItems1 { + @BeforeTemplate + Set before(T e1) { + return Set.of(e1); + } + + @AfterTemplate + ImmutableSet after(T e1) { + return ImmutableSet.of(e1); + } + } + + static final class ImmutableSetOfItems2 { + @BeforeTemplate + Set before(T e1, T e2) { + return Set.of(e1, e2); + } + + @AfterTemplate + ImmutableSet after(T e1, T e2) { + return ImmutableSet.of(e1, e2); + } + } + + static final class ImmutableSetOfItems3 { + @BeforeTemplate + Set before(T e1, T e2, T e3) { + return Set.of(e1, e2, e3); + } + + @AfterTemplate + ImmutableSet after(T e1, T e2, T e3) { + return ImmutableSet.of(e1, e2, e3); + } + } + + static final class ImmutableSetOfItems4 { + @BeforeTemplate + Set before(T e1, T e2, T e3, T e4) { + return Set.of(e1, e2, e3, e4); + } + + @AfterTemplate + ImmutableSet after(T e1, T e2, T e3, T e4) { + return ImmutableSet.of(e1, e2, e3, e4); + } + } + + static final class ImmutableSetOfItems5 { + @BeforeTemplate + Set before(T e1, T e2, T e3, T e4, T e5) { + return Set.of(e1, e2, e3, e4, e5); + } + + @AfterTemplate + ImmutableSet after(T e1, T e2, T e3, T e4, T e5) { + return ImmutableSet.of(e1, e2, e3, e4, e5); + } + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java index 6b84e41d3c..f4b56376db 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java @@ -90,7 +90,23 @@ List testImmutableListOfTyped() { return Collections.emptyList(); } - ImmutableSet> testImmutableList1() { - return ImmutableSet.of(List.of("1")); + Collection testImmutableListOf1() { + return List.of("1"); + } + + Collection testImmutableListOf2() { + return List.of("1", "2"); + } + + Collection testImmutableListOf3() { + return List.of("1", "2", "3"); + } + + Collection testImmutableListOf4() { + return List.of("1", "2", "3", "4"); + } + + Collection testImmutableListOf5() { + return List.of("1", "2", "3", "4", "5"); } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java index fede952e20..18d9b55901 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java @@ -85,7 +85,23 @@ List testImmutableListOfTyped() { return ImmutableList.of(); } - ImmutableSet> testImmutableList1() { - return ImmutableSet.of(ImmutableList.of("1")); + Collection testImmutableListOf1() { + return ImmutableList.of("1"); + } + + Collection testImmutableListOf2() { + return ImmutableList.of("1", "2"); + } + + Collection testImmutableListOf3() { + return ImmutableList.of("1", "2", "3"); + } + + Collection testImmutableListOf4() { + return ImmutableList.of("1", "2", "3", "4"); + } + + Collection testImmutableListOf5() { + return ImmutableList.of("1", "2", "3", "4", "5"); } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java index 723f993e71..ed6921056c 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java @@ -87,11 +87,17 @@ ImmutableSet> testTransformMapValuesToImmutableMap k -> Math.toIntExact(ImmutableMap.of("bar", 2L).get(k)))); } - Map testImmutableMapOf() { + Map testImmutableMapOfTyped() { return Collections.emptyMap(); } - Map testImmutableMapOfTyped() { - return Collections.emptyMap(); + ImmutableSet> testImmutableMapOfN() { + return ImmutableSet.of( + Collections.emptyMap(), + Map.of("k1", "v1"), + Map.of("k1", "v1", "k2", "v2"), + Map.of("k1", "v1", "k2", "v2", "k3", "v3"), + Map.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4"), + Map.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4", "k5", "v5")); } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java index 0a61f5a75b..7dad01ab47 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java @@ -73,11 +73,17 @@ ImmutableSet> testTransformMapValuesToImmutableMap Maps.transformValues(ImmutableMap.of("bar", 2L), v -> Math.toIntExact(v)))); } - Map testImmutableMapOf() { + Map testImmutableMapOfTyped() { return ImmutableMap.of(); } - Map testImmutableMapOfTyped() { - return ImmutableMap.of(); + ImmutableSet> testImmutableMapOfN() { + return ImmutableSet.of( + ImmutableMap.of(), + ImmutableMap.of("k1", "v1"), + ImmutableMap.of("k1", "v1", "k2", "v2"), + ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3"), + ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4"), + ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4", "k5", "v5")); } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java index 147902b8ee..1097766df4 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java @@ -10,6 +10,7 @@ import com.google.common.collect.Sets; import com.google.common.collect.Streams; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.Set; import java.util.stream.Stream; @@ -70,4 +71,24 @@ Set testImmutableSetOf() { Set testImmutableSetOfTyped() { return Collections.emptySet(); } + + Collection testImmutableSetOfItems1() { + return Set.of("1"); + } + + Collection testImmutableSetOfItems2() { + return Set.of("1", "2"); + } + + Collection testImmutableSetOfItems3() { + return Set.of("1", "2", "3"); + } + + Collection testImmutableSetOfItems4() { + return Set.of("1", "2", "3", "4"); + } + + Collection testImmutableSetOfItems5() { + return Set.of("1", "2", "3", "4", "5"); + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java index 0fbc0fbccb..868e301423 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java @@ -10,6 +10,7 @@ import com.google.common.collect.Sets; import com.google.common.collect.Streams; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.Set; import java.util.stream.Stream; @@ -69,4 +70,24 @@ Set testImmutableSetOf() { Set testImmutableSetOfTyped() { return ImmutableSet.of(); } + + Collection testImmutableSetOfItems1() { + return ImmutableSet.of("1"); + } + + Collection testImmutableSetOfItems2() { + return ImmutableSet.of("1", "2"); + } + + Collection testImmutableSetOfItems3() { + return ImmutableSet.of("1", "2", "3"); + } + + Collection testImmutableSetOfItems4() { + return ImmutableSet.of("1", "2", "3", "4"); + } + + Collection testImmutableSetOfItems5() { + return ImmutableSet.of("1", "2", "3", "4", "5"); + } } From 55a2437ec3f49a637d56dd27caa6c7d0648c9739 Mon Sep 17 00:00:00 2001 From: Vitor Mussatto Date: Wed, 23 Feb 2022 13:12:36 +0100 Subject: [PATCH 04/15] CR remarks, adding javadoc, updating tests --- .../ImmutableListTemplates.java | 59 +++++++++++++------ .../ImmutableMapTemplates.java | 27 +++++++++ .../ImmutableSetTemplates.java | 24 ++++++++ .../ImmutableListTemplatesTestInput.java | 10 ++-- .../ImmutableListTemplatesTestOutput.java | 10 ++-- .../ImmutableMapTemplatesTestInput.java | 30 +++++++--- .../ImmutableMapTemplatesTestOutput.java | 30 +++++++--- .../ImmutableSetTemplatesTestInput.java | 11 ++-- .../ImmutableSetTemplatesTestOutput.java | 11 ++-- 9 files changed, 156 insertions(+), 56 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java index d88e3ad2c1..6749af867b 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java @@ -7,7 +7,6 @@ import static java.util.stream.Collectors.collectingAndThen; import static java.util.stream.Collectors.toList; -import com.google.common.collect.ImmutableCollection; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Streams; @@ -188,6 +187,10 @@ ImmutableList after(Stream stream) { } } + /** + * Prefer {@link ImmutableList#of(Object)} over alternatives that don't communicate the + * immutability of the resulting list at the type level. + */ static final class ImmutableListOf { @BeforeTemplate List before() { @@ -200,6 +203,10 @@ ImmutableList after() { } } + /** + * Prefer {@link ImmutableList#of(Object)} over alternatives that don't communicate the + * immutability of the resulting list at the type level. + */ static final class ImmutableListOf1 { @BeforeTemplate List before(T item) { @@ -207,56 +214,72 @@ List before(T item) { } @AfterTemplate - ImmutableCollection after(T item) { + ImmutableList after(T item) { return ImmutableList.of(item); } } + /** + * Prefer {@link ImmutableList#of(Object, Object)} over alternatives that don't communicate the + * immutability of the resulting list at the type level. + */ static final class ImmutableListOf2 { @BeforeTemplate - List before(T item, T item2) { - return List.of(item, item2); + List before(T e1, T e2) { + return List.of(e1, e2); } @AfterTemplate - ImmutableCollection after(T item, T item2) { - return ImmutableList.of(item, item2); + ImmutableList after(T e1, T e2) { + return ImmutableList.of(e1, e2); } } + /** + * Prefer {@link ImmutableList#of(Object, Object, Object)} over alternatives that don't + * communicate the immutability of the resulting list at the type level. + */ static final class ImmutableListOf3 { @BeforeTemplate - List before(T i1, T i2, T i3) { - return List.of(i1, i2, i3); + List before(T e1, T e2, T e3) { + return List.of(e1, e2, e3); } @AfterTemplate - ImmutableCollection after(T i1, T i2, T i3) { - return ImmutableList.of(i1, i2, i3); + ImmutableList after(T e1, T e2, T e3) { + return ImmutableList.of(e1, e2, e3); } } + /** + * Prefer {@link ImmutableList#of(Object, Object, Object, Object)} over alternatives that don't + * communicate the immutability of the resulting list at the type level. + */ static final class ImmutableListOf4 { @BeforeTemplate - List before(T i1, T i2, T i3, T i4) { - return List.of(i1, i2, i3, i4); + List before(T e1, T e2, T e3, T e4) { + return List.of(e1, e2, e3, e4); } @AfterTemplate - ImmutableCollection after(T i1, T i2, T i3, T i4) { - return ImmutableList.of(i1, i2, i3, i4); + ImmutableList after(T e1, T e2, T e3, T e4) { + return ImmutableList.of(e1, e2, e3, e4); } } + /** + * Prefer {@link ImmutableList#of(Object, Object, Object, Object, Object)} over alternatives that + * don't communicate the immutability of the resulting list at the type level. + */ static final class ImmutableListOf5 { @BeforeTemplate - List before(T i1, T i2, T i3, T i4, T i5) { - return List.of(i1, i2, i3, i4, i5); + List before(T e1, T e2, T e3, T e4, T e5) { + return List.of(e1, e2, e3, e4, e5); } @AfterTemplate - ImmutableCollection after(T i1, T i2, T i3, T i4, T i5) { - return ImmutableList.of(i1, i2, i3, i4, i5); + ImmutableList after(T e1, T e2, T e3, T e4, T e5) { + return ImmutableList.of(e1, e2, e3, e4, e5); } } } diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java index acaa80f7ac..a830a51180 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java @@ -255,6 +255,10 @@ ImmutableMap after(ImmutableMap map) { } } + /** + * Prefer {@link ImmutableMap#of()} over alternatives that don't communicate the immutability of + * the resulting list at the type level. + */ static final class ImmutableMapOf { @BeforeTemplate Map before() { @@ -267,6 +271,10 @@ ImmutableMap after() { } } + /** + * Prefer {@link ImmutableMap#of(Object, Object)} over alternatives that don't communicate the + * immutability of the resulting list at the type level. + */ static final class ImmutableMapOf1 { @BeforeTemplate Map before(K k1, V v1) { @@ -279,6 +287,10 @@ ImmutableMap after(K k1, V v1) { } } + /** + * Prefer {@link ImmutableMap#of(Object, Object, Object, Object)} over alternatives that don't + * communicate the immutability of the resulting list at the type level. + */ static final class ImmutableMapOf2 { @BeforeTemplate Map before(K k1, V v1, K k2, V v2) { @@ -291,6 +303,10 @@ ImmutableMap after(K k1, V v1, K k2, V v2) { } } + /** + * Prefer {@link ImmutableMap#of(Object, Object, Object, Object, Object, Object)} over + * alternatives that don't communicate the immutability of the resulting list at the type level. + */ static final class ImmutableMapOf3 { @BeforeTemplate Map before(K k1, V v1, K k2, V v2, K k3, V v3) { @@ -303,6 +319,11 @@ ImmutableMap after(K k1, V v1, K k2, V v2, K k3, V v3) { } } + /** + * Prefer {@link ImmutableMap#of(Object, Object, Object, Object, Object, Object, Object, Object)} + * over alternatives that don't communicate the immutability of the resulting list at the type + * level. + */ static final class ImmutableMapOf4 { @BeforeTemplate Map before(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { @@ -315,6 +336,11 @@ ImmutableMap after(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { } } + /** + * Prefer {@link ImmutableMap#of(Object, Object, Object, Object, Object, Object, Object, Object, + * Object, Object)} over alternatives that don't communicate the immutability of the resulting + * list at the type level. + */ static final class ImmutableMapOf5 { @BeforeTemplate Map before(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) { @@ -326,6 +352,7 @@ ImmutableMap after(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V return ImmutableMap.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5); } } + // XXX: Add a template for this: // Maps.transformValues(streamOfEntries.collect(groupBy(fun)), ImmutableMap::copyOf) // -> diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java index 90756a5bbf..62ef9b06fc 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java @@ -139,6 +139,10 @@ ImmutableSet after(SetView set) { } } + /** + * Prefer {@link ImmutableSet#of()} over alternatives that don't communicate the immutability of + * the resulting list at the type level. + */ static final class ImmutableSetOf { @BeforeTemplate Set before() { @@ -151,6 +155,10 @@ ImmutableSet after() { } } + /** + * Prefer {@link ImmutableSet#of(Object)} over alternatives that don't communicate the + * immutability of the resulting list at the type level. + */ static final class ImmutableSetOfItems1 { @BeforeTemplate Set before(T e1) { @@ -163,6 +171,10 @@ ImmutableSet after(T e1) { } } + /** + * Prefer {@link ImmutableSet#of(Object, Object)} over alternatives that don't communicate the + * immutability of the resulting list at the type level. + */ static final class ImmutableSetOfItems2 { @BeforeTemplate Set before(T e1, T e2) { @@ -175,6 +187,10 @@ ImmutableSet after(T e1, T e2) { } } + /** + * Prefer {@link ImmutableSet#of(Object, Object, Object)} over alternatives that don't communicate + * the immutability of the resulting list at the type level. + */ static final class ImmutableSetOfItems3 { @BeforeTemplate Set before(T e1, T e2, T e3) { @@ -187,6 +203,10 @@ ImmutableSet after(T e1, T e2, T e3) { } } + /** + * Prefer {@link ImmutableSet#of(Object, Object, Object, Object)} over alternatives that don't + * communicate the immutability of the resulting list at the type level. + */ static final class ImmutableSetOfItems4 { @BeforeTemplate Set before(T e1, T e2, T e3, T e4) { @@ -199,6 +219,10 @@ ImmutableSet after(T e1, T e2, T e3, T e4) { } } + /** + * Prefer {@link ImmutableSet#of(Object, Object, Object, Object, Object)} over alternatives that + * don't communicate the immutability of the resulting list at the type level. + */ static final class ImmutableSetOfItems5 { @BeforeTemplate Set before(T e1, T e2, T e3, T e4, T e5) { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java index f4b56376db..498462a147 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java @@ -90,23 +90,23 @@ List testImmutableListOfTyped() { return Collections.emptyList(); } - Collection testImmutableListOf1() { + List testImmutableListOf1() { return List.of("1"); } - Collection testImmutableListOf2() { + List testImmutableListOf2() { return List.of("1", "2"); } - Collection testImmutableListOf3() { + List testImmutableListOf3() { return List.of("1", "2", "3"); } - Collection testImmutableListOf4() { + List testImmutableListOf4() { return List.of("1", "2", "3", "4"); } - Collection testImmutableListOf5() { + List testImmutableListOf5() { return List.of("1", "2", "3", "4", "5"); } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java index 18d9b55901..e037323148 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java @@ -85,23 +85,23 @@ List testImmutableListOfTyped() { return ImmutableList.of(); } - Collection testImmutableListOf1() { + List testImmutableListOf1() { return ImmutableList.of("1"); } - Collection testImmutableListOf2() { + List testImmutableListOf2() { return ImmutableList.of("1", "2"); } - Collection testImmutableListOf3() { + List testImmutableListOf3() { return ImmutableList.of("1", "2", "3"); } - Collection testImmutableListOf4() { + List testImmutableListOf4() { return ImmutableList.of("1", "2", "3", "4"); } - Collection testImmutableListOf5() { + List testImmutableListOf5() { return ImmutableList.of("1", "2", "3", "4", "5"); } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java index ed6921056c..402a255a69 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java @@ -91,13 +91,27 @@ Map testImmutableMapOfTyped() { return Collections.emptyMap(); } - ImmutableSet> testImmutableMapOfN() { - return ImmutableSet.of( - Collections.emptyMap(), - Map.of("k1", "v1"), - Map.of("k1", "v1", "k2", "v2"), - Map.of("k1", "v1", "k2", "v2", "k3", "v3"), - Map.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4"), - Map.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4", "k5", "v5")); + Map testImmutableMapOf() { + return Collections.emptyMap(); + } + + Map testImmutableMapOf1() { + return Map.of("k1", "v1"); + } + + Map testImmutableMapOf2() { + return Map.of("k1", "v1", "k2", "v2"); + } + + Map testImmutableMapOf3() { + return Map.of("k1", "v1", "k2", "v2", "k3", "v3"); + } + + Map testImmutableMapOf4() { + return Map.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4"); + } + + Map testImmutableMapOf5() { + return Map.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4", "k5", "v5"); } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java index 7dad01ab47..e12d28c973 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java @@ -77,13 +77,27 @@ Map testImmutableMapOfTyped() { return ImmutableMap.of(); } - ImmutableSet> testImmutableMapOfN() { - return ImmutableSet.of( - ImmutableMap.of(), - ImmutableMap.of("k1", "v1"), - ImmutableMap.of("k1", "v1", "k2", "v2"), - ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3"), - ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4"), - ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4", "k5", "v5")); + Map testImmutableMapOf() { + return ImmutableMap.of(); + } + + Map testImmutableMapOf1() { + return ImmutableMap.of("k1", "v1"); + } + + Map testImmutableMapOf2() { + return ImmutableMap.of("k1", "v1", "k2", "v2"); + } + + Map testImmutableMapOf3() { + return ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3"); + } + + Map testImmutableMapOf4() { + return ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4"); + } + + Map testImmutableMapOf5() { + return ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3", "k4", "v4", "k5", "v5"); } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java index 1097766df4..501da499e3 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java @@ -10,7 +10,6 @@ import com.google.common.collect.Sets; import com.google.common.collect.Streams; import java.util.Arrays; -import java.util.Collection; import java.util.Collections; import java.util.Set; import java.util.stream.Stream; @@ -72,23 +71,23 @@ Set testImmutableSetOfTyped() { return Collections.emptySet(); } - Collection testImmutableSetOfItems1() { + Set testImmutableSetOfItems1() { return Set.of("1"); } - Collection testImmutableSetOfItems2() { + Set testImmutableSetOfItems2() { return Set.of("1", "2"); } - Collection testImmutableSetOfItems3() { + Set testImmutableSetOfItems3() { return Set.of("1", "2", "3"); } - Collection testImmutableSetOfItems4() { + Set testImmutableSetOfItems4() { return Set.of("1", "2", "3", "4"); } - Collection testImmutableSetOfItems5() { + Set testImmutableSetOfItems5() { return Set.of("1", "2", "3", "4", "5"); } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java index 868e301423..7c5e96707c 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java @@ -10,7 +10,6 @@ import com.google.common.collect.Sets; import com.google.common.collect.Streams; import java.util.Arrays; -import java.util.Collection; import java.util.Collections; import java.util.Set; import java.util.stream.Stream; @@ -71,23 +70,23 @@ Set testImmutableSetOfTyped() { return ImmutableSet.of(); } - Collection testImmutableSetOfItems1() { + Set testImmutableSetOfItems1() { return ImmutableSet.of("1"); } - Collection testImmutableSetOfItems2() { + Set testImmutableSetOfItems2() { return ImmutableSet.of("1", "2"); } - Collection testImmutableSetOfItems3() { + Set testImmutableSetOfItems3() { return ImmutableSet.of("1", "2", "3"); } - Collection testImmutableSetOfItems4() { + Set testImmutableSetOfItems4() { return ImmutableSet.of("1", "2", "3", "4"); } - Collection testImmutableSetOfItems5() { + Set testImmutableSetOfItems5() { return ImmutableSet.of("1", "2", "3", "4", "5"); } } From eb4d03d4c233fdf3c888ffc1de4c50e521d69961 Mon Sep 17 00:00:00 2001 From: Vitor Mussatto Date: Thu, 24 Feb 2022 09:37:09 +0100 Subject: [PATCH 05/15] Making method names consistent --- .../bugpatterns/ImmutableSetTemplatesTestInput.java | 10 +++++----- .../bugpatterns/ImmutableSetTemplatesTestOutput.java | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java index 501da499e3..cfa8fea661 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java @@ -71,23 +71,23 @@ Set testImmutableSetOfTyped() { return Collections.emptySet(); } - Set testImmutableSetOfItems1() { + Set testImmutableSetOf1() { return Set.of("1"); } - Set testImmutableSetOfItems2() { + Set testImmutableSetOf2() { return Set.of("1", "2"); } - Set testImmutableSetOfItems3() { + Set testImmutableSetOf3() { return Set.of("1", "2", "3"); } - Set testImmutableSetOfItems4() { + Set testImmutableSetOf4() { return Set.of("1", "2", "3", "4"); } - Set testImmutableSetOfItems5() { + Set testImmutableSetOf5() { return Set.of("1", "2", "3", "4", "5"); } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java index 7c5e96707c..373e28f25f 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java @@ -70,23 +70,23 @@ Set testImmutableSetOfTyped() { return ImmutableSet.of(); } - Set testImmutableSetOfItems1() { + Set testImmutableSetOf1() { return ImmutableSet.of("1"); } - Set testImmutableSetOfItems2() { + Set testImmutableSetOf2() { return ImmutableSet.of("1", "2"); } - Set testImmutableSetOfItems3() { + Set testImmutableSetOf3() { return ImmutableSet.of("1", "2", "3"); } - Set testImmutableSetOfItems4() { + Set testImmutableSetOf4() { return ImmutableSet.of("1", "2", "3", "4"); } - Set testImmutableSetOfItems5() { + Set testImmutableSetOf5() { return ImmutableSet.of("1", "2", "3", "4", "5"); } } From 6630b6e0a47409ec0c96a8e0bb56934653fa322f Mon Sep 17 00:00:00 2001 From: Vitor Mussatto Date: Fri, 25 Feb 2022 09:13:41 +0100 Subject: [PATCH 06/15] Removing typed tests --- .../bugpatterns/ImmutableListTemplatesTestInput.java | 4 ---- .../bugpatterns/ImmutableListTemplatesTestOutput.java | 4 ---- .../bugpatterns/ImmutableMapTemplatesTestInput.java | 4 ---- .../bugpatterns/ImmutableMapTemplatesTestOutput.java | 4 ---- .../bugpatterns/ImmutableSetTemplatesTestInput.java | 4 ---- .../bugpatterns/ImmutableSetTemplatesTestOutput.java | 4 ---- 6 files changed, 24 deletions(-) diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java index 498462a147..d524fea9b7 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java @@ -86,10 +86,6 @@ List testImmutableListOf() { return Collections.emptyList(); } - List testImmutableListOfTyped() { - return Collections.emptyList(); - } - List testImmutableListOf1() { return List.of("1"); } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java index e037323148..98e9274ab3 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java @@ -81,10 +81,6 @@ List testImmutableListOf() { return ImmutableList.of(); } - List testImmutableListOfTyped() { - return ImmutableList.of(); - } - List testImmutableListOf1() { return ImmutableList.of("1"); } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java index 402a255a69..038806adea 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java @@ -87,10 +87,6 @@ ImmutableSet> testTransformMapValuesToImmutableMap k -> Math.toIntExact(ImmutableMap.of("bar", 2L).get(k)))); } - Map testImmutableMapOfTyped() { - return Collections.emptyMap(); - } - Map testImmutableMapOf() { return Collections.emptyMap(); } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java index e12d28c973..befd51d666 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java @@ -73,10 +73,6 @@ ImmutableSet> testTransformMapValuesToImmutableMap Maps.transformValues(ImmutableMap.of("bar", 2L), v -> Math.toIntExact(v)))); } - Map testImmutableMapOfTyped() { - return ImmutableMap.of(); - } - Map testImmutableMapOf() { return ImmutableMap.of(); } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java index cfa8fea661..acaf587405 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java @@ -67,10 +67,6 @@ Set testImmutableSetOf() { return Collections.emptySet(); } - Set testImmutableSetOfTyped() { - return Collections.emptySet(); - } - Set testImmutableSetOf1() { return Set.of("1"); } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java index 373e28f25f..cdf361b4e1 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java @@ -66,10 +66,6 @@ Set testImmutableSetOf() { return ImmutableSet.of(); } - Set testImmutableSetOfTyped() { - return ImmutableSet.of(); - } - Set testImmutableSetOf1() { return ImmutableSet.of("1"); } From 1876587a8ac94a13cf71791ae897361f5555a514 Mon Sep 17 00:00:00 2001 From: Vitor Mussatto Date: Fri, 25 Feb 2022 10:07:32 +0100 Subject: [PATCH 07/15] To object --- .../errorprone/bugpatterns/ImmutableListTemplatesTestInput.java | 2 +- .../bugpatterns/ImmutableListTemplatesTestOutput.java | 2 +- .../errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java | 2 +- .../errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java | 2 +- .../errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java | 2 +- .../errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java index d524fea9b7..9b021b4e25 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java @@ -82,7 +82,7 @@ ImmutableList testStreamToDistinctImmutableList() { return Stream.of(1).distinct().collect(toImmutableList()); } - List testImmutableListOf() { + List testImmutableListOf() { return Collections.emptyList(); } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java index 98e9274ab3..3069931ef2 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java @@ -77,7 +77,7 @@ ImmutableList testStreamToDistinctImmutableList() { return Stream.of(1).collect(toImmutableSet()).asList(); } - List testImmutableListOf() { + List testImmutableListOf() { return ImmutableList.of(); } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java index 038806adea..97c4fd3308 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java @@ -87,7 +87,7 @@ ImmutableSet> testTransformMapValuesToImmutableMap k -> Math.toIntExact(ImmutableMap.of("bar", 2L).get(k)))); } - Map testImmutableMapOf() { + Map testImmutableMapOf() { return Collections.emptyMap(); } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java index befd51d666..1ed37db857 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java @@ -73,7 +73,7 @@ ImmutableSet> testTransformMapValuesToImmutableMap Maps.transformValues(ImmutableMap.of("bar", 2L), v -> Math.toIntExact(v)))); } - Map testImmutableMapOf() { + Map testImmutableMapOf() { return ImmutableMap.of(); } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java index acaf587405..71b45c7f16 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java @@ -63,7 +63,7 @@ ImmutableSet testImmutableSetCopyOfSetView() { return ImmutableSet.copyOf(Sets.difference(ImmutableSet.of(1), ImmutableSet.of(2))); } - Set testImmutableSetOf() { + Set testImmutableSetOf() { return Collections.emptySet(); } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java index cdf361b4e1..eff0f0b85b 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java @@ -62,7 +62,7 @@ ImmutableSet testImmutableSetCopyOfSetView() { return Sets.difference(ImmutableSet.of(1), ImmutableSet.of(2)).immutableCopy(); } - Set testImmutableSetOf() { + Set testImmutableSetOf() { return ImmutableSet.of(); } From a92c05ec86af589933750954ff1e4647a19b6b6d Mon Sep 17 00:00:00 2001 From: Vitor Mussatto Date: Fri, 25 Feb 2022 15:11:30 +0100 Subject: [PATCH 08/15] CR remarks --- .../refastertemplates/ImmutableListTemplates.java | 8 ++++---- .../refastertemplates/ImmutableSetTemplates.java | 10 +++++----- .../bugpatterns/ImmutableListTemplatesTestInput.java | 2 -- .../bugpatterns/ImmutableListTemplatesTestOutput.java | 2 -- 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java index 6749af867b..8a4c69b8c6 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java @@ -209,13 +209,13 @@ ImmutableList after() { */ static final class ImmutableListOf1 { @BeforeTemplate - List before(T item) { - return List.of(item); + List before(T e1) { + return List.of(e1); } @AfterTemplate - ImmutableList after(T item) { - return ImmutableList.of(item); + ImmutableList after(T e1) { + return ImmutableList.of(e1); } } diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java index 62ef9b06fc..1468cefc8a 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java @@ -159,7 +159,7 @@ ImmutableSet after() { * Prefer {@link ImmutableSet#of(Object)} over alternatives that don't communicate the * immutability of the resulting list at the type level. */ - static final class ImmutableSetOfItems1 { + static final class ImmutableSetOf1 { @BeforeTemplate Set before(T e1) { return Set.of(e1); @@ -175,7 +175,7 @@ ImmutableSet after(T e1) { * Prefer {@link ImmutableSet#of(Object, Object)} over alternatives that don't communicate the * immutability of the resulting list at the type level. */ - static final class ImmutableSetOfItems2 { + static final class ImmutableSetOf2 { @BeforeTemplate Set before(T e1, T e2) { return Set.of(e1, e2); @@ -191,7 +191,7 @@ ImmutableSet after(T e1, T e2) { * Prefer {@link ImmutableSet#of(Object, Object, Object)} over alternatives that don't communicate * the immutability of the resulting list at the type level. */ - static final class ImmutableSetOfItems3 { + static final class ImmutableSetOf3 { @BeforeTemplate Set before(T e1, T e2, T e3) { return Set.of(e1, e2, e3); @@ -207,7 +207,7 @@ ImmutableSet after(T e1, T e2, T e3) { * Prefer {@link ImmutableSet#of(Object, Object, Object, Object)} over alternatives that don't * communicate the immutability of the resulting list at the type level. */ - static final class ImmutableSetOfItems4 { + static final class ImmutableSetOf4 { @BeforeTemplate Set before(T e1, T e2, T e3, T e4) { return Set.of(e1, e2, e3, e4); @@ -223,7 +223,7 @@ ImmutableSet after(T e1, T e2, T e3, T e4) { * Prefer {@link ImmutableSet#of(Object, Object, Object, Object, Object)} over alternatives that * don't communicate the immutability of the resulting list at the type level. */ - static final class ImmutableSetOfItems5 { + static final class ImmutableSetOf5 { @BeforeTemplate Set before(T e1, T e2, T e3, T e4, T e5) { return Set.of(e1, e2, e3, e4, e5); diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java index 9b021b4e25..d34aa86714 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java @@ -9,7 +9,6 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Streams; import java.util.Arrays; -import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -20,7 +19,6 @@ final class ImmutableListTemplatesTest implements RefasterTemplateTestCase { public ImmutableSet elidedTypesAndStaticImports() { return ImmutableSet.of( Arrays.class, - Collection.class, Collections.class, Comparator.class, Streams.class, diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java index 3069931ef2..0f5431134b 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java @@ -10,7 +10,6 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Streams; import java.util.Arrays; -import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -21,7 +20,6 @@ final class ImmutableListTemplatesTest implements RefasterTemplateTestCase { public ImmutableSet elidedTypesAndStaticImports() { return ImmutableSet.of( Arrays.class, - Collection.class, Collections.class, Comparator.class, Streams.class, From a58862f9e149b1e86a79489c6382da6e7e1f44b8 Mon Sep 17 00:00:00 2001 From: vmussatto Date: Mon, 28 Feb 2022 16:38:56 +0100 Subject: [PATCH 09/15] List.of changes --- .../errorprone/refastertemplates/ImmutableListTemplates.java | 2 +- .../errorprone/bugpatterns/ImmutableListTemplatesTestInput.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java index 8a4c69b8c6..9be5efe16c 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java @@ -194,7 +194,7 @@ ImmutableList after(Stream stream) { static final class ImmutableListOf { @BeforeTemplate List before() { - return Collections.emptyList(); + return List.of(); } @AfterTemplate diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java index d34aa86714..ee73577f8a 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java @@ -81,7 +81,7 @@ ImmutableList testStreamToDistinctImmutableList() { } List testImmutableListOf() { - return Collections.emptyList(); + return List.of(); } List testImmutableListOf1() { From 7aa801de3c803235f23d59fa2ac1959bab5387ff Mon Sep 17 00:00:00 2001 From: vmussatto Date: Tue, 1 Mar 2022 10:30:45 +0100 Subject: [PATCH 10/15] CR remarks --- .../errorprone/refastertemplates/ImmutableListTemplates.java | 4 ++-- .../errorprone/refastertemplates/ImmutableMapTemplates.java | 2 +- .../errorprone/refastertemplates/ImmutableSetTemplates.java | 2 +- .../bugpatterns/ImmutableMapTemplatesTestInput.java | 2 +- .../bugpatterns/ImmutableSetTemplatesTestInput.java | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java index 9be5efe16c..ab34781837 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java @@ -188,8 +188,8 @@ ImmutableList after(Stream stream) { } /** - * Prefer {@link ImmutableList#of(Object)} over alternatives that don't communicate the - * immutability of the resulting list at the type level. + * Prefer {@link ImmutableList#of()} over alternatives that don't communicate the immutability of + * the resulting list at the type level. */ static final class ImmutableListOf { @BeforeTemplate diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java index a830a51180..d26f47dcd2 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java @@ -262,7 +262,7 @@ ImmutableMap after(ImmutableMap map) { static final class ImmutableMapOf { @BeforeTemplate Map before() { - return Collections.emptyMap(); + return Map.of(); } @AfterTemplate diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java index 1468cefc8a..8ac5b25e7e 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java @@ -146,7 +146,7 @@ ImmutableSet after(SetView set) { static final class ImmutableSetOf { @BeforeTemplate Set before() { - return Collections.emptySet(); + return Set.of(); } @AfterTemplate diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java index 97c4fd3308..5f0f38bd06 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java @@ -88,7 +88,7 @@ ImmutableSet> testTransformMapValuesToImmutableMap } Map testImmutableMapOf() { - return Collections.emptyMap(); + return Map.of(); } Map testImmutableMapOf1() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java index 71b45c7f16..147d65a8c5 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java @@ -64,7 +64,7 @@ ImmutableSet testImmutableSetCopyOfSetView() { } Set testImmutableSetOf() { - return Collections.emptySet(); + return Set.of(); } Set testImmutableSetOf1() { From f4b02853c36fefb22c38be6284c75f7de7dd7275 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Wed, 2 Mar 2022 16:23:43 +0100 Subject: [PATCH 11/15] `Immutable{Set,List}Test{Input,Output}` use Integer instead of String --- .../ImmutableListTemplatesTestInput.java | 20 +++++++++---------- .../ImmutableListTemplatesTestOutput.java | 20 +++++++++---------- .../ImmutableSetTemplatesTestInput.java | 20 +++++++++---------- .../ImmutableSetTemplatesTestOutput.java | 20 +++++++++---------- 4 files changed, 40 insertions(+), 40 deletions(-) diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java index ee73577f8a..85f8128b6d 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java @@ -84,23 +84,23 @@ List testImmutableListOf() { return List.of(); } - List testImmutableListOf1() { - return List.of("1"); + List testImmutableListOf1() { + return List.of(1); } - List testImmutableListOf2() { - return List.of("1", "2"); + List testImmutableListOf2() { + return List.of(1, 2); } - List testImmutableListOf3() { - return List.of("1", "2", "3"); + List testImmutableListOf3() { + return List.of(1, 2, 3); } - List testImmutableListOf4() { - return List.of("1", "2", "3", "4"); + List testImmutableListOf4() { + return List.of(1, 2, 3, 4); } - List testImmutableListOf5() { - return List.of("1", "2", "3", "4", "5"); + List testImmutableListOf5() { + return List.of(1, 2, 3, 4, 5); } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java index 0f5431134b..7deec322ef 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java @@ -79,23 +79,23 @@ List testImmutableListOf() { return ImmutableList.of(); } - List testImmutableListOf1() { - return ImmutableList.of("1"); + List testImmutableListOf1() { + return ImmutableList.of(1); } - List testImmutableListOf2() { - return ImmutableList.of("1", "2"); + List testImmutableListOf2() { + return ImmutableList.of(1, 2); } - List testImmutableListOf3() { - return ImmutableList.of("1", "2", "3"); + List testImmutableListOf3() { + return ImmutableList.of(1, 2, 3); } - List testImmutableListOf4() { - return ImmutableList.of("1", "2", "3", "4"); + List testImmutableListOf4() { + return ImmutableList.of(1, 2, 3, 4); } - List testImmutableListOf5() { - return ImmutableList.of("1", "2", "3", "4", "5"); + List testImmutableListOf5() { + return ImmutableList.of(1, 2, 3, 4, 5); } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java index 147d65a8c5..aff766c679 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java @@ -67,23 +67,23 @@ Set testImmutableSetOf() { return Set.of(); } - Set testImmutableSetOf1() { - return Set.of("1"); + Set testImmutableSetOf1() { + return Set.of(1); } - Set testImmutableSetOf2() { - return Set.of("1", "2"); + Set testImmutableSetOf2() { + return Set.of(1, 2); } - Set testImmutableSetOf3() { - return Set.of("1", "2", "3"); + Set testImmutableSetOf3() { + return Set.of(1, 2, 3); } - Set testImmutableSetOf4() { - return Set.of("1", "2", "3", "4"); + Set testImmutableSetOf4() { + return Set.of(1, 2, 3, 4); } - Set testImmutableSetOf5() { - return Set.of("1", "2", "3", "4", "5"); + Set testImmutableSetOf5() { + return Set.of(1, 2, 3, 4, 5); } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java index eff0f0b85b..7fdcc7d42d 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java @@ -66,23 +66,23 @@ Set testImmutableSetOf() { return ImmutableSet.of(); } - Set testImmutableSetOf1() { - return ImmutableSet.of("1"); + Set testImmutableSetOf1() { + return ImmutableSet.of(1); } - Set testImmutableSetOf2() { - return ImmutableSet.of("1", "2"); + Set testImmutableSetOf2() { + return ImmutableSet.of(1, 2); } - Set testImmutableSetOf3() { - return ImmutableSet.of("1", "2", "3"); + Set testImmutableSetOf3() { + return ImmutableSet.of(1, 2, 3); } - Set testImmutableSetOf4() { - return ImmutableSet.of("1", "2", "3", "4"); + Set testImmutableSetOf4() { + return ImmutableSet.of(1, 2, 3, 4); } - Set testImmutableSetOf5() { - return ImmutableSet.of("1", "2", "3", "4", "5"); + Set testImmutableSetOf5() { + return ImmutableSet.of(1, 2, 3, 4, 5); } } From f8c20485f239738f51712d56c34fcd1c4d2ab21d Mon Sep 17 00:00:00 2001 From: vmussatto Date: Fri, 1 Apr 2022 11:40:15 +0200 Subject: [PATCH 12/15] Re-add emptyList, emptySet and emptyMap templates --- .../errorprone/refastertemplates/ImmutableListTemplates.java | 5 +++++ .../errorprone/refastertemplates/ImmutableMapTemplates.java | 5 +++++ .../errorprone/refastertemplates/ImmutableSetTemplates.java | 5 +++++ .../bugpatterns/ImmutableListTemplatesTestInput.java | 4 ++-- .../bugpatterns/ImmutableListTemplatesTestOutput.java | 4 ++-- .../bugpatterns/ImmutableMapTemplatesTestInput.java | 4 ++-- .../bugpatterns/ImmutableMapTemplatesTestOutput.java | 4 ++-- .../bugpatterns/ImmutableSetTemplatesTestInput.java | 4 ++-- .../bugpatterns/ImmutableSetTemplatesTestOutput.java | 4 ++-- 9 files changed, 27 insertions(+), 12 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java index ab34781837..a711d543ee 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java @@ -197,6 +197,11 @@ List before() { return List.of(); } + @BeforeTemplate + List before2() { + return Collections.emptyList(); + } + @AfterTemplate ImmutableList after() { return ImmutableList.of(); diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java index d26f47dcd2..b3cf771255 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java @@ -265,6 +265,11 @@ Map before() { return Map.of(); } + @BeforeTemplate + Map before2() { + return Collections.emptyMap(); + } + @AfterTemplate ImmutableMap after() { return ImmutableMap.of(); diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java index 8ac5b25e7e..7a43d614f1 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java @@ -149,6 +149,11 @@ Set before() { return Set.of(); } + @BeforeTemplate + Set before2() { + return Collections.emptySet(); + } + @AfterTemplate ImmutableSet after() { return ImmutableSet.of(); diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java index 85f8128b6d..26e08c8014 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java @@ -80,8 +80,8 @@ ImmutableList testStreamToDistinctImmutableList() { return Stream.of(1).distinct().collect(toImmutableList()); } - List testImmutableListOf() { - return List.of(); + ImmutableSet> testImmutableListOf() { + return ImmutableSet.of(List.of(), Collections.emptyList()); } List testImmutableListOf1() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java index 7deec322ef..d7b62dfb3e 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java @@ -75,8 +75,8 @@ ImmutableList testStreamToDistinctImmutableList() { return Stream.of(1).collect(toImmutableSet()).asList(); } - List testImmutableListOf() { - return ImmutableList.of(); + ImmutableSet> testImmutableListOf() { + return ImmutableSet.of(ImmutableList.of(), ImmutableList.of()); } List testImmutableListOf1() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java index 5f0f38bd06..83eae25f1b 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java @@ -87,8 +87,8 @@ ImmutableSet> testTransformMapValuesToImmutableMap k -> Math.toIntExact(ImmutableMap.of("bar", 2L).get(k)))); } - Map testImmutableMapOf() { - return Map.of(); + ImmutableSet> testImmutableMapOf() { + return ImmutableSet.of(Map.of(), Collections.emptyMap()); } Map testImmutableMapOf1() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java index 1ed37db857..416ce74bd3 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java @@ -73,8 +73,8 @@ ImmutableSet> testTransformMapValuesToImmutableMap Maps.transformValues(ImmutableMap.of("bar", 2L), v -> Math.toIntExact(v)))); } - Map testImmutableMapOf() { - return ImmutableMap.of(); + ImmutableSet> testImmutableMapOf() { + return ImmutableSet.of(ImmutableMap.of(), ImmutableMap.of()); } Map testImmutableMapOf1() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java index aff766c679..64f4cb7a95 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java @@ -63,8 +63,8 @@ ImmutableSet testImmutableSetCopyOfSetView() { return ImmutableSet.copyOf(Sets.difference(ImmutableSet.of(1), ImmutableSet.of(2))); } - Set testImmutableSetOf() { - return Set.of(); + ImmutableSet> testImmutableSetOf() { + return ImmutableSet.of(Set.of(), Collections.emptySet()); } Set testImmutableSetOf1() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java index 7fdcc7d42d..fad4b1bdcd 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java @@ -62,8 +62,8 @@ ImmutableSet testImmutableSetCopyOfSetView() { return Sets.difference(ImmutableSet.of(1), ImmutableSet.of(2)).immutableCopy(); } - Set testImmutableSetOf() { - return ImmutableSet.of(); + ImmutableSet> testImmutableSetOf() { + return ImmutableSet.of(ImmutableSet.of(), ImmutableSet.of()); } Set testImmutableSetOf1() { From 59ef9e92df27bc529f6638e14160d96951077ec6 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Mon, 4 Apr 2022 15:32:16 +0200 Subject: [PATCH 13/15] Suggestions --- .../errorprone/refastertemplates/ImmutableListTemplates.java | 4 ++-- .../errorprone/refastertemplates/ImmutableMapTemplates.java | 4 ++-- .../errorprone/refastertemplates/ImmutableSetTemplates.java | 4 ++-- .../bugpatterns/ImmutableListTemplatesTestInput.java | 2 +- .../bugpatterns/ImmutableMapTemplatesTestInput.java | 2 +- .../bugpatterns/ImmutableSetTemplatesTestInput.java | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java index a711d543ee..b5c39e0d02 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java @@ -194,12 +194,12 @@ ImmutableList after(Stream stream) { static final class ImmutableListOf { @BeforeTemplate List before() { - return List.of(); + return Collections.emptyList(); } @BeforeTemplate List before2() { - return Collections.emptyList(); + return List.of(); } @AfterTemplate diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java index b3cf771255..ed21d3fe99 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java @@ -262,12 +262,12 @@ ImmutableMap after(ImmutableMap map) { static final class ImmutableMapOf { @BeforeTemplate Map before() { - return Map.of(); + return Collections.emptyMap(); } @BeforeTemplate Map before2() { - return Collections.emptyMap(); + return Map.of(); } @AfterTemplate diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java index 7a43d614f1..a7184751b0 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java @@ -146,12 +146,12 @@ ImmutableSet after(SetView set) { static final class ImmutableSetOf { @BeforeTemplate Set before() { - return Set.of(); + return Collections.emptySet(); } @BeforeTemplate Set before2() { - return Collections.emptySet(); + return Set.of(); } @AfterTemplate diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java index 26e08c8014..bced6ae863 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java @@ -81,7 +81,7 @@ ImmutableList testStreamToDistinctImmutableList() { } ImmutableSet> testImmutableListOf() { - return ImmutableSet.of(List.of(), Collections.emptyList()); + return ImmutableSet.of(Collections.emptyList(), List.of()); } List testImmutableListOf1() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java index 83eae25f1b..6957713d9f 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java @@ -88,7 +88,7 @@ ImmutableSet> testTransformMapValuesToImmutableMap } ImmutableSet> testImmutableMapOf() { - return ImmutableSet.of(Map.of(), Collections.emptyMap()); + return ImmutableSet.of(Collections.emptyMap(), Map.of()); } Map testImmutableMapOf1() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java index 64f4cb7a95..d542a7d97f 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java @@ -64,7 +64,7 @@ ImmutableSet testImmutableSetCopyOfSetView() { } ImmutableSet> testImmutableSetOf() { - return ImmutableSet.of(Set.of(), Collections.emptySet()); + return ImmutableSet.of(Collections.emptySet(), Set.of()); } Set testImmutableSetOf1() { From dbae0c195446491fae68f93a1a59ed1e73d0895f Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sun, 10 Apr 2022 16:35:40 +0200 Subject: [PATCH 14/15] Post-rebase fix --- .../refastertemplates/ImmutableMapTemplates.java | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java index ed21d3fe99..4da16d1873 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java @@ -242,19 +242,6 @@ ImmutableMap after(Map map) { } } - /** Don't unnecessarily copy an {@link ImmutableMap}. */ - static final class ImmutableMapCopyOfImmutableMap { - @BeforeTemplate - ImmutableMap before(ImmutableMap map) { - return ImmutableMap.copyOf(map); - } - - @AfterTemplate - ImmutableMap after(ImmutableMap map) { - return map; - } - } - /** * Prefer {@link ImmutableMap#of()} over alternatives that don't communicate the immutability of * the resulting list at the type level. From 920a2e1ad492b6ea5dd6432a9eea54feb4527f3b Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sun, 10 Apr 2022 17:59:27 +0200 Subject: [PATCH 15/15] Suggestions --- .../ImmutableListTemplates.java | 65 ++++++----------- .../ImmutableMapTemplates.java | 71 ++++++------------ .../ImmutableSetTemplates.java | 72 +++++++------------ .../ImmutableListTemplatesTestInput.java | 22 +++--- .../ImmutableListTemplatesTestOutput.java | 17 ++--- .../ImmutableMapTemplatesTestInput.java | 24 +++---- .../ImmutableMapTemplatesTestOutput.java | 17 ++--- .../ImmutableSetTemplatesTestInput.java | 22 +++--- .../ImmutableSetTemplatesTestOutput.java | 17 ++--- 9 files changed, 111 insertions(+), 216 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java index b5c39e0d02..12e699400a 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableListTemplates.java @@ -41,37 +41,6 @@ ImmutableList.Builder after() { } } - /** Prefer {@link ImmutableList#of()} over more contrived alternatives. */ - static final class EmptyImmutableList { - @BeforeTemplate - ImmutableList before() { - return Refaster.anyOf( - ImmutableList.builder().build(), Stream.empty().collect(toImmutableList())); - } - - @AfterTemplate - ImmutableList after() { - return ImmutableList.of(); - } - } - - /** - * Prefer {@link ImmutableList#of(Object)} over alternatives that don't communicate the - * immutability of the resulting list at the type level. - */ - // XXX: Note that this rewrite rule is incorrect for nullable elements. - static final class SingletonImmutableList { - @BeforeTemplate - List before(T element) { - return Collections.singletonList(element); - } - - @AfterTemplate - ImmutableList after(T element) { - return ImmutableList.of(element); - } - } - /** * Prefer {@link ImmutableList#copyOf(Iterable)} and variants over more contrived alternatives. */ @@ -188,18 +157,19 @@ ImmutableList after(Stream stream) { } /** - * Prefer {@link ImmutableList#of()} over alternatives that don't communicate the immutability of - * the resulting list at the type level. + * Prefer {@link ImmutableList#of()} over more contrived alternatives or alternatives that don't + * communicate the immutability of the resulting list at the type level. */ + // XXX: The `Stream` variant may be too contrived to warrant inclusion. Review its usage if/when + // this and similar Refaster templates are replaced with an Error Prone check. static final class ImmutableListOf { @BeforeTemplate List before() { - return Collections.emptyList(); - } - - @BeforeTemplate - List before2() { - return List.of(); + return Refaster.anyOf( + ImmutableList.builder().build(), + Stream.empty().collect(toImmutableList()), + Collections.emptyList(), + List.of()); } @AfterTemplate @@ -209,13 +179,16 @@ ImmutableList after() { } /** - * Prefer {@link ImmutableList#of(Object)} over alternatives that don't communicate the - * immutability of the resulting list at the type level. + * Prefer {@link ImmutableList#of(Object)} over more contrived alternatives or alternatives that + * don't communicate the immutability of the resulting list at the type level. */ + // XXX: Note that the replacement of `Collections#singletonList` is incorrect for nullable + // elements. static final class ImmutableListOf1 { @BeforeTemplate List before(T e1) { - return List.of(e1); + return Refaster.anyOf( + ImmutableList.builder().add(e1).build(), Collections.singletonList(e1), List.of(e1)); } @AfterTemplate @@ -228,6 +201,8 @@ ImmutableList after(T e1) { * Prefer {@link ImmutableList#of(Object, Object)} over alternatives that don't communicate the * immutability of the resulting list at the type level. */ + // XXX: Consider writing an Error Prone check which also flags straightforward + // `ImmutableList.builder()` usages. static final class ImmutableListOf2 { @BeforeTemplate List before(T e1, T e2) { @@ -244,6 +219,8 @@ ImmutableList after(T e1, T e2) { * Prefer {@link ImmutableList#of(Object, Object, Object)} over alternatives that don't * communicate the immutability of the resulting list at the type level. */ + // XXX: Consider writing an Error Prone check which also flags straightforward + // `ImmutableList.builder()` usages. static final class ImmutableListOf3 { @BeforeTemplate List before(T e1, T e2, T e3) { @@ -260,6 +237,8 @@ ImmutableList after(T e1, T e2, T e3) { * Prefer {@link ImmutableList#of(Object, Object, Object, Object)} over alternatives that don't * communicate the immutability of the resulting list at the type level. */ + // XXX: Consider writing an Error Prone check which also flags straightforward + // `ImmutableList.builder()` usages. static final class ImmutableListOf4 { @BeforeTemplate List before(T e1, T e2, T e3, T e4) { @@ -276,6 +255,8 @@ ImmutableList after(T e1, T e2, T e3, T e4) { * Prefer {@link ImmutableList#of(Object, Object, Object, Object, Object)} over alternatives that * don't communicate the immutability of the resulting list at the type level. */ + // XXX: Consider writing an Error Prone check which also flags straightforward + // `ImmutableList.builder()` usages. static final class ImmutableListOf5 { @BeforeTemplate List before(T e1, T e2, T e3, T e4, T e5) { diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java index 4da16d1873..1ed11f0339 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java @@ -40,41 +40,6 @@ ImmutableMap.Builder after() { } } - /** Prefer {@link ImmutableMap#of()} over more contrived alternatives. */ - static final class EmptyImmutableMap { - @BeforeTemplate - ImmutableMap before() { - return ImmutableMap.builder().build(); - } - - @AfterTemplate - ImmutableMap after() { - return ImmutableMap.of(); - } - } - - /** - * Prefer {@link ImmutableMap#of(Object, Object)} over more contrived alternatives and - * alternatives that don't communicate the immutability of the resulting map at the type level. - */ - // XXX: One can define variants for more than one key-value pair, but at some point the builder - // actually produces nicer code. So it's not clear we should add Refaster templates for those - // variants. - // XXX: Note that the `singletonMap` rewrite rule is incorrect for nullable elements. - static final class PairToImmutableMap { - @BeforeTemplate - Map before(K key, V value) { - return Refaster.anyOf( - ImmutableMap.builder().put(key, value).build(), - Collections.singletonMap(key, value)); - } - - @AfterTemplate - ImmutableMap after(K key, V value) { - return ImmutableMap.of(key, value); - } - } - /** Prefer {@link ImmutableMap#of(Object, Object)} over more contrived alternatives. */ static final class EntryToImmutableMap { @BeforeTemplate @@ -243,18 +208,13 @@ ImmutableMap after(Map map) { } /** - * Prefer {@link ImmutableMap#of()} over alternatives that don't communicate the immutability of - * the resulting list at the type level. + * Prefer {@link ImmutableMap#of()} over more contrived alternatives or alternatives that don't + * communicate the immutability of the resulting map at the type level. */ static final class ImmutableMapOf { @BeforeTemplate Map before() { - return Collections.emptyMap(); - } - - @BeforeTemplate - Map before2() { - return Map.of(); + return Refaster.anyOf(ImmutableMap.builder().build(), Collections.emptyMap(), Map.of()); } @AfterTemplate @@ -264,13 +224,18 @@ ImmutableMap after() { } /** - * Prefer {@link ImmutableMap#of(Object, Object)} over alternatives that don't communicate the - * immutability of the resulting list at the type level. + * Prefer {@link ImmutableMap#of(Object, Object)} over more contrived alternatives or alternatives + * that don't communicate the immutability of the resulting map at the type level. */ + // XXX: Note that the replacement of `Collections#singletonMap` is incorrect for nullable + // elements. static final class ImmutableMapOf1 { @BeforeTemplate Map before(K k1, V v1) { - return Map.of(k1, v1); + return Refaster.anyOf( + ImmutableMap.builder().put(k1, v1).build(), + Collections.singletonMap(k1, v1), + Map.of(k1, v1)); } @AfterTemplate @@ -281,8 +246,9 @@ ImmutableMap after(K k1, V v1) { /** * Prefer {@link ImmutableMap#of(Object, Object, Object, Object)} over alternatives that don't - * communicate the immutability of the resulting list at the type level. + * communicate the immutability of the resulting map at the type level. */ + // XXX: Also rewrite the `ImmutableMap.builder()` variant? static final class ImmutableMapOf2 { @BeforeTemplate Map before(K k1, V v1, K k2, V v2) { @@ -297,8 +263,9 @@ ImmutableMap after(K k1, V v1, K k2, V v2) { /** * Prefer {@link ImmutableMap#of(Object, Object, Object, Object, Object, Object)} over - * alternatives that don't communicate the immutability of the resulting list at the type level. + * alternatives that don't communicate the immutability of the resulting map at the type level. */ + // XXX: Also rewrite the `ImmutableMap.builder()` variant? static final class ImmutableMapOf3 { @BeforeTemplate Map before(K k1, V v1, K k2, V v2, K k3, V v3) { @@ -313,9 +280,10 @@ ImmutableMap after(K k1, V v1, K k2, V v2, K k3, V v3) { /** * Prefer {@link ImmutableMap#of(Object, Object, Object, Object, Object, Object, Object, Object)} - * over alternatives that don't communicate the immutability of the resulting list at the type + * over alternatives that don't communicate the immutability of the resulting map at the type * level. */ + // XXX: Also rewrite the `ImmutableMap.builder()` variant? static final class ImmutableMapOf4 { @BeforeTemplate Map before(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { @@ -330,9 +298,10 @@ ImmutableMap after(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { /** * Prefer {@link ImmutableMap#of(Object, Object, Object, Object, Object, Object, Object, Object, - * Object, Object)} over alternatives that don't communicate the immutability of the resulting - * list at the type level. + * Object, Object)} over alternatives that don't communicate the immutability of the resulting map + * at the type level. */ + // XXX: Also rewrite the `ImmutableMap.builder()` variant? static final class ImmutableMapOf5 { @BeforeTemplate Map before(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) { diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java index a7184751b0..510f34f9f2 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java @@ -39,37 +39,6 @@ ImmutableSet.Builder after() { } } - /** Prefer {@link ImmutableSet#of()} over more contrived alternatives. */ - static final class EmptyImmutableSet { - @BeforeTemplate - ImmutableSet before() { - return Refaster.anyOf( - ImmutableSet.builder().build(), Stream.empty().collect(toImmutableSet())); - } - - @AfterTemplate - ImmutableSet after() { - return ImmutableSet.of(); - } - } - - /** - * Prefer {@link ImmutableSet#of(Object)} over alternatives that don't communicate the - * immutability of the resulting set at the type level. - */ - // XXX: Note that this rewrite rule is incorrect for nullable elements. - static final class SingletonImmutableSet { - @BeforeTemplate - Set before(T element) { - return Collections.singleton(element); - } - - @AfterTemplate - ImmutableSet after(T element) { - return ImmutableSet.of(element); - } - } - /** Prefer {@link ImmutableSet#copyOf(Iterable)} and variants over more contrived alternatives. */ static final class IterableToImmutableSet { @BeforeTemplate @@ -140,18 +109,19 @@ ImmutableSet after(SetView set) { } /** - * Prefer {@link ImmutableSet#of()} over alternatives that don't communicate the immutability of - * the resulting list at the type level. + * Prefer {@link ImmutableSet#of()} over more contrived alternatives or alternatives that don't + * communicate the immutability of the resulting set at the type level. */ + // XXX: The `Stream` variant may be too contrived to warrant inclusion. Review its usage if/when + // this and similar Refaster templates are replaced with an Error Prone check. static final class ImmutableSetOf { @BeforeTemplate Set before() { - return Collections.emptySet(); - } - - @BeforeTemplate - Set before2() { - return Set.of(); + return Refaster.anyOf( + ImmutableSet.builder().build(), + Stream.empty().collect(toImmutableSet()), + Collections.emptySet(), + Set.of()); } @AfterTemplate @@ -161,13 +131,15 @@ ImmutableSet after() { } /** - * Prefer {@link ImmutableSet#of(Object)} over alternatives that don't communicate the - * immutability of the resulting list at the type level. + * Prefer {@link ImmutableSet#of(Object)} over more contrived alternatives or alternatives that + * don't communicate the immutability of the resulting set at the type level. */ + // XXX: Note that the replacement of `Collections#singleton` is incorrect for nullable elements. static final class ImmutableSetOf1 { @BeforeTemplate Set before(T e1) { - return Set.of(e1); + return Refaster.anyOf( + ImmutableSet.builder().add(e1).build(), Collections.singleton(e1), Set.of(e1)); } @AfterTemplate @@ -178,8 +150,10 @@ ImmutableSet after(T e1) { /** * Prefer {@link ImmutableSet#of(Object, Object)} over alternatives that don't communicate the - * immutability of the resulting list at the type level. + * immutability of the resulting set at the type level. */ + // XXX: Consider writing an Error Prone check which also flags straightforward + // `ImmutableSet.builder()` usages. static final class ImmutableSetOf2 { @BeforeTemplate Set before(T e1, T e2) { @@ -194,8 +168,10 @@ ImmutableSet after(T e1, T e2) { /** * Prefer {@link ImmutableSet#of(Object, Object, Object)} over alternatives that don't communicate - * the immutability of the resulting list at the type level. + * the immutability of the resulting set at the type level. */ + // XXX: Consider writing an Error Prone check which also flags straightforward + // `ImmutableSet.builder()` usages. static final class ImmutableSetOf3 { @BeforeTemplate Set before(T e1, T e2, T e3) { @@ -210,8 +186,10 @@ ImmutableSet after(T e1, T e2, T e3) { /** * Prefer {@link ImmutableSet#of(Object, Object, Object, Object)} over alternatives that don't - * communicate the immutability of the resulting list at the type level. + * communicate the immutability of the resulting set at the type level. */ + // XXX: Consider writing an Error Prone check which also flags straightforward + // `ImmutableSet.builder()` usages. static final class ImmutableSetOf4 { @BeforeTemplate Set before(T e1, T e2, T e3, T e4) { @@ -226,8 +204,10 @@ ImmutableSet after(T e1, T e2, T e3, T e4) { /** * Prefer {@link ImmutableSet#of(Object, Object, Object, Object, Object)} over alternatives that - * don't communicate the immutability of the resulting list at the type level. + * don't communicate the immutability of the resulting set at the type level. */ + // XXX: Consider writing an Error Prone check which also flags straightforward + // `ImmutableSet.builder()` usages. static final class ImmutableSetOf5 { @BeforeTemplate Set before(T e1, T e2, T e3, T e4, T e5) { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java index bced6ae863..2034c5bfb7 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestInput.java @@ -31,16 +31,6 @@ ImmutableList.Builder testImmutableListBuilder() { return new ImmutableList.Builder<>(); } - ImmutableSet> testEmptyImmutableList() { - return ImmutableSet.of( - ImmutableList.builder().build(), - Stream.empty().collect(toImmutableList())); - } - - List testSingletonImmutableList() { - return Collections.singletonList("foo"); - } - ImmutableSet> testIterableToImmutableList() { return ImmutableSet.of( ImmutableList.of(1).stream().collect(toImmutableList()), @@ -80,12 +70,16 @@ ImmutableList testStreamToDistinctImmutableList() { return Stream.of(1).distinct().collect(toImmutableList()); } - ImmutableSet> testImmutableListOf() { - return ImmutableSet.of(Collections.emptyList(), List.of()); + ImmutableSet> testImmutableListOf() { + return ImmutableSet.of( + ImmutableList.builder().build(), + Stream.empty().collect(toImmutableList()), + Collections.emptyList(), + List.of()); } - List testImmutableListOf1() { - return List.of(1); + ImmutableSet> testImmutableListOf1() { + return ImmutableSet.of(Collections.singletonList(1), List.of(1)); } List testImmutableListOf2() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java index d7b62dfb3e..edab2e982c 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableListTemplatesTestOutput.java @@ -32,14 +32,6 @@ ImmutableList.Builder testImmutableListBuilder() { return ImmutableList.builder(); } - ImmutableSet> testEmptyImmutableList() { - return ImmutableSet.of(ImmutableList.of(), ImmutableList.of()); - } - - List testSingletonImmutableList() { - return ImmutableList.of("foo"); - } - ImmutableSet> testIterableToImmutableList() { return ImmutableSet.of( ImmutableList.copyOf(ImmutableList.of(1)), @@ -75,12 +67,13 @@ ImmutableList testStreamToDistinctImmutableList() { return Stream.of(1).collect(toImmutableSet()).asList(); } - ImmutableSet> testImmutableListOf() { - return ImmutableSet.of(ImmutableList.of(), ImmutableList.of()); + ImmutableSet> testImmutableListOf() { + return ImmutableSet.of( + ImmutableList.of(), ImmutableList.of(), ImmutableList.of(), ImmutableList.of()); } - List testImmutableListOf1() { - return ImmutableList.of(1); + ImmutableSet> testImmutableListOf1() { + return ImmutableSet.of(ImmutableList.of(1), ImmutableList.of(1)); } List testImmutableListOf2() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java index 6957713d9f..5678b0d9e2 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestInput.java @@ -23,16 +23,6 @@ ImmutableMap.Builder testImmutableMapBuilder() { return new ImmutableMap.Builder<>(); } - ImmutableMap testEmptyImmutableMap() { - return ImmutableMap.builder().build(); - } - - ImmutableSet> testPairToImmutableMap() { - return ImmutableSet.of( - ImmutableMap.builder().put("foo", 1).build(), - Collections.singletonMap("bar", 2)); - } - ImmutableSet> testEntryToImmutableMap() { return ImmutableSet.of( ImmutableMap.builder().put(Map.entry("foo", 1)).build(), @@ -87,12 +77,18 @@ ImmutableSet> testTransformMapValuesToImmutableMap k -> Math.toIntExact(ImmutableMap.of("bar", 2L).get(k)))); } - ImmutableSet> testImmutableMapOf() { - return ImmutableSet.of(Collections.emptyMap(), Map.of()); + ImmutableSet> testImmutableMapOf() { + return ImmutableSet.of( + ImmutableMap.builder().build(), + Collections.emptyMap(), + Map.of()); } - Map testImmutableMapOf1() { - return Map.of("k1", "v1"); + ImmutableSet> testImmutableMapOf1() { + return ImmutableSet.of( + ImmutableMap.builder().put("k1", "v1").build(), + Collections.singletonMap("k1", "v1"), + Map.of("k1", "v1")); } Map testImmutableMapOf2() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java index 416ce74bd3..37f84c3da0 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableMapTemplatesTestOutput.java @@ -23,14 +23,6 @@ ImmutableMap.Builder testImmutableMapBuilder() { return ImmutableMap.builder(); } - ImmutableMap testEmptyImmutableMap() { - return ImmutableMap.of(); - } - - ImmutableSet> testPairToImmutableMap() { - return ImmutableSet.of(ImmutableMap.of("foo", 1), ImmutableMap.of("bar", 2)); - } - ImmutableSet> testEntryToImmutableMap() { return ImmutableSet.of( ImmutableMap.of(Map.entry("foo", 1).getKey(), Map.entry("foo", 1).getValue()), @@ -73,12 +65,13 @@ ImmutableSet> testTransformMapValuesToImmutableMap Maps.transformValues(ImmutableMap.of("bar", 2L), v -> Math.toIntExact(v)))); } - ImmutableSet> testImmutableMapOf() { - return ImmutableSet.of(ImmutableMap.of(), ImmutableMap.of()); + ImmutableSet> testImmutableMapOf() { + return ImmutableSet.of(ImmutableMap.of(), ImmutableMap.of(), ImmutableMap.of()); } - Map testImmutableMapOf1() { - return ImmutableMap.of("k1", "v1"); + ImmutableSet> testImmutableMapOf1() { + return ImmutableSet.of( + ImmutableMap.of("k1", "v1"), ImmutableMap.of("k1", "v1"), ImmutableMap.of("k1", "v1")); } Map testImmutableMapOf2() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java index d542a7d97f..65dcdfa9f1 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestInput.java @@ -30,15 +30,6 @@ ImmutableSet.Builder testImmutableSetBuilder() { return new ImmutableSet.Builder<>(); } - ImmutableSet> testEmptyImmutableSet() { - return ImmutableSet.of( - ImmutableSet.builder().build(), Stream.empty().collect(toImmutableSet())); - } - - Set testSingletonImmutableSet() { - return Collections.singleton("foo"); - } - ImmutableSet> testIterableToImmutableSet() { return ImmutableSet.of( ImmutableList.of(1).stream().collect(toImmutableSet()), @@ -63,12 +54,17 @@ ImmutableSet testImmutableSetCopyOfSetView() { return ImmutableSet.copyOf(Sets.difference(ImmutableSet.of(1), ImmutableSet.of(2))); } - ImmutableSet> testImmutableSetOf() { - return ImmutableSet.of(Collections.emptySet(), Set.of()); + ImmutableSet> testImmutableSetOf() { + return ImmutableSet.of( + ImmutableSet.builder().build(), + Stream.empty().collect(toImmutableSet()), + Collections.emptySet(), + Set.of()); } - Set testImmutableSetOf1() { - return Set.of(1); + ImmutableSet> testImmutableSetOf1() { + return ImmutableSet.of( + ImmutableSet.builder().add(1).build(), Collections.singleton(1), Set.of(1)); } Set testImmutableSetOf2() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java index fad4b1bdcd..421e6c29ba 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/ImmutableSetTemplatesTestOutput.java @@ -30,14 +30,6 @@ ImmutableSet.Builder testImmutableSetBuilder() { return ImmutableSet.builder(); } - ImmutableSet> testEmptyImmutableSet() { - return ImmutableSet.of(ImmutableSet.of(), ImmutableSet.of()); - } - - Set testSingletonImmutableSet() { - return ImmutableSet.of("foo"); - } - ImmutableSet> testIterableToImmutableSet() { return ImmutableSet.of( ImmutableSet.copyOf(ImmutableList.of(1)), @@ -62,12 +54,13 @@ ImmutableSet testImmutableSetCopyOfSetView() { return Sets.difference(ImmutableSet.of(1), ImmutableSet.of(2)).immutableCopy(); } - ImmutableSet> testImmutableSetOf() { - return ImmutableSet.of(ImmutableSet.of(), ImmutableSet.of()); + ImmutableSet> testImmutableSetOf() { + return ImmutableSet.of( + ImmutableSet.of(), ImmutableSet.of(), ImmutableSet.of(), ImmutableSet.of()); } - Set testImmutableSetOf1() { - return ImmutableSet.of(1); + ImmutableSet> testImmutableSetOf1() { + return ImmutableSet.of(ImmutableSet.of(1), ImmutableSet.of(1), ImmutableSet.of(1)); } Set testImmutableSetOf2() {