mirror of
https://github.com/jlengrand/error-prone-support.git
synced 2026-03-10 08:11:25 +00:00
Introduce ArraysAsList Refaster rule (#1275)
This commit is contained in:
@@ -8,7 +8,9 @@ import com.google.errorprone.refaster.Refaster;
|
|||||||
import com.google.errorprone.refaster.annotation.AfterTemplate;
|
import com.google.errorprone.refaster.annotation.AfterTemplate;
|
||||||
import com.google.errorprone.refaster.annotation.AlsoNegation;
|
import com.google.errorprone.refaster.annotation.AlsoNegation;
|
||||||
import com.google.errorprone.refaster.annotation.BeforeTemplate;
|
import com.google.errorprone.refaster.annotation.BeforeTemplate;
|
||||||
|
import com.google.errorprone.refaster.annotation.NotMatches;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -21,6 +23,7 @@ import java.util.function.Consumer;
|
|||||||
import java.util.function.IntFunction;
|
import java.util.function.IntFunction;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation;
|
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation;
|
||||||
|
import tech.picnic.errorprone.refaster.matchers.IsRefasterAsVarargs;
|
||||||
|
|
||||||
/** Refaster rules related to expressions dealing with (arbitrary) collections. */
|
/** 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
|
// XXX: There are other Guava `Iterables` methods that should not be called if the input is known to
|
||||||
@@ -294,6 +297,23 @@ final class CollectionRules {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 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<T> {
|
||||||
|
// XXX: This expression produces an unmodifiable list, while the alternative doesn't.
|
||||||
|
@BeforeTemplate
|
||||||
|
List<T> before(@NotMatches(IsRefasterAsVarargs.class) T[] array) {
|
||||||
|
return Arrays.stream(array).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterTemplate
|
||||||
|
List<T> after(T[] array) {
|
||||||
|
return Arrays.asList(array);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Prefer calling {@link Collection#toArray()} over more contrived alternatives. */
|
/** Prefer calling {@link Collection#toArray()} over more contrived alternatives. */
|
||||||
static final class CollectionToArray<T> {
|
static final class CollectionToArray<T> {
|
||||||
@BeforeTemplate
|
@BeforeTemplate
|
||||||
|
|||||||
@@ -6,9 +6,11 @@ import com.google.common.collect.ImmutableSortedSet;
|
|||||||
import com.google.common.collect.Iterables;
|
import com.google.common.collect.Iterables;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
@@ -98,6 +100,10 @@ final class CollectionRulesTest implements RefasterRuleCollectionTestCase {
|
|||||||
return ImmutableSet.of(1).asList().toString();
|
return ImmutableSet.of(1).asList().toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<String> testArraysAsList() {
|
||||||
|
return Arrays.stream(new String[0]).toList();
|
||||||
|
}
|
||||||
|
|
||||||
ImmutableSet<Object[]> testCollectionToArray() {
|
ImmutableSet<Object[]> testCollectionToArray() {
|
||||||
return ImmutableSet.of(
|
return ImmutableSet.of(
|
||||||
ImmutableSet.of(1).toArray(new Object[1]),
|
ImmutableSet.of(1).toArray(new Object[1]),
|
||||||
|
|||||||
@@ -6,9 +6,11 @@ import com.google.common.collect.ImmutableSortedSet;
|
|||||||
import com.google.common.collect.Iterables;
|
import com.google.common.collect.Iterables;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
@@ -90,6 +92,10 @@ final class CollectionRulesTest implements RefasterRuleCollectionTestCase {
|
|||||||
return ImmutableSet.of(1).toString();
|
return ImmutableSet.of(1).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<String> testArraysAsList() {
|
||||||
|
return Arrays.asList(new String[0]);
|
||||||
|
}
|
||||||
|
|
||||||
ImmutableSet<Object[]> testCollectionToArray() {
|
ImmutableSet<Object[]> testCollectionToArray() {
|
||||||
return ImmutableSet.of(
|
return ImmutableSet.of(
|
||||||
ImmutableSet.of(1).toArray(), ImmutableSet.of(2).toArray(), ImmutableSet.of(3).toArray());
|
ImmutableSet.of(1).toArray(), ImmutableSet.of(2).toArray(), ImmutableSet.of(3).toArray());
|
||||||
|
|||||||
Reference in New Issue
Block a user