From a83574e42748952f7bbb805ce73fb00aa8b8a412 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Tue, 3 Jan 2023 09:12:30 +0100 Subject: [PATCH] Post-rebase fix --- .../refasterrules/ImmutableSetRules.java | 4 +- .../ImmutableSetTemplates.java | 155 ------------------ 2 files changed, 1 insertion(+), 158 deletions(-) delete mode 100644 error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableSetRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableSetRules.java index 628bb1cf6b..ea949fdb80 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableSetRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ImmutableSetRules.java @@ -29,8 +29,6 @@ final class ImmutableSetRules { private ImmutableSetRules() {} /** Prefer {@link ImmutableSet#builder()} over the associated constructor. */ - // XXX: This drops generic type information, sometimes leading to non-compilable code. See - // https://github.com/google/error-prone/pull/2706. static final class ImmutableSetBuilder { @BeforeTemplate ImmutableSet.Builder before() { @@ -39,7 +37,7 @@ ImmutableSet.Builder before() { @AfterTemplate ImmutableSet.Builder after() { - return ImmutableSet.builder(); + return ImmutableSet.builder(); } } 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 deleted file mode 100644 index 30bfae2916..0000000000 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableSetTemplates.java +++ /dev/null @@ -1,155 +0,0 @@ -package tech.picnic.errorprone.refastertemplates; - -import static com.google.common.collect.ImmutableSet.toImmutableSet; -import static java.util.stream.Collectors.collectingAndThen; -import static java.util.stream.Collectors.toList; -import static java.util.stream.Collectors.toSet; - -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Sets.SetView; -import com.google.common.collect.Streams; -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.Collection; -import java.util.Collections; -import java.util.Iterator; -import java.util.Set; -import java.util.stream.Stream; - -/** Refaster templates related to expressions dealing with {@link ImmutableSet}s. */ -final class ImmutableSetTemplates { - private ImmutableSetTemplates() {} - - /** Prefer {@link ImmutableSet#builder()} over the associated constructor. */ - // XXX: Picnic's Error Prone fork supports method invocation type argument inlining in the - // `@AfterTemplate`. Without using the fork, the expression in the `@AfterTemplate` can result in - // non-compilable code. See: https://github.com/google/error-prone/pull/2706. - static final class ImmutableSetBuilder { - @BeforeTemplate - ImmutableSet.Builder before() { - return new ImmutableSet.Builder<>(); - } - - @AfterTemplate - ImmutableSet.Builder after() { - return ImmutableSet.builder(); - } - } - - /** 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 - ImmutableSet before(T[] iterable) { - return Refaster.anyOf( - ImmutableSet.builder().add(iterable).build(), - Arrays.stream(iterable).collect(toImmutableSet())); - } - - @BeforeTemplate - ImmutableSet before(Iterator iterable) { - return Refaster.anyOf( - ImmutableSet.builder().addAll(iterable).build(), - Streams.stream(iterable).collect(toImmutableSet())); - } - - @BeforeTemplate - ImmutableSet before(Iterable iterable) { - return Refaster.anyOf( - ImmutableSet.builder().addAll(iterable).build(), - Streams.stream(iterable).collect(toImmutableSet())); - } - - @BeforeTemplate - ImmutableSet before(Collection iterable) { - return iterable.stream().collect(toImmutableSet()); - } - - @AfterTemplate - ImmutableSet after(Iterable iterable) { - return ImmutableSet.copyOf(iterable); - } - } - - /** Prefer {@link ImmutableSet#toImmutableSet()} over less idiomatic alternatives. */ - // XXX: Once the code base has been sufficiently cleaned up, we might want to also rewrite - // `Collectors.toSet(`), with the caveat that it allows mutation (though this cannot be relied - // upon) as well as nulls. Another option is to explicitly rewrite those variants to - // `Collectors.toSet(HashSet::new)`. - static final class StreamToImmutableSet { - @BeforeTemplate - ImmutableSet before(Stream stream) { - return Refaster.anyOf( - ImmutableSet.copyOf(stream.iterator()), - stream.distinct().collect(toImmutableSet()), - stream.collect(collectingAndThen(toList(), ImmutableSet::copyOf)), - stream.collect(collectingAndThen(toSet(), ImmutableSet::copyOf))); - } - - @AfterTemplate - @UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) - ImmutableSet after(Stream stream) { - return stream.collect(toImmutableSet()); - } - } - - /** Don't unnecessarily copy an {@link ImmutableSet}. */ - static final class ImmutableSetCopyOfImmutableSet { - @BeforeTemplate - ImmutableSet before(ImmutableSet set) { - return ImmutableSet.copyOf(set); - } - - @AfterTemplate - ImmutableSet after(ImmutableSet set) { - return set; - } - } - - /** Prefer {@link SetView#immutableCopy()} over the more verbose alternative. */ - static final class ImmutableSetCopyOfSetView { - @BeforeTemplate - ImmutableSet before(SetView set) { - return ImmutableSet.copyOf(set); - } - - @AfterTemplate - ImmutableSet after(SetView set) { - return set.immutableCopy(); - } - } -}