diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/CollectionRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/CollectionRules.java index ecbc7e51f49..4ee37189e1d 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/CollectionRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/CollectionRules.java @@ -8,7 +8,9 @@ import com.google.errorprone.refaster.annotation.AfterTemplate; import com.google.errorprone.refaster.annotation.AlsoNegation; import com.google.errorprone.refaster.annotation.BeforeTemplate; +import com.google.errorprone.refaster.annotation.NotMatches; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.List; @@ -21,6 +23,7 @@ import java.util.function.IntFunction; import java.util.stream.Stream; import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; +import tech.picnic.errorprone.refaster.matchers.IsRefasterAsVarargs; /** Refaster rules related to expressions dealing with (arbitrary) collections. */ // XXX: There are other Guava `Iterables` methods that should not be called if the input is known to @@ -294,6 +297,23 @@ String after(ImmutableCollection collection) { } } + /** Prefer {@link Arrays#asList(Object[])} over more contrived alternatives. */ + // XXX: Consider moving this rule to `ImmutableListRules` and having it suggest + // `ImmutableList#copyOf`. That would retain immutability, at the cost of no longer handling + // `null`s. + static final class ArraysAsList { + // XXX: This expression produces an unmodifiable list, while the alternative doesn't. + @BeforeTemplate + List before(@NotMatches(IsRefasterAsVarargs.class) T[] array) { + return Arrays.stream(array).toList(); + } + + @AfterTemplate + List after(T[] array) { + return Arrays.asList(array); + } + } + /** Prefer calling {@link Collection#toArray()} over more contrived alternatives. */ static final class CollectionToArray { @BeforeTemplate diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestInput.java index 2968c3a093d..c667a80895f 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestInput.java @@ -6,9 +6,11 @@ import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; +import java.util.List; import java.util.Optional; import java.util.TreeSet; import java.util.stream.Stream; @@ -98,6 +100,10 @@ String testImmutableCollectionToString() { return ImmutableSet.of(1).asList().toString(); } + List testArraysAsList() { + return Arrays.stream(new String[0]).toList(); + } + ImmutableSet testCollectionToArray() { return ImmutableSet.of( ImmutableSet.of(1).toArray(new Object[1]), diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestOutput.java index 1eb0abc0c37..756135c8890 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestOutput.java @@ -6,9 +6,11 @@ import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; +import java.util.List; import java.util.Optional; import java.util.TreeSet; import java.util.stream.Stream; @@ -90,6 +92,10 @@ String testImmutableCollectionToString() { return ImmutableSet.of(1).toString(); } + List testArraysAsList() { + return Arrays.asList(new String[0]); + } + ImmutableSet testCollectionToArray() { return ImmutableSet.of( ImmutableSet.of(1).toArray(), ImmutableSet.of(2).toArray(), ImmutableSet.of(3).toArray());