Add Refaster rules for the new Guava release

This commit is contained in:
Stephan Schroevers
2020-10-17 16:53:32 +02:00
parent 2c7d2174b4
commit 71f4d15db8
3 changed files with 142 additions and 2 deletions

View File

@@ -2,11 +2,16 @@ package tech.picnic.errorprone.refastertemplates;
import static java.util.function.Function.identity;
import com.google.common.collect.Comparators;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.refaster.ImportPolicy;
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.UseImportPolicy;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.function.Function;
@@ -60,4 +65,78 @@ final class ComparatorTemplates {
return Comparator.reverseOrder();
}
}
/** Prefer {@link Comparators#min(Comparable, Comparable)}} over more verbose alternatives. */
static final class MinOfPairNaturalOrder<T extends Comparable<? super T>> {
@BeforeTemplate
T before(T value1, T value2) {
return Collections.min(
Refaster.anyOf(
Arrays.asList(value1, value2),
ImmutableList.of(value1, value2),
ImmutableSet.of(value1, value2)));
}
@AfterTemplate
T after(T value1, T value2) {
return Comparators.min(value1, value2);
}
}
/**
* Prefer {@link Comparators#min(Object, Object, Comparator)}}} over more verbose alternatives.
*/
static final class MinOfPairCustomOrder<T> {
@BeforeTemplate
T before(T value1, T value2, Comparator<T> cmp) {
return Collections.min(
Refaster.anyOf(
Arrays.asList(value1, value2),
ImmutableList.of(value1, value2),
ImmutableSet.of(value1, value2)),
cmp);
}
@AfterTemplate
T after(T value1, T value2, Comparator<T> cmp) {
return Comparators.min(value1, value2, cmp);
}
}
/** Prefer {@link Comparators#max(Comparable, Comparable)}} over more verbose alternatives. */
static final class MaxOfPairNaturalOrder<T extends Comparable<? super T>> {
@BeforeTemplate
T before(T value1, T value2) {
return Collections.max(
Refaster.anyOf(
Arrays.asList(value1, value2),
ImmutableList.of(value1, value2),
ImmutableSet.of(value1, value2)));
}
@AfterTemplate
T after(T value1, T value2) {
return Comparators.max(value1, value2);
}
}
/**
* Prefer {@link Comparators#max(Object, Object, Comparator)}}} over more verbose alternatives.
*/
static final class MaxOfPairCustomOrder<T> {
@BeforeTemplate
T before(T value1, T value2, Comparator<T> cmp) {
return Collections.max(
Refaster.anyOf(
Arrays.asList(value1, value2),
ImmutableList.of(value1, value2),
ImmutableSet.of(value1, value2)),
cmp);
}
@AfterTemplate
T after(T value1, T value2, Comparator<T> cmp) {
return Comparators.max(value1, value2, cmp);
}
}
}

View File

@@ -2,13 +2,17 @@ package tech.picnic.errorprone.bugpatterns;
import static java.util.function.Function.identity;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
final class ComparatorTemplatesTest implements RefasterTemplateTestCase {
@Override
public ImmutableSet<?> elidedTypesAndStaticImports() {
return ImmutableSet.of(identity());
return ImmutableSet.of(
Arrays.class, Collections.class, ImmutableList.class, ImmutableSet.class, identity());
}
ImmutableSet<Comparator<String>> testNaturalOrderComparator() {
@@ -24,4 +28,32 @@ final class ComparatorTemplatesTest implements RefasterTemplateTestCase {
Comparator<String> testReverseOrder() {
return Comparator.<String>naturalOrder().reversed();
}
ImmutableSet<String> testMinOfPairNaturalOrder() {
return ImmutableSet.of(
Collections.min(Arrays.asList("a", "b")),
Collections.min(ImmutableList.of("a", "b")),
Collections.min(ImmutableSet.of("a", "b")));
}
ImmutableSet<Object> testMinOfPairCustomOrder() {
return ImmutableSet.of(
Collections.min(Arrays.asList(new Object(), new Object()), (a, b) -> -1),
Collections.min(ImmutableList.of(new Object(), new Object()), (a, b) -> 0),
Collections.min(ImmutableSet.of(new Object(), new Object()), (a, b) -> 1));
}
ImmutableSet<String> testMaxOfPairNaturalOrder() {
return ImmutableSet.of(
Collections.max(Arrays.asList("a", "b")),
Collections.max(ImmutableList.of("a", "b")),
Collections.max(ImmutableSet.of("a", "b")));
}
ImmutableSet<Object> testMaxOfPairCustomOrder() {
return ImmutableSet.of(
Collections.max(Arrays.asList(new Object(), new Object()), (a, b) -> -1),
Collections.max(ImmutableList.of(new Object(), new Object()), (a, b) -> 0),
Collections.max(ImmutableSet.of(new Object(), new Object()), (a, b) -> 1));
}
}

View File

@@ -4,13 +4,18 @@ import static java.util.Comparator.naturalOrder;
import static java.util.Comparator.reverseOrder;
import static java.util.function.Function.identity;
import com.google.common.collect.Comparators;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
final class ComparatorTemplatesTest implements RefasterTemplateTestCase {
@Override
public ImmutableSet<?> elidedTypesAndStaticImports() {
return ImmutableSet.of(identity());
return ImmutableSet.of(
Arrays.class, Collections.class, ImmutableList.class, ImmutableSet.class, identity());
}
ImmutableSet<Comparator<String>> testNaturalOrderComparator() {
@@ -26,4 +31,28 @@ final class ComparatorTemplatesTest implements RefasterTemplateTestCase {
Comparator<String> testReverseOrder() {
return reverseOrder();
}
ImmutableSet<String> testMinOfPairNaturalOrder() {
return ImmutableSet.of(
Comparators.min("a", "b"), Comparators.min("a", "b"), Comparators.min("a", "b"));
}
ImmutableSet<Object> testMinOfPairCustomOrder() {
return ImmutableSet.of(
Comparators.min(new Object(), new Object(), (a, b) -> -1),
Comparators.min(new Object(), new Object(), (a, b) -> 0),
Comparators.min(new Object(), new Object(), (a, b) -> 1));
}
ImmutableSet<String> testMaxOfPairNaturalOrder() {
return ImmutableSet.of(
Comparators.max("a", "b"), Comparators.max("a", "b"), Comparators.max("a", "b"));
}
ImmutableSet<Object> testMaxOfPairCustomOrder() {
return ImmutableSet.of(
Comparators.max(new Object(), new Object(), (a, b) -> -1),
Comparators.max(new Object(), new Object(), (a, b) -> 0),
Comparators.max(new Object(), new Object(), (a, b) -> 1));
}
}