From 5b65230d9da69bbc4d2d4db7f6478e57d861fba2 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Tue, 2 May 2023 12:51:54 +0200 Subject: [PATCH] Suggestion --- .../errorprone/refasterrules/ImmutableSetRules.java | 13 +++++++++++++ .../refasterrules/ImmutableSetRulesTestInput.java | 5 +++++ .../refasterrules/ImmutableSetRulesTestOutput.java | 4 ++++ 3 files changed, 22 insertions(+) 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 a5af1a00e42..628bb1cf6b9 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 @@ -314,4 +314,17 @@ ImmutableSet after(Set set, Multimap multimap) { return Sets.intersection(set, multimap.keySet()).immutableCopy(); } } + + /** Prefer an immutable copy of {@link Sets#union(Set, Set)} over more contrived alternatives. */ + static final class SetsUnion { + @BeforeTemplate + ImmutableSet before(Set set1, Set set2) { + return Stream.concat(set1.stream(), set2.stream()).collect(toImmutableSet()); + } + + @AfterTemplate + ImmutableSet after(Set set1, Set set2) { + return Sets.union(set1, set2).immutableCopy(); + } + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableSetRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableSetRulesTestInput.java index 09fdae535bd..53e7c41556d 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableSetRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableSetRulesTestInput.java @@ -123,4 +123,9 @@ ImmutableSet testSetsIntersectionMultimap() { .filter(ImmutableSetMultimap.of(2, 3)::containsKey) .collect(toImmutableSet()); } + + ImmutableSet testSetsUnion() { + return Stream.concat(ImmutableSet.of(1).stream(), ImmutableSet.of(2).stream()) + .collect(toImmutableSet()); + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableSetRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableSetRulesTestOutput.java index f60aa49842e..74a6c9d67e0 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableSetRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ImmutableSetRulesTestOutput.java @@ -102,4 +102,8 @@ ImmutableSet testSetsIntersectionMultimap() { return Sets.intersection(ImmutableSet.of(1), ImmutableSetMultimap.of(2, 3).keySet()) .immutableCopy(); } + + ImmutableSet testSetsUnion() { + return Sets.union(ImmutableSet.of(1), ImmutableSet.of(2)).immutableCopy(); + } }