From 068f00d4532258764222469500d6ad94538c7ee8 Mon Sep 17 00:00:00 2001 From: mohamedsamehsalah Date: Sat, 10 Aug 2024 15:01:53 +0200 Subject: [PATCH 1/3] Introduce `ImmutableEnumSetRules` refaster rules --- ...ocumentationGeneratorTaskListenerTest.java | 4 +- .../bugpatterns/JUnitClassModifiers.java | 4 +- .../refasterrules/ImmutableEnumSetRules.java | 177 ++++++++++++++++++ .../refasterrules/RefasterRulesTest.java | 1 + .../ImmutableEnumSetRulesTestInput.java | 89 +++++++++ .../ImmutableEnumSetRulesTestOutput.java | 88 +++++++++ 6 files changed, 359 insertions(+), 4 deletions(-) create mode 100644 error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRules.java create mode 100644 error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRulesTestInput.java create mode 100644 error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRulesTestOutput.java diff --git a/documentation-support/src/test/java/tech/picnic/errorprone/documentation/DocumentationGeneratorTaskListenerTest.java b/documentation-support/src/test/java/tech/picnic/errorprone/documentation/DocumentationGeneratorTaskListenerTest.java index 685c7cd6f2..4e9d175a47 100644 --- a/documentation-support/src/test/java/tech/picnic/errorprone/documentation/DocumentationGeneratorTaskListenerTest.java +++ b/documentation-support/src/test/java/tech/picnic/errorprone/documentation/DocumentationGeneratorTaskListenerTest.java @@ -10,7 +10,6 @@ import com.google.auto.service.AutoService; import com.google.auto.value.AutoValue; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; import com.google.common.collect.Streams; import com.google.errorprone.VisitorState; @@ -41,7 +40,8 @@ void readOnlyFileSystemWindows(@TempDir Path outputDirectory) throws IOException entry -> AclEntry.newBuilder(entry) .setPermissions( - Sets.difference(entry.permissions(), ImmutableSet.of(ADD_SUBDIRECTORY))) + Sets.difference( + entry.permissions(), Sets.immutableEnumSet(ADD_SUBDIRECTORY))) .build()) .collect(toImmutableList())); diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/JUnitClassModifiers.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/JUnitClassModifiers.java index 8cfd59cd2a..372adebb71 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/JUnitClassModifiers.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/JUnitClassModifiers.java @@ -16,7 +16,7 @@ import static tech.picnic.errorprone.utils.MoreMatchers.hasMetaAnnotation; import com.google.auto.service.AutoService; -import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; import com.google.errorprone.BugPattern; import com.google.errorprone.VisitorState; import com.google.errorprone.bugpatterns.BugChecker; @@ -70,7 +70,7 @@ public Description matchClass(ClassTree tree, VisitorState state) { SuggestedFixes.removeModifiers( tree.getModifiers(), state, - ImmutableSet.of(Modifier.PRIVATE, Modifier.PROTECTED, Modifier.PUBLIC)) + Sets.immutableEnumSet(Modifier.PRIVATE, Modifier.PROTECTED, Modifier.PUBLIC)) .ifPresent(fixBuilder::merge); if (!HAS_SPRING_CONFIGURATION_ANNOTATION.matches(tree, state)) { diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRules.java new file mode 100644 index 0000000000..b513725c2b --- /dev/null +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRules.java @@ -0,0 +1,177 @@ +package tech.picnic.errorprone.refasterrules; + +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; +import com.google.errorprone.refaster.Refaster; +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import com.google.errorprone.refaster.annotation.Repeated; +import java.util.Arrays; +import java.util.Collection; +import java.util.EnumSet; +import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; + +/** Refaster rules related to expressions dealing with immutable {@link Enum} collections. */ +@OnlineDocumentation +final class ImmutableEnumSetRules { + private ImmutableEnumSetRules() {} + + /** + * Prefer {@link Sets#immutableEnumSet(Iterable)} for enum collections to take advantage of + * internally used {@link EnumSet}. + */ + static final class ImmutableEnumSetIterable> { + @BeforeTemplate + ImmutableSet before(Collection elements) { + return ImmutableSet.copyOf(elements); + } + + @AfterTemplate + ImmutableSet after(Collection elements) { + return Sets.immutableEnumSet(elements); + } + } + + /** + * Prefer {@link Sets#immutableEnumSet(Iterable)} for enum collections to take advantage of + * internally used {@link EnumSet}. + */ + static final class ImmutableEnumSetIterable1> { + @BeforeTemplate + ImmutableSet before(T[] elements) { + return ImmutableSet.copyOf(elements); + } + + @AfterTemplate + ImmutableSet after(T[] elements) { + return Sets.immutableEnumSet(Arrays.asList(elements)); + } + } + + /** + * Prefer {@link Sets#immutableEnumSet(Enum, Enum[])} for enum collections to take advantage of + * internally used {@link EnumSet}. + */ + static final class ImmutableEnumSetOneElement> { + @BeforeTemplate + @SuppressWarnings("ImmutableEnumSetIterable" /* This is a more specific template. */) + ImmutableSet before(T e1) { + return Refaster.anyOf(ImmutableSet.of(e1), ImmutableSet.copyOf(EnumSet.of(e1))); + } + + @AfterTemplate + @SuppressWarnings("unchecked") + ImmutableSet after(T e1) { + return Sets.immutableEnumSet(e1); + } + } + + /** + * Prefer {@link Sets#immutableEnumSet(Enum, Enum[])} for enum collections to take advantage of + * internally used {@link EnumSet}. + */ + static final class ImmutableEnumSetTwoElements> { + @BeforeTemplate + @SuppressWarnings("ImmutableEnumSetIterable" /* This is a more specific template. */) + ImmutableSet before(T e1, T e2) { + return Refaster.anyOf(ImmutableSet.of(e1, e2), ImmutableSet.copyOf(EnumSet.of(e1, e2))); + } + + @AfterTemplate + @SuppressWarnings("unchecked") + ImmutableSet after(T e1, T e2) { + return Sets.immutableEnumSet(e1, e2); + } + } + + /** + * Prefer {@link Sets#immutableEnumSet(Enum, Enum[])} for enum collections to take advantage of + * internally used {@link EnumSet}. + */ + static final class ImmutableEnumSetThreeElements> { + @BeforeTemplate + @SuppressWarnings("ImmutableEnumSetIterable" /* This is a more specific template. */) + ImmutableSet before(T e1, T e2, T e3) { + return Refaster.anyOf( + ImmutableSet.of(e1, e2, e3), ImmutableSet.copyOf(EnumSet.of(e1, e2, e3))); + } + + @AfterTemplate + @SuppressWarnings("unchecked") + ImmutableSet after(T e1, T e2, T e3) { + return Sets.immutableEnumSet(e1, e2, e3); + } + } + + /** + * Prefer {@link Sets#immutableEnumSet(Enum, Enum[])} for enum collections to take advantage of + * internally used {@link EnumSet}. + */ + static final class ImmutableEnumSetFourElements> { + @BeforeTemplate + @SuppressWarnings("ImmutableEnumSetIterable" /* This is a more specific template. */) + ImmutableSet before(T e1, T e2, T e3, T e4) { + return Refaster.anyOf( + ImmutableSet.of(e1, e2, e3, e4), ImmutableSet.copyOf(EnumSet.of(e1, e2, e3, e4))); + } + + @AfterTemplate + @SuppressWarnings("unchecked") + ImmutableSet after(T e1, T e2, T e3, T e4) { + return Sets.immutableEnumSet(e1, e2, e3, e4); + } + } + + /** + * Prefer {@link Sets#immutableEnumSet(Enum, Enum[])} for enum collections to take advantage of + * internally used {@link EnumSet}. + */ + static final class ImmutableEnumSetFiveElements> { + @BeforeTemplate + @SuppressWarnings("ImmutableEnumSetIterable" /* This is a more specific template. */) + ImmutableSet before(T e1, T e2, T e3, T e4, T e5) { + return Refaster.anyOf( + ImmutableSet.of(e1, e2, e3, e4, e5), ImmutableSet.copyOf(EnumSet.of(e1, e2, e3, e4, e5))); + } + + @AfterTemplate + @SuppressWarnings("unchecked") + ImmutableSet after(T e1, T e2, T e3, T e4, T e5) { + return Sets.immutableEnumSet(e1, e2, e3, e4, e5); + } + } + + /** + * Prefer {@link Sets#immutableEnumSet(Enum, Enum[])} for enum collections to take advantage of + * internally used {@link EnumSet}. + */ + static final class ImmutableEnumSetSixElements> { + @BeforeTemplate + ImmutableSet before(T e1, T e2, T e3, T e4, T e5, T e6) { + return ImmutableSet.of(e1, e2, e3, e4, e5, e6); + } + + @AfterTemplate + @SuppressWarnings("unchecked") + ImmutableSet after(T e1, T e2, T e3, T e4, T e5, T e6) { + return Sets.immutableEnumSet(e1, e2, e3, e4, e5, e6); + } + } + + /** + * Prefer {@link Sets#immutableEnumSet(Enum, Enum[])} for enum collections to take advantage of + * internally used {@link EnumSet}. + */ + static final class ImmutableEnumSetVarArgs> { + @BeforeTemplate + @SuppressWarnings("ImmutableEnumSetIterable" /* This is a more specific template. */) + ImmutableSet before(T e1, @Repeated T elements) { + return ImmutableSet.copyOf(EnumSet.of(e1, Refaster.asVarargs(elements))); + } + + @AfterTemplate + ImmutableSet after(T e1, @Repeated T elements) { + return Sets.immutableEnumSet(e1, Refaster.asVarargs(elements)); + } + } +} diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java index a7c35621a8..30839af965 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java @@ -43,6 +43,7 @@ final class RefasterRulesTest { EqualityRules.class, FileRules.class, InputStreamRules.class, + ImmutableEnumSetRules.class, ImmutableListRules.class, ImmutableListMultimapRules.class, ImmutableMapRules.class, diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRulesTestInput.java new file mode 100644 index 0000000000..c693d024ae --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRulesTestInput.java @@ -0,0 +1,89 @@ +package tech.picnic.errorprone.refasterrules; + +import com.google.common.collect.ImmutableSet; +import java.math.RoundingMode; +import java.util.EnumSet; +import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; + +final class ImmutableEnumSetRulesTest implements RefasterRuleCollectionTestCase { + @Override + public ImmutableSet elidedTypesAndStaticImports() { + return ImmutableSet.of(EnumSet.class); + } + + ImmutableSet> testImmutableEnumSetIterable() { + return ImmutableSet.of( + ImmutableSet.copyOf(EnumSet.range(RoundingMode.UP, RoundingMode.UNNECESSARY)), + ImmutableSet.copyOf(EnumSet.allOf(RoundingMode.class))); + } + + ImmutableSet> testImmutableEnumSetIterable1() { + return ImmutableSet.of( + ImmutableSet.copyOf(RoundingMode.values()), + ImmutableSet.copyOf(RoundingMode.class.getEnumConstants())); + } + + ImmutableSet> testImmutableEnumSetOneElement() { + return ImmutableSet.of( + ImmutableSet.of(RoundingMode.UP), ImmutableSet.copyOf(EnumSet.of(RoundingMode.UP))); + } + + ImmutableSet> testImmutableEnumSetTwoElements() { + return ImmutableSet.of( + ImmutableSet.of(RoundingMode.UP, RoundingMode.DOWN), + ImmutableSet.copyOf(EnumSet.of(RoundingMode.UP, RoundingMode.DOWN))); + } + + ImmutableSet> testImmutableEnumSetThreeElements() { + return ImmutableSet.of( + ImmutableSet.of(RoundingMode.UP, RoundingMode.DOWN, RoundingMode.CEILING), + ImmutableSet.copyOf(EnumSet.of(RoundingMode.UP, RoundingMode.DOWN, RoundingMode.CEILING))); + } + + ImmutableSet> testImmutableEnumSetFourElements() { + return ImmutableSet.of( + ImmutableSet.of( + RoundingMode.UP, RoundingMode.DOWN, RoundingMode.CEILING, RoundingMode.FLOOR), + ImmutableSet.copyOf( + EnumSet.of( + RoundingMode.UP, RoundingMode.DOWN, RoundingMode.CEILING, RoundingMode.FLOOR))); + } + + ImmutableSet> testImmutableEnumSetFiveElements() { + return ImmutableSet.of( + ImmutableSet.of( + RoundingMode.UP, + RoundingMode.DOWN, + RoundingMode.CEILING, + RoundingMode.FLOOR, + RoundingMode.UNNECESSARY), + ImmutableSet.copyOf( + EnumSet.of( + RoundingMode.UP, + RoundingMode.DOWN, + RoundingMode.CEILING, + RoundingMode.FLOOR, + RoundingMode.UNNECESSARY))); + } + + ImmutableSet testImmutableEnumSetSixElements() { + return ImmutableSet.of( + RoundingMode.UP, + RoundingMode.DOWN, + RoundingMode.CEILING, + RoundingMode.FLOOR, + RoundingMode.UNNECESSARY, + RoundingMode.HALF_EVEN); + } + + ImmutableSet testImmutableEnumSetVarArgs() { + return ImmutableSet.copyOf( + EnumSet.of( + RoundingMode.UP, + RoundingMode.DOWN, + RoundingMode.CEILING, + RoundingMode.FLOOR, + RoundingMode.UNNECESSARY, + RoundingMode.HALF_EVEN)); + } +} diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRulesTestOutput.java new file mode 100644 index 0000000000..11b22b26c1 --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRulesTestOutput.java @@ -0,0 +1,88 @@ +package tech.picnic.errorprone.refasterrules; + +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; +import java.math.RoundingMode; +import java.util.Arrays; +import java.util.EnumSet; +import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; + +final class ImmutableEnumSetRulesTest implements RefasterRuleCollectionTestCase { + @Override + public ImmutableSet elidedTypesAndStaticImports() { + return ImmutableSet.of(EnumSet.class); + } + + ImmutableSet> testImmutableEnumSetIterable() { + return ImmutableSet.of( + Sets.immutableEnumSet(EnumSet.range(RoundingMode.UP, RoundingMode.UNNECESSARY)), + Sets.immutableEnumSet(EnumSet.allOf(RoundingMode.class))); + } + + ImmutableSet> testImmutableEnumSetIterable1() { + return ImmutableSet.of( + Sets.immutableEnumSet(Arrays.asList(RoundingMode.values())), + Sets.immutableEnumSet(Arrays.asList(RoundingMode.class.getEnumConstants()))); + } + + ImmutableSet> testImmutableEnumSetOneElement() { + return ImmutableSet.of( + Sets.immutableEnumSet(RoundingMode.UP), Sets.immutableEnumSet(RoundingMode.UP)); + } + + ImmutableSet> testImmutableEnumSetTwoElements() { + return ImmutableSet.of( + Sets.immutableEnumSet(RoundingMode.UP, RoundingMode.DOWN), + Sets.immutableEnumSet(RoundingMode.UP, RoundingMode.DOWN)); + } + + ImmutableSet> testImmutableEnumSetThreeElements() { + return ImmutableSet.of( + Sets.immutableEnumSet(RoundingMode.UP, RoundingMode.DOWN, RoundingMode.CEILING), + Sets.immutableEnumSet(RoundingMode.UP, RoundingMode.DOWN, RoundingMode.CEILING)); + } + + ImmutableSet> testImmutableEnumSetFourElements() { + return ImmutableSet.of( + Sets.immutableEnumSet( + RoundingMode.UP, RoundingMode.DOWN, RoundingMode.CEILING, RoundingMode.FLOOR), + Sets.immutableEnumSet( + RoundingMode.UP, RoundingMode.DOWN, RoundingMode.CEILING, RoundingMode.FLOOR)); + } + + ImmutableSet> testImmutableEnumSetFiveElements() { + return ImmutableSet.of( + Sets.immutableEnumSet( + RoundingMode.UP, + RoundingMode.DOWN, + RoundingMode.CEILING, + RoundingMode.FLOOR, + RoundingMode.UNNECESSARY), + Sets.immutableEnumSet( + RoundingMode.UP, + RoundingMode.DOWN, + RoundingMode.CEILING, + RoundingMode.FLOOR, + RoundingMode.UNNECESSARY)); + } + + ImmutableSet testImmutableEnumSetSixElements() { + return Sets.immutableEnumSet( + RoundingMode.UP, + RoundingMode.DOWN, + RoundingMode.CEILING, + RoundingMode.FLOOR, + RoundingMode.UNNECESSARY, + RoundingMode.HALF_EVEN); + } + + ImmutableSet testImmutableEnumSetVarArgs() { + return Sets.immutableEnumSet( + RoundingMode.UP, + RoundingMode.DOWN, + RoundingMode.CEILING, + RoundingMode.FLOOR, + RoundingMode.UNNECESSARY, + RoundingMode.HALF_EVEN); + } +} From 7a794d1a74a22883a54d06c89c104bdd0fdd2f72 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Wed, 23 Oct 2024 10:51:33 +0200 Subject: [PATCH 2/3] Suggestions --- .../refasterrules/ImmutableEnumSetRules.java | 51 ++++++++++--------- .../ImmutableEnumSetRulesTestInput.java | 24 ++++----- .../ImmutableEnumSetRulesTestOutput.java | 24 ++++----- 3 files changed, 47 insertions(+), 52 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRules.java index b513725c2b..8d38be3b92 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRules.java @@ -11,16 +11,19 @@ import java.util.EnumSet; import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; -/** Refaster rules related to expressions dealing with immutable {@link Enum} collections. */ +/** + * Refaster rules related to expressions dealing with {@code + * com.google.common.collect.ImmutableEnumSet}s. + */ @OnlineDocumentation final class ImmutableEnumSetRules { private ImmutableEnumSetRules() {} /** - * Prefer {@link Sets#immutableEnumSet(Iterable)} for enum collections to take advantage of + * Prefer {@link Sets#immutableEnumSet(Iterable)} for enum collections to take advantage of the * internally used {@link EnumSet}. */ - static final class ImmutableEnumSetIterable> { + static final class SetsImmutableEnumSetIterable> { @BeforeTemplate ImmutableSet before(Collection elements) { return ImmutableSet.copyOf(elements); @@ -33,10 +36,10 @@ ImmutableSet after(Collection elements) { } /** - * Prefer {@link Sets#immutableEnumSet(Iterable)} for enum collections to take advantage of + * Prefer {@link Sets#immutableEnumSet(Iterable)} for enum collections to take advantage of the * internally used {@link EnumSet}. */ - static final class ImmutableEnumSetIterable1> { + static final class SetsImmutableEnumSetIterableArray> { @BeforeTemplate ImmutableSet before(T[] elements) { return ImmutableSet.copyOf(elements); @@ -50,11 +53,11 @@ ImmutableSet after(T[] elements) { /** * Prefer {@link Sets#immutableEnumSet(Enum, Enum[])} for enum collections to take advantage of - * internally used {@link EnumSet}. + * the internally used {@link EnumSet}. */ - static final class ImmutableEnumSetOneElement> { + static final class SetsImmutableEnumSet1> { @BeforeTemplate - @SuppressWarnings("ImmutableEnumSetIterable" /* This is a more specific template. */) + @SuppressWarnings("SetsImmutableEnumSetIterable" /* This is a more specific template. */) ImmutableSet before(T e1) { return Refaster.anyOf(ImmutableSet.of(e1), ImmutableSet.copyOf(EnumSet.of(e1))); } @@ -68,11 +71,11 @@ ImmutableSet after(T e1) { /** * Prefer {@link Sets#immutableEnumSet(Enum, Enum[])} for enum collections to take advantage of - * internally used {@link EnumSet}. + * the internally used {@link EnumSet}. */ - static final class ImmutableEnumSetTwoElements> { + static final class SetsImmutableEnumSet2> { @BeforeTemplate - @SuppressWarnings("ImmutableEnumSetIterable" /* This is a more specific template. */) + @SuppressWarnings("SetsImmutableEnumSetIterable" /* This is a more specific template. */) ImmutableSet before(T e1, T e2) { return Refaster.anyOf(ImmutableSet.of(e1, e2), ImmutableSet.copyOf(EnumSet.of(e1, e2))); } @@ -86,11 +89,11 @@ ImmutableSet after(T e1, T e2) { /** * Prefer {@link Sets#immutableEnumSet(Enum, Enum[])} for enum collections to take advantage of - * internally used {@link EnumSet}. + * the internally used {@link EnumSet}. */ - static final class ImmutableEnumSetThreeElements> { + static final class SetsImmutableEnumSet3> { @BeforeTemplate - @SuppressWarnings("ImmutableEnumSetIterable" /* This is a more specific template. */) + @SuppressWarnings("SetsImmutableEnumSetIterable" /* This is a more specific template. */) ImmutableSet before(T e1, T e2, T e3) { return Refaster.anyOf( ImmutableSet.of(e1, e2, e3), ImmutableSet.copyOf(EnumSet.of(e1, e2, e3))); @@ -105,11 +108,11 @@ ImmutableSet after(T e1, T e2, T e3) { /** * Prefer {@link Sets#immutableEnumSet(Enum, Enum[])} for enum collections to take advantage of - * internally used {@link EnumSet}. + * the internally used {@link EnumSet}. */ - static final class ImmutableEnumSetFourElements> { + static final class SetsImmutableEnumSet4> { @BeforeTemplate - @SuppressWarnings("ImmutableEnumSetIterable" /* This is a more specific template. */) + @SuppressWarnings("SetsImmutableEnumSetIterable" /* This is a more specific template. */) ImmutableSet before(T e1, T e2, T e3, T e4) { return Refaster.anyOf( ImmutableSet.of(e1, e2, e3, e4), ImmutableSet.copyOf(EnumSet.of(e1, e2, e3, e4))); @@ -124,11 +127,11 @@ ImmutableSet after(T e1, T e2, T e3, T e4) { /** * Prefer {@link Sets#immutableEnumSet(Enum, Enum[])} for enum collections to take advantage of - * internally used {@link EnumSet}. + * the internally used {@link EnumSet}. */ - static final class ImmutableEnumSetFiveElements> { + static final class SetsImmutableEnumSet5> { @BeforeTemplate - @SuppressWarnings("ImmutableEnumSetIterable" /* This is a more specific template. */) + @SuppressWarnings("SetsImmutableEnumSetIterable" /* This is a more specific template. */) ImmutableSet before(T e1, T e2, T e3, T e4, T e5) { return Refaster.anyOf( ImmutableSet.of(e1, e2, e3, e4, e5), ImmutableSet.copyOf(EnumSet.of(e1, e2, e3, e4, e5))); @@ -143,9 +146,9 @@ ImmutableSet after(T e1, T e2, T e3, T e4, T e5) { /** * Prefer {@link Sets#immutableEnumSet(Enum, Enum[])} for enum collections to take advantage of - * internally used {@link EnumSet}. + * the internally used {@link EnumSet}. */ - static final class ImmutableEnumSetSixElements> { + static final class SetsImmutableEnumSet6> { @BeforeTemplate ImmutableSet before(T e1, T e2, T e3, T e4, T e5, T e6) { return ImmutableSet.of(e1, e2, e3, e4, e5, e6); @@ -160,11 +163,11 @@ ImmutableSet after(T e1, T e2, T e3, T e4, T e5, T e6) { /** * Prefer {@link Sets#immutableEnumSet(Enum, Enum[])} for enum collections to take advantage of - * internally used {@link EnumSet}. + * the internally used {@link EnumSet}. */ static final class ImmutableEnumSetVarArgs> { @BeforeTemplate - @SuppressWarnings("ImmutableEnumSetIterable" /* This is a more specific template. */) + @SuppressWarnings("SetsImmutableEnumSetIterable" /* This is a more specific template. */) ImmutableSet before(T e1, @Repeated T elements) { return ImmutableSet.copyOf(EnumSet.of(e1, Refaster.asVarargs(elements))); } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRulesTestInput.java index c693d024ae..c3d1c5ac78 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRulesTestInput.java @@ -11,36 +11,32 @@ public ImmutableSet elidedTypesAndStaticImports() { return ImmutableSet.of(EnumSet.class); } - ImmutableSet> testImmutableEnumSetIterable() { - return ImmutableSet.of( - ImmutableSet.copyOf(EnumSet.range(RoundingMode.UP, RoundingMode.UNNECESSARY)), - ImmutableSet.copyOf(EnumSet.allOf(RoundingMode.class))); + ImmutableSet testSetsImmutableEnumSetIterable() { + return ImmutableSet.copyOf(EnumSet.range(RoundingMode.UP, RoundingMode.UNNECESSARY)); } - ImmutableSet> testImmutableEnumSetIterable1() { - return ImmutableSet.of( - ImmutableSet.copyOf(RoundingMode.values()), - ImmutableSet.copyOf(RoundingMode.class.getEnumConstants())); + ImmutableSet testSetsImmutableEnumSetIterableArray() { + return ImmutableSet.copyOf(RoundingMode.values()); } - ImmutableSet> testImmutableEnumSetOneElement() { + ImmutableSet> testSetsImmutableEnumSet1() { return ImmutableSet.of( ImmutableSet.of(RoundingMode.UP), ImmutableSet.copyOf(EnumSet.of(RoundingMode.UP))); } - ImmutableSet> testImmutableEnumSetTwoElements() { + ImmutableSet> testSetsImmutableEnumSet2() { return ImmutableSet.of( ImmutableSet.of(RoundingMode.UP, RoundingMode.DOWN), ImmutableSet.copyOf(EnumSet.of(RoundingMode.UP, RoundingMode.DOWN))); } - ImmutableSet> testImmutableEnumSetThreeElements() { + ImmutableSet> testSetsImmutableEnumSet3() { return ImmutableSet.of( ImmutableSet.of(RoundingMode.UP, RoundingMode.DOWN, RoundingMode.CEILING), ImmutableSet.copyOf(EnumSet.of(RoundingMode.UP, RoundingMode.DOWN, RoundingMode.CEILING))); } - ImmutableSet> testImmutableEnumSetFourElements() { + ImmutableSet> testSetsImmutableEnumSet4() { return ImmutableSet.of( ImmutableSet.of( RoundingMode.UP, RoundingMode.DOWN, RoundingMode.CEILING, RoundingMode.FLOOR), @@ -49,7 +45,7 @@ ImmutableSet> testImmutableEnumSetFourElements() { RoundingMode.UP, RoundingMode.DOWN, RoundingMode.CEILING, RoundingMode.FLOOR))); } - ImmutableSet> testImmutableEnumSetFiveElements() { + ImmutableSet> testSetsImmutableEnumSet5() { return ImmutableSet.of( ImmutableSet.of( RoundingMode.UP, @@ -66,7 +62,7 @@ ImmutableSet> testImmutableEnumSetFiveElements() { RoundingMode.UNNECESSARY))); } - ImmutableSet testImmutableEnumSetSixElements() { + ImmutableSet testSetsImmutableEnumSet6() { return ImmutableSet.of( RoundingMode.UP, RoundingMode.DOWN, diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRulesTestOutput.java index 11b22b26c1..45544b7fa6 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRulesTestOutput.java @@ -13,36 +13,32 @@ public ImmutableSet elidedTypesAndStaticImports() { return ImmutableSet.of(EnumSet.class); } - ImmutableSet> testImmutableEnumSetIterable() { - return ImmutableSet.of( - Sets.immutableEnumSet(EnumSet.range(RoundingMode.UP, RoundingMode.UNNECESSARY)), - Sets.immutableEnumSet(EnumSet.allOf(RoundingMode.class))); + ImmutableSet testSetsImmutableEnumSetIterable() { + return Sets.immutableEnumSet(EnumSet.range(RoundingMode.UP, RoundingMode.UNNECESSARY)); } - ImmutableSet> testImmutableEnumSetIterable1() { - return ImmutableSet.of( - Sets.immutableEnumSet(Arrays.asList(RoundingMode.values())), - Sets.immutableEnumSet(Arrays.asList(RoundingMode.class.getEnumConstants()))); + ImmutableSet testSetsImmutableEnumSetIterableArray() { + return Sets.immutableEnumSet(Arrays.asList(RoundingMode.values())); } - ImmutableSet> testImmutableEnumSetOneElement() { + ImmutableSet> testSetsImmutableEnumSet1() { return ImmutableSet.of( Sets.immutableEnumSet(RoundingMode.UP), Sets.immutableEnumSet(RoundingMode.UP)); } - ImmutableSet> testImmutableEnumSetTwoElements() { + ImmutableSet> testSetsImmutableEnumSet2() { return ImmutableSet.of( Sets.immutableEnumSet(RoundingMode.UP, RoundingMode.DOWN), Sets.immutableEnumSet(RoundingMode.UP, RoundingMode.DOWN)); } - ImmutableSet> testImmutableEnumSetThreeElements() { + ImmutableSet> testSetsImmutableEnumSet3() { return ImmutableSet.of( Sets.immutableEnumSet(RoundingMode.UP, RoundingMode.DOWN, RoundingMode.CEILING), Sets.immutableEnumSet(RoundingMode.UP, RoundingMode.DOWN, RoundingMode.CEILING)); } - ImmutableSet> testImmutableEnumSetFourElements() { + ImmutableSet> testSetsImmutableEnumSet4() { return ImmutableSet.of( Sets.immutableEnumSet( RoundingMode.UP, RoundingMode.DOWN, RoundingMode.CEILING, RoundingMode.FLOOR), @@ -50,7 +46,7 @@ ImmutableSet> testImmutableEnumSetFourElements() { RoundingMode.UP, RoundingMode.DOWN, RoundingMode.CEILING, RoundingMode.FLOOR)); } - ImmutableSet> testImmutableEnumSetFiveElements() { + ImmutableSet> testSetsImmutableEnumSet5() { return ImmutableSet.of( Sets.immutableEnumSet( RoundingMode.UP, @@ -66,7 +62,7 @@ ImmutableSet> testImmutableEnumSetFiveElements() { RoundingMode.UNNECESSARY)); } - ImmutableSet testImmutableEnumSetSixElements() { + ImmutableSet testSetsImmutableEnumSet6() { return Sets.immutableEnumSet( RoundingMode.UP, RoundingMode.DOWN, From fd415f53cdc746a9118e01a0249245ab55f68c5d Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sat, 26 Oct 2024 12:32:52 +0200 Subject: [PATCH 3/3] Suggestions --- .../refasterrules/AssortedRules.java | 24 ------- .../refasterrules/ImmutableEnumSetRules.java | 72 ++++++++++++++++++- .../refasterrules/AssortedRulesTestInput.java | 10 +-- .../AssortedRulesTestOutput.java | 10 +-- .../ImmutableEnumSetRulesTestInput.java | 21 ++++-- .../ImmutableEnumSetRulesTestOutput.java | 22 ++++-- 6 files changed, 104 insertions(+), 55 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssortedRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssortedRules.java index 4841481ee8..a38a823ea4 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssortedRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssortedRules.java @@ -1,8 +1,6 @@ package tech.picnic.errorprone.refasterrules; import static com.google.common.base.Preconditions.checkElementIndex; -import static com.google.common.collect.ImmutableSet.toImmutableSet; -import static com.google.common.collect.Sets.toImmutableEnumSet; import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS; import static java.util.Collections.disjoint; import static java.util.Objects.checkIndex; @@ -70,28 +68,6 @@ void after(int index, int size) { } } - /** - * Use {@link Sets#toImmutableEnumSet()} when possible, as it is more efficient than {@link - * ImmutableSet#toImmutableSet()} and produces a more compact object. - * - *

Warning: this rewrite rule is not completely behavior preserving: while the - * original code produces a set that iterates over the elements in encounter order, the - * replacement code iterates over the elements in enum definition order. - */ - // XXX: ^ Consider emitting a comment warning about this fact? - static final class StreamToImmutableEnumSet> { - @BeforeTemplate - ImmutableSet before(Stream stream) { - return stream.collect(toImmutableSet()); - } - - @AfterTemplate - @UseImportPolicy(STATIC_IMPORT_ALWAYS) - ImmutableSet after(Stream stream) { - return stream.collect(toImmutableEnumSet()); - } - } - /** Prefer {@link Iterators#getNext(Iterator, Object)} over more contrived alternatives. */ static final class IteratorGetNextOrDefault { @BeforeTemplate diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRules.java index 8d38be3b92..b7b40af650 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRules.java @@ -1,20 +1,32 @@ package tech.picnic.errorprone.refasterrules; +import static com.google.common.collect.ImmutableSet.toImmutableSet; +import static com.google.common.collect.Sets.toImmutableEnumSet; +import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS; + import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; import com.google.errorprone.refaster.Refaster; import com.google.errorprone.refaster.annotation.AfterTemplate; import com.google.errorprone.refaster.annotation.BeforeTemplate; import com.google.errorprone.refaster.annotation.Repeated; +import com.google.errorprone.refaster.annotation.UseImportPolicy; import java.util.Arrays; import java.util.Collection; import java.util.EnumSet; +import java.util.stream.Stream; import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; /** * Refaster rules related to expressions dealing with {@code * com.google.common.collect.ImmutableEnumSet}s. */ +// XXX: Some of the rules defined here impact iteration order. That's a rather subtle change. Should +// we emit a comment warning about this fact? (This may produce a lot of noise. A bug checker could +// in some cases determine whether iteration order is important.) +// XXX: Consider replacing the `SetsImmutableEnumSet[N]` Refaster rules with a bug checker, such +// that call to `ImmutableSet#of(Object, Object, Object, Object, Object, Object, Object[])` with +// enum-typed values can also be rewritten. @OnlineDocumentation final class ImmutableEnumSetRules { private ImmutableEnumSetRules() {} @@ -22,15 +34,24 @@ private ImmutableEnumSetRules() {} /** * Prefer {@link Sets#immutableEnumSet(Iterable)} for enum collections to take advantage of the * internally used {@link EnumSet}. + * + *

Warning: this rule is not completely behavior preserving: while the + * original code produces a set that iterates over its elements in the same order as the input + * {@link Iterable}, the replacement code iterates over the elements in enum definition order. */ static final class SetsImmutableEnumSetIterable> { + @BeforeTemplate + ImmutableSet before(Iterable elements) { + return ImmutableSet.copyOf(elements); + } + @BeforeTemplate ImmutableSet before(Collection elements) { return ImmutableSet.copyOf(elements); } @AfterTemplate - ImmutableSet after(Collection elements) { + ImmutableSet after(Iterable elements) { return Sets.immutableEnumSet(elements); } } @@ -38,8 +59,12 @@ ImmutableSet after(Collection elements) { /** * Prefer {@link Sets#immutableEnumSet(Iterable)} for enum collections to take advantage of the * internally used {@link EnumSet}. + * + *

Warning: this rule is not completely behavior preserving: while the + * original code produces a set that iterates over its elements in the same order as defined in + * the array, the replacement code iterates over the elements in enum definition order. */ - static final class SetsImmutableEnumSetIterableArray> { + static final class SetsImmutableEnumSetArraysAsList> { @BeforeTemplate ImmutableSet before(T[] elements) { return ImmutableSet.copyOf(elements); @@ -72,6 +97,10 @@ ImmutableSet after(T e1) { /** * Prefer {@link Sets#immutableEnumSet(Enum, Enum[])} for enum collections to take advantage of * the internally used {@link EnumSet}. + * + *

Warning: this rule is not completely behavior preserving: while the {@link + * ImmutableSet#of} expression produces a set that iterates over its elements in the listed order, + * the replacement code iterates over the elements in enum definition order. */ static final class SetsImmutableEnumSet2> { @BeforeTemplate @@ -90,6 +119,10 @@ ImmutableSet after(T e1, T e2) { /** * Prefer {@link Sets#immutableEnumSet(Enum, Enum[])} for enum collections to take advantage of * the internally used {@link EnumSet}. + * + *

Warning: this rule is not completely behavior preserving: while the {@link + * ImmutableSet#of} expression produces a set that iterates over its elements in the listed order, + * the replacement code iterates over the elements in enum definition order. */ static final class SetsImmutableEnumSet3> { @BeforeTemplate @@ -109,6 +142,10 @@ ImmutableSet after(T e1, T e2, T e3) { /** * Prefer {@link Sets#immutableEnumSet(Enum, Enum[])} for enum collections to take advantage of * the internally used {@link EnumSet}. + * + *

Warning: this rule is not completely behavior preserving: while the {@link + * ImmutableSet#of} expression produces a set that iterates over its elements in the listed order, + * the replacement code iterates over the elements in enum definition order. */ static final class SetsImmutableEnumSet4> { @BeforeTemplate @@ -128,6 +165,10 @@ ImmutableSet after(T e1, T e2, T e3, T e4) { /** * Prefer {@link Sets#immutableEnumSet(Enum, Enum[])} for enum collections to take advantage of * the internally used {@link EnumSet}. + * + *

Warning: this rule is not completely behavior preserving: while the {@link + * ImmutableSet#of} expression produces a set that iterates over its elements in the listed order, + * the replacement code iterates over the elements in enum definition order. */ static final class SetsImmutableEnumSet5> { @BeforeTemplate @@ -147,6 +188,10 @@ ImmutableSet after(T e1, T e2, T e3, T e4, T e5) { /** * Prefer {@link Sets#immutableEnumSet(Enum, Enum[])} for enum collections to take advantage of * the internally used {@link EnumSet}. + * + *

Warning: this rule is not completely behavior preserving: while the + * original code produces a set that iterates over its elements in the listed order, the + * replacement code iterates over the elements in enum definition order. */ static final class SetsImmutableEnumSet6> { @BeforeTemplate @@ -165,7 +210,7 @@ ImmutableSet after(T e1, T e2, T e3, T e4, T e5, T e6) { * Prefer {@link Sets#immutableEnumSet(Enum, Enum[])} for enum collections to take advantage of * the internally used {@link EnumSet}. */ - static final class ImmutableEnumSetVarArgs> { + static final class SetsImmutableEnumSetVarArgs> { @BeforeTemplate @SuppressWarnings("SetsImmutableEnumSetIterable" /* This is a more specific template. */) ImmutableSet before(T e1, @Repeated T elements) { @@ -177,4 +222,25 @@ ImmutableSet after(T e1, @Repeated T elements) { return Sets.immutableEnumSet(e1, Refaster.asVarargs(elements)); } } + + /** + * Use {@link Sets#toImmutableEnumSet()} when possible, as it is more efficient than {@link + * ImmutableSet#toImmutableSet()} and produces a more compact object. + * + *

Warning: this rule is not completely behavior preserving: while the + * original code produces a set that iterates over its elements in encounter order, the + * replacement code iterates over the elements in enum definition order. + */ + static final class StreamToImmutableEnumSet> { + @BeforeTemplate + ImmutableSet before(Stream stream) { + return stream.collect(toImmutableSet()); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + ImmutableSet after(Stream stream) { + return stream.collect(toImmutableEnumSet()); + } + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssortedRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssortedRulesTestInput.java index 1f785293b0..98174797e0 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssortedRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssortedRulesTestInput.java @@ -1,10 +1,7 @@ package tech.picnic.errorprone.refasterrules; -import static com.google.common.collect.ImmutableSet.toImmutableSet; - import com.google.common.base.Preconditions; import com.google.common.base.Splitter; -import com.google.common.collect.BoundType; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; @@ -24,8 +21,7 @@ public ImmutableSet elidedTypesAndStaticImports() { Preconditions.class, Sets.class, Splitter.class, - Streams.class, - toImmutableSet()); + Streams.class); } int testCheckIndex() { @@ -38,10 +34,6 @@ void testCheckIndexConditional() { } } - ImmutableSet testStreamToImmutableEnumSet() { - return Stream.of(BoundType.OPEN).collect(toImmutableSet()); - } - ImmutableSet testIteratorGetNextOrDefault() { return ImmutableSet.of( ImmutableList.of("a").iterator().hasNext() diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssortedRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssortedRulesTestOutput.java index 2fd9c408af..2e6794dbfd 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssortedRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssortedRulesTestOutput.java @@ -1,12 +1,9 @@ package tech.picnic.errorprone.refasterrules; -import static com.google.common.collect.ImmutableSet.toImmutableSet; -import static com.google.common.collect.Sets.toImmutableEnumSet; import static java.util.Objects.checkIndex; import com.google.common.base.Preconditions; import com.google.common.base.Splitter; -import com.google.common.collect.BoundType; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; @@ -27,8 +24,7 @@ public ImmutableSet elidedTypesAndStaticImports() { Preconditions.class, Sets.class, Splitter.class, - Streams.class, - toImmutableSet()); + Streams.class); } int testCheckIndex() { @@ -39,10 +35,6 @@ void testCheckIndexConditional() { checkIndex(1, 2); } - ImmutableSet testStreamToImmutableEnumSet() { - return Stream.of(BoundType.OPEN).collect(toImmutableEnumSet()); - } - ImmutableSet testIteratorGetNextOrDefault() { return ImmutableSet.of( Iterators.getNext(ImmutableList.of("a").iterator(), "foo"), diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRulesTestInput.java index c3d1c5ac78..10662a9ab6 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRulesTestInput.java @@ -1,21 +1,28 @@ package tech.picnic.errorprone.refasterrules; +import static com.google.common.collect.ImmutableSet.toImmutableSet; + +import com.google.common.collect.BoundType; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; import java.math.RoundingMode; import java.util.EnumSet; +import java.util.stream.Stream; import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; final class ImmutableEnumSetRulesTest implements RefasterRuleCollectionTestCase { @Override public ImmutableSet elidedTypesAndStaticImports() { - return ImmutableSet.of(EnumSet.class); + return ImmutableSet.of(EnumSet.class, toImmutableSet()); } - ImmutableSet testSetsImmutableEnumSetIterable() { - return ImmutableSet.copyOf(EnumSet.range(RoundingMode.UP, RoundingMode.UNNECESSARY)); + ImmutableSet> testSetsImmutableEnumSetIterable() { + return ImmutableSet.of( + ImmutableSet.copyOf(Iterables.cycle(RoundingMode.UP)), + ImmutableSet.copyOf(EnumSet.allOf(RoundingMode.class))); } - ImmutableSet testSetsImmutableEnumSetIterableArray() { + ImmutableSet testSetsImmutableEnumSetArraysAsList() { return ImmutableSet.copyOf(RoundingMode.values()); } @@ -72,7 +79,7 @@ ImmutableSet testSetsImmutableEnumSet6() { RoundingMode.HALF_EVEN); } - ImmutableSet testImmutableEnumSetVarArgs() { + ImmutableSet testSetsImmutableEnumSetVarArgs() { return ImmutableSet.copyOf( EnumSet.of( RoundingMode.UP, @@ -82,4 +89,8 @@ ImmutableSet testImmutableEnumSetVarArgs() { RoundingMode.UNNECESSARY, RoundingMode.HALF_EVEN)); } + + ImmutableSet testStreamToImmutableEnumSet() { + return Stream.of(BoundType.OPEN).collect(toImmutableSet()); + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRulesTestOutput.java index 45544b7fa6..c96b01db21 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableEnumSetRulesTestOutput.java @@ -1,23 +1,31 @@ package tech.picnic.errorprone.refasterrules; +import static com.google.common.collect.ImmutableSet.toImmutableSet; +import static com.google.common.collect.Sets.toImmutableEnumSet; + +import com.google.common.collect.BoundType; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; import com.google.common.collect.Sets; import java.math.RoundingMode; import java.util.Arrays; import java.util.EnumSet; +import java.util.stream.Stream; import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; final class ImmutableEnumSetRulesTest implements RefasterRuleCollectionTestCase { @Override public ImmutableSet elidedTypesAndStaticImports() { - return ImmutableSet.of(EnumSet.class); + return ImmutableSet.of(EnumSet.class, toImmutableSet()); } - ImmutableSet testSetsImmutableEnumSetIterable() { - return Sets.immutableEnumSet(EnumSet.range(RoundingMode.UP, RoundingMode.UNNECESSARY)); + ImmutableSet> testSetsImmutableEnumSetIterable() { + return ImmutableSet.of( + Sets.immutableEnumSet(Iterables.cycle(RoundingMode.UP)), + Sets.immutableEnumSet(EnumSet.allOf(RoundingMode.class))); } - ImmutableSet testSetsImmutableEnumSetIterableArray() { + ImmutableSet testSetsImmutableEnumSetArraysAsList() { return Sets.immutableEnumSet(Arrays.asList(RoundingMode.values())); } @@ -72,7 +80,7 @@ ImmutableSet testSetsImmutableEnumSet6() { RoundingMode.HALF_EVEN); } - ImmutableSet testImmutableEnumSetVarArgs() { + ImmutableSet testSetsImmutableEnumSetVarArgs() { return Sets.immutableEnumSet( RoundingMode.UP, RoundingMode.DOWN, @@ -81,4 +89,8 @@ ImmutableSet testImmutableEnumSetVarArgs() { RoundingMode.UNNECESSARY, RoundingMode.HALF_EVEN); } + + ImmutableSet testStreamToImmutableEnumSet() { + return Stream.of(BoundType.OPEN).collect(toImmutableEnumSet()); + } }