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..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. */ @@ -186,4 +155,117 @@ ImmutableList after(Stream stream) { return stream.collect(toImmutableSet()).asList(); } } + + /** + * 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 Refaster.anyOf( + ImmutableList.builder().build(), + Stream.empty().collect(toImmutableList()), + Collections.emptyList(), + List.of()); + } + + @AfterTemplate + ImmutableList after() { + return ImmutableList.of(); + } + } + + /** + * 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 Refaster.anyOf( + ImmutableList.builder().add(e1).build(), Collections.singletonList(e1), List.of(e1)); + } + + @AfterTemplate + ImmutableList after(T e1) { + return ImmutableList.of(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) { + return List.of(e1, e2); + } + + @AfterTemplate + 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. + */ + // 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) { + return List.of(e1, e2, e3); + } + + @AfterTemplate + 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. + */ + // 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) { + return List.of(e1, e2, e3, e4); + } + + @AfterTemplate + 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. + */ + // 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) { + return List.of(e1, e2, e3, e4, e5); + } + + @AfterTemplate + 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 5c8aa28142..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 @@ -242,6 +207,113 @@ ImmutableMap after(Map map) { } } + /** + * 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 Refaster.anyOf(ImmutableMap.builder().build(), Collections.emptyMap(), Map.of()); + } + + @AfterTemplate + ImmutableMap after() { + return ImmutableMap.of(); + } + } + + /** + * 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 Refaster.anyOf( + ImmutableMap.builder().put(k1, v1).build(), + Collections.singletonMap(k1, v1), + Map.of(k1, v1)); + } + + @AfterTemplate + ImmutableMap after(K k1, V v1) { + return ImmutableMap.of(k1, v1); + } + } + + /** + * Prefer {@link ImmutableMap#of(Object, Object, 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 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); + } + } + + /** + * Prefer {@link ImmutableMap#of(Object, Object, Object, Object, 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 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); + } + } + + /** + * Prefer {@link ImmutableMap#of(Object, Object, Object, Object, Object, Object, 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 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); + } + } + + /** + * 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 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) { + 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 7e19377eaa..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 @@ -138,4 +107,116 @@ ImmutableSet after(SetView set) { return set.immutableCopy(); } } + + /** + * 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 Refaster.anyOf( + ImmutableSet.builder().build(), + Stream.empty().collect(toImmutableSet()), + Collections.emptySet(), + Set.of()); + } + + @AfterTemplate + ImmutableSet after() { + return ImmutableSet.of(); + } + } + + /** + * 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 Refaster.anyOf( + ImmutableSet.builder().add(e1).build(), Collections.singleton(e1), Set.of(e1)); + } + + @AfterTemplate + ImmutableSet after(T e1) { + return ImmutableSet.of(e1); + } + } + + /** + * Prefer {@link ImmutableSet#of(Object, Object)} over alternatives that 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 ImmutableSetOf2 { + @BeforeTemplate + Set before(T e1, T e2) { + return Set.of(e1, e2); + } + + @AfterTemplate + ImmutableSet after(T e1, T e2) { + return ImmutableSet.of(e1, e2); + } + } + + /** + * Prefer {@link ImmutableSet#of(Object, Object, Object)} over alternatives that 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 ImmutableSetOf3 { + @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); + } + } + + /** + * Prefer {@link ImmutableSet#of(Object, Object, Object, Object)} over alternatives that 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 ImmutableSetOf4 { + @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); + } + } + + /** + * Prefer {@link ImmutableSet#of(Object, Object, Object, Object, Object)} over alternatives that + * 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) { + 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 f2d564e343..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()), @@ -79,4 +69,32 @@ ImmutableSet> testImmutableListSortedCopyOfWithCustomCompa ImmutableList testStreamToDistinctImmutableList() { return Stream.of(1).distinct().collect(toImmutableList()); } + + ImmutableSet> testImmutableListOf() { + return ImmutableSet.of( + ImmutableList.builder().build(), + Stream.empty().collect(toImmutableList()), + Collections.emptyList(), + List.of()); + } + + ImmutableSet> testImmutableListOf1() { + return ImmutableSet.of(Collections.singletonList(1), List.of(1)); + } + + List testImmutableListOf2() { + return List.of(1, 2); + } + + List testImmutableListOf3() { + return List.of(1, 2, 3); + } + + List testImmutableListOf4() { + return List.of(1, 2, 3, 4); + } + + 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 f13829aad0..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)), @@ -74,4 +66,29 @@ ImmutableSet> testImmutableListSortedCopyOfWithCustomCompa ImmutableList testStreamToDistinctImmutableList() { return Stream.of(1).collect(toImmutableSet()).asList(); } + + ImmutableSet> testImmutableListOf() { + return ImmutableSet.of( + ImmutableList.of(), ImmutableList.of(), ImmutableList.of(), ImmutableList.of()); + } + + ImmutableSet> testImmutableListOf1() { + return ImmutableSet.of(ImmutableList.of(1), ImmutableList.of(1)); + } + + List testImmutableListOf2() { + return ImmutableList.of(1, 2); + } + + List testImmutableListOf3() { + return ImmutableList.of(1, 2, 3); + } + + List testImmutableListOf4() { + return ImmutableList.of(1, 2, 3, 4); + } + + 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 d119cd9763..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(), @@ -86,4 +76,34 @@ ImmutableSet> testTransformMapValuesToImmutableMap ImmutableMap.of("bar", 2L).keySet(), k -> Math.toIntExact(ImmutableMap.of("bar", 2L).get(k)))); } + + ImmutableSet> testImmutableMapOf() { + return ImmutableSet.of( + ImmutableMap.builder().build(), + Collections.emptyMap(), + Map.of()); + } + + ImmutableSet> testImmutableMapOf1() { + return ImmutableSet.of( + ImmutableMap.builder().put("k1", "v1").build(), + Collections.singletonMap("k1", "v1"), + 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 e1638de70e..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()), @@ -72,4 +64,29 @@ ImmutableSet> testTransformMapValuesToImmutableMap ImmutableMap.copyOf( Maps.transformValues(ImmutableMap.of("bar", 2L), v -> Math.toIntExact(v)))); } + + ImmutableSet> testImmutableMapOf() { + return ImmutableSet.of(ImmutableMap.of(), ImmutableMap.of(), ImmutableMap.of()); + } + + ImmutableSet> testImmutableMapOf1() { + return ImmutableSet.of( + ImmutableMap.of("k1", "v1"), ImmutableMap.of("k1", "v1"), 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 4fd3650179..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()), @@ -62,4 +53,33 @@ ImmutableSet> testStreamToImmutableSet() { ImmutableSet testImmutableSetCopyOfSetView() { return ImmutableSet.copyOf(Sets.difference(ImmutableSet.of(1), ImmutableSet.of(2))); } + + ImmutableSet> testImmutableSetOf() { + return ImmutableSet.of( + ImmutableSet.builder().build(), + Stream.empty().collect(toImmutableSet()), + Collections.emptySet(), + Set.of()); + } + + ImmutableSet> testImmutableSetOf1() { + return ImmutableSet.of( + ImmutableSet.builder().add(1).build(), Collections.singleton(1), Set.of(1)); + } + + Set testImmutableSetOf2() { + return Set.of(1, 2); + } + + Set testImmutableSetOf3() { + return Set.of(1, 2, 3); + } + + Set testImmutableSetOf4() { + return Set.of(1, 2, 3, 4); + } + + 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 b23aac8871..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)), @@ -61,4 +53,29 @@ ImmutableSet> testStreamToImmutableSet() { ImmutableSet testImmutableSetCopyOfSetView() { return Sets.difference(ImmutableSet.of(1), ImmutableSet.of(2)).immutableCopy(); } + + ImmutableSet> testImmutableSetOf() { + return ImmutableSet.of( + ImmutableSet.of(), ImmutableSet.of(), ImmutableSet.of(), ImmutableSet.of()); + } + + ImmutableSet> testImmutableSetOf1() { + return ImmutableSet.of(ImmutableSet.of(1), ImmutableSet.of(1), ImmutableSet.of(1)); + } + + Set testImmutableSetOf2() { + return ImmutableSet.of(1, 2); + } + + Set testImmutableSetOf3() { + return ImmutableSet.of(1, 2, 3); + } + + Set testImmutableSetOf4() { + return ImmutableSet.of(1, 2, 3, 4); + } + + Set testImmutableSetOf5() { + return ImmutableSet.of(1, 2, 3, 4, 5); + } }