From 3c162cb1d73eecf5001e5d2ff3dcdb88e3b29143 Mon Sep 17 00:00:00 2001 From: mohamedsamehsalah Date: Mon, 1 May 2023 07:40:05 +0200 Subject: [PATCH 1/4] Prefer Sets#intersection over more contrived alternatives. --- .../refasterrules/ImmutableSetRules.java | 42 +++++++++++++++++++ .../ImmutableSetRulesTestInput.java | 19 +++++++++ .../ImmutableSetRulesTestOutput.java | 17 ++++++++ 3 files changed, 78 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 f4d5ba78b7..e3f169fe77 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 @@ -6,6 +6,8 @@ import static java.util.Collections.singleton; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.SetMultimap; +import com.google.common.collect.Sets; import com.google.common.collect.Sets.SetView; import com.google.common.collect.Streams; import com.google.errorprone.refaster.Refaster; @@ -15,6 +17,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.Iterator; +import java.util.Map; import java.util.Set; import java.util.stream.Stream; import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; @@ -211,4 +214,43 @@ ImmutableSet after(T e1, T e2, T e3, T e4, T e5) { return ImmutableSet.of(e1, e2, e3, e4, e5); } } + + /** Prefer {@link Sets#intersection(Set, Set)} ()} over more contrived alternatives. */ + static final class SetsIntersection { + @BeforeTemplate + ImmutableSet before(Set set1, Set set2) { + return set1.stream().filter(set2::contains).collect(toImmutableSet()); + } + + @AfterTemplate + ImmutableSet after(Set set1, Set set2) { + return Sets.intersection(set1, set2).immutableCopy(); + } + } + + /** Prefer {@link Sets#intersection(Set, Set)} ()} over more contrived alternatives. */ + static final class MapKeySetIntersection { + @BeforeTemplate + ImmutableSet before(Set set, Map map) { + return set.stream().filter(map::containsKey).collect(toImmutableSet()); + } + + @AfterTemplate + ImmutableSet after(Set set, Map map) { + return Sets.intersection(set, map.keySet()).immutableCopy(); + } + } + + /** Prefer {@link Sets#intersection(Set, Set)} ()} over more contrived alternatives. */ + static final class SetMultimapKeySetIntersection { + @BeforeTemplate + ImmutableSet before(Set set, SetMultimap setMultimap) { + return set.stream().filter(setMultimap::containsKey).collect(toImmutableSet()); + } + + @AfterTemplate + ImmutableSet after(Set set, SetMultimap setMultimap) { + return Sets.intersection(set, setMultimap.keySet()).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 9512cdea64..e90414aa08 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 @@ -3,7 +3,9 @@ import static com.google.common.collect.ImmutableSet.toImmutableSet; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.ImmutableSetMultimap; import com.google.common.collect.Sets; import com.google.common.collect.Streams; import java.util.Arrays; @@ -72,4 +74,21 @@ Set testImmutableSetOf4() { Set testImmutableSetOf5() { return Set.of(1, 2, 3, 4, 5); } + + ImmutableSet testSetsIntersection() { + ImmutableSet set = ImmutableSet.of(1); + return ImmutableSet.of(1, 2, 3).stream().filter(set::contains).collect(toImmutableSet()); + } + + ImmutableSet testMapKeySetIntersection() { + ImmutableMap map = ImmutableMap.of(1, 4); + return ImmutableSet.of(1, 2, 3).stream().filter(map::containsKey).collect(toImmutableSet()); + } + + ImmutableSet testSetMultimapKeySetIntersection() { + ImmutableSetMultimap setMultiMap = ImmutableSetMultimap.of(1, 4); + return ImmutableSet.of(1, 2, 3).stream() + .filter(setMultiMap::containsKey) + .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 b081feee13..2c8b4212a3 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 @@ -3,7 +3,9 @@ import static com.google.common.collect.ImmutableSet.toImmutableSet; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.ImmutableSetMultimap; import com.google.common.collect.Sets; import com.google.common.collect.Streams; import java.util.Arrays; @@ -67,4 +69,19 @@ Set testImmutableSetOf4() { Set testImmutableSetOf5() { return ImmutableSet.of(1, 2, 3, 4, 5); } + + ImmutableSet testSetsIntersection() { + ImmutableSet set = ImmutableSet.of(1); + return Sets.intersection(ImmutableSet.of(1, 2, 3), set).immutableCopy(); + } + + ImmutableSet testMapKeySetIntersection() { + ImmutableMap map = ImmutableMap.of(1, 4); + return Sets.intersection(ImmutableSet.of(1, 2, 3), map.keySet()).immutableCopy(); + } + + ImmutableSet testSetMultimapKeySetIntersection() { + ImmutableSetMultimap setMultiMap = ImmutableSetMultimap.of(1, 4); + return Sets.intersection(ImmutableSet.of(1, 2, 3), setMultiMap.keySet()).immutableCopy(); + } } From 3f096fa066a85678ba2eac7cb8ad84b45de77eac Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Tue, 2 May 2023 09:18:43 +0200 Subject: [PATCH 2/4] Suggestions --- .../refasterrules/ImmutableSetRules.java | 20 +++++++++---------- .../ImmutableSetRulesTestInput.java | 18 ++++++++--------- .../ImmutableSetRulesTestOutput.java | 16 +++++++-------- 3 files changed, 26 insertions(+), 28 deletions(-) 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 e3f169fe77..37feb51a1b 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 @@ -6,7 +6,7 @@ import static java.util.Collections.singleton; import com.google.common.collect.ImmutableSet; -import com.google.common.collect.SetMultimap; +import com.google.common.collect.Multimap; import com.google.common.collect.Sets; import com.google.common.collect.Sets.SetView; import com.google.common.collect.Streams; @@ -215,7 +215,7 @@ ImmutableSet after(T e1, T e2, T e3, T e4, T e5) { } } - /** Prefer {@link Sets#intersection(Set, Set)} ()} over more contrived alternatives. */ + /** Prefer {@link Sets#intersection(Set, Set)} over more contrived alternatives. */ static final class SetsIntersection { @BeforeTemplate ImmutableSet before(Set set1, Set set2) { @@ -228,8 +228,8 @@ ImmutableSet after(Set set1, Set set2) { } } - /** Prefer {@link Sets#intersection(Set, Set)} ()} over more contrived alternatives. */ - static final class MapKeySetIntersection { + /** Prefer {@link Sets#intersection(Set, Set)} over more contrived alternatives. */ + static final class SetsIntersectionMap { @BeforeTemplate ImmutableSet before(Set set, Map map) { return set.stream().filter(map::containsKey).collect(toImmutableSet()); @@ -241,16 +241,16 @@ ImmutableSet after(Set set, Map map) { } } - /** Prefer {@link Sets#intersection(Set, Set)} ()} over more contrived alternatives. */ - static final class SetMultimapKeySetIntersection { + /** Prefer {@link Sets#intersection(Set, Set)} over more contrived alternatives. */ + static final class SetsIntersectionMultimap { @BeforeTemplate - ImmutableSet before(Set set, SetMultimap setMultimap) { - return set.stream().filter(setMultimap::containsKey).collect(toImmutableSet()); + ImmutableSet before(Set set, Multimap multimap) { + return set.stream().filter(multimap::containsKey).collect(toImmutableSet()); } @AfterTemplate - ImmutableSet after(Set set, SetMultimap setMultimap) { - return Sets.intersection(set, setMultimap.keySet()).immutableCopy(); + ImmutableSet after(Set set, Multimap multimap) { + return Sets.intersection(set, multimap.keySet()).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 e90414aa08..e0c426f7f1 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 @@ -4,8 +4,8 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableSet; -import com.google.common.collect.ImmutableSetMultimap; import com.google.common.collect.Sets; import com.google.common.collect.Streams; import java.util.Arrays; @@ -77,18 +77,16 @@ Set testImmutableSetOf5() { ImmutableSet testSetsIntersection() { ImmutableSet set = ImmutableSet.of(1); - return ImmutableSet.of(1, 2, 3).stream().filter(set::contains).collect(toImmutableSet()); + return ImmutableSet.of(2).stream().filter(set::contains).collect(toImmutableSet()); } - ImmutableSet testMapKeySetIntersection() { - ImmutableMap map = ImmutableMap.of(1, 4); - return ImmutableSet.of(1, 2, 3).stream().filter(map::containsKey).collect(toImmutableSet()); + ImmutableSet testSetsIntersectionMap() { + ImmutableMap map = ImmutableMap.of(1, 2); + return ImmutableSet.of(3).stream().filter(map::containsKey).collect(toImmutableSet()); } - ImmutableSet testSetMultimapKeySetIntersection() { - ImmutableSetMultimap setMultiMap = ImmutableSetMultimap.of(1, 4); - return ImmutableSet.of(1, 2, 3).stream() - .filter(setMultiMap::containsKey) - .collect(toImmutableSet()); + ImmutableSet testSetsIntersectionMultimap() { + ImmutableMultimap multimap = ImmutableMultimap.of(1, 2); + return ImmutableSet.of(3).stream().filter(multimap::containsKey).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 2c8b4212a3..367e2e84fe 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 @@ -4,8 +4,8 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableSet; -import com.google.common.collect.ImmutableSetMultimap; import com.google.common.collect.Sets; import com.google.common.collect.Streams; import java.util.Arrays; @@ -72,16 +72,16 @@ Set testImmutableSetOf5() { ImmutableSet testSetsIntersection() { ImmutableSet set = ImmutableSet.of(1); - return Sets.intersection(ImmutableSet.of(1, 2, 3), set).immutableCopy(); + return Sets.intersection(ImmutableSet.of(2), set).immutableCopy(); } - ImmutableSet testMapKeySetIntersection() { - ImmutableMap map = ImmutableMap.of(1, 4); - return Sets.intersection(ImmutableSet.of(1, 2, 3), map.keySet()).immutableCopy(); + ImmutableSet testSetsIntersectionMap() { + ImmutableMap map = ImmutableMap.of(1, 2); + return Sets.intersection(ImmutableSet.of(3), map.keySet()).immutableCopy(); } - ImmutableSet testSetMultimapKeySetIntersection() { - ImmutableSetMultimap setMultiMap = ImmutableSetMultimap.of(1, 4); - return Sets.intersection(ImmutableSet.of(1, 2, 3), setMultiMap.keySet()).immutableCopy(); + ImmutableSet testSetsIntersectionMultimap() { + ImmutableMultimap multimap = ImmutableMultimap.of(1, 2); + return Sets.intersection(ImmutableSet.of(3), multimap.keySet()).immutableCopy(); } } From 704e38d2f0cab174baa36df497c5b5e89e892931 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Tue, 2 May 2023 12:13:08 +0200 Subject: [PATCH 3/4] Suggestions --- .../refasterrules/ImmutableSetRules.java | 85 ++++++++++++++++--- .../ImmutableSetRulesTestInput.java | 50 +++++++++-- .../ImmutableSetRulesTestOutput.java | 34 ++++++-- 3 files changed, 141 insertions(+), 28 deletions(-) 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 37feb51a1b..a5af1a00e4 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 @@ -4,6 +4,7 @@ import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS; import static java.util.Collections.emptySet; import static java.util.Collections.singleton; +import static java.util.function.Predicate.not; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Multimap; @@ -215,41 +216,101 @@ ImmutableSet after(T e1, T e2, T e3, T e4, T e5) { } } - /** Prefer {@link Sets#intersection(Set, Set)} over more contrived alternatives. */ - static final class SetsIntersection { + /** + * Prefer an immutable copy of {@link Sets#difference(Set, Set)} over more contrived alternatives. + */ + static final class SetsDifference { + @BeforeTemplate + ImmutableSet before(Set set1, Set set2) { + return set1.stream() + .filter(Refaster.anyOf(not(set2::contains), e -> !set2.contains(e))) + .collect(toImmutableSet()); + } + + @AfterTemplate + ImmutableSet after(Set set1, Set set2) { + return Sets.difference(set1, set2).immutableCopy(); + } + } + + /** + * Prefer an immutable copy of {@link Sets#difference(Set, Set)} over more contrived alternatives. + */ + static final class SetsDifferenceMap { @BeforeTemplate - ImmutableSet before(Set set1, Set set2) { + ImmutableSet before(Set set, Map map) { + return set.stream() + .filter(Refaster.anyOf(not(map::containsKey), e -> !map.containsKey(e))) + .collect(toImmutableSet()); + } + + @AfterTemplate + ImmutableSet after(Set set, Map map) { + return Sets.difference(set, map.keySet()).immutableCopy(); + } + } + + /** + * Prefer an immutable copy of {@link Sets#difference(Set, Set)} over more contrived alternatives. + */ + static final class SetsDifferenceMultimap { + @BeforeTemplate + ImmutableSet before(Set set, Multimap multimap) { + return set.stream() + .filter(Refaster.anyOf(not(multimap::containsKey), e -> !multimap.containsKey(e))) + .collect(toImmutableSet()); + } + + @AfterTemplate + ImmutableSet after(Set set, Multimap multimap) { + return Sets.difference(set, multimap.keySet()).immutableCopy(); + } + } + + /** + * Prefer an immutable copy of {@link Sets#intersection(Set, Set)} over more contrived + * alternatives. + */ + static final class SetsIntersection { + @BeforeTemplate + ImmutableSet before(Set set1, Set set2) { return set1.stream().filter(set2::contains).collect(toImmutableSet()); } @AfterTemplate - ImmutableSet after(Set set1, Set set2) { + ImmutableSet after(Set set1, Set set2) { return Sets.intersection(set1, set2).immutableCopy(); } } - /** Prefer {@link Sets#intersection(Set, Set)} over more contrived alternatives. */ - static final class SetsIntersectionMap { + /** + * Prefer an immutable copy of {@link Sets#intersection(Set, Set)} over more contrived + * alternatives. + */ + static final class SetsIntersectionMap { @BeforeTemplate - ImmutableSet before(Set set, Map map) { + ImmutableSet before(Set set, Map map) { return set.stream().filter(map::containsKey).collect(toImmutableSet()); } @AfterTemplate - ImmutableSet after(Set set, Map map) { + ImmutableSet after(Set set, Map map) { return Sets.intersection(set, map.keySet()).immutableCopy(); } } - /** Prefer {@link Sets#intersection(Set, Set)} over more contrived alternatives. */ - static final class SetsIntersectionMultimap { + /** + * Prefer an immutable copy of {@link Sets#intersection(Set, Set)} over more contrived + * alternatives. + */ + static final class SetsIntersectionMultimap { @BeforeTemplate - ImmutableSet before(Set set, Multimap multimap) { + ImmutableSet before(Set set, Multimap multimap) { return set.stream().filter(multimap::containsKey).collect(toImmutableSet()); } @AfterTemplate - ImmutableSet after(Set set, Multimap multimap) { + ImmutableSet after(Set set, Multimap multimap) { return Sets.intersection(set, multimap.keySet()).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 e0c426f7f1..09fdae535b 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 @@ -1,11 +1,12 @@ package tech.picnic.errorprone.refasterrules; import static com.google.common.collect.ImmutableSet.toImmutableSet; +import static java.util.function.Predicate.not; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.ImmutableSetMultimap; import com.google.common.collect.Sets; import com.google.common.collect.Streams; import java.util.Arrays; @@ -17,7 +18,7 @@ final class ImmutableSetRulesTest implements RefasterRuleCollectionTestCase { @Override public ImmutableSet elidedTypesAndStaticImports() { - return ImmutableSet.of(Arrays.class, Collections.class, Streams.class); + return ImmutableSet.of(Arrays.class, Collections.class, Streams.class, not(null)); } ImmutableSet.Builder testImmutableSetBuilder() { @@ -75,18 +76,51 @@ Set testImmutableSetOf5() { return Set.of(1, 2, 3, 4, 5); } + ImmutableSet> testSetsDifference() { + return ImmutableSet.of( + ImmutableSet.of(1).stream() + .filter(not(ImmutableSet.of(2)::contains)) + .collect(toImmutableSet()), + ImmutableSet.of(3).stream() + .filter(v -> !ImmutableSet.of(4).contains(v)) + .collect(toImmutableSet())); + } + + ImmutableSet> testSetsDifferenceMap() { + return ImmutableSet.of( + ImmutableSet.of(1).stream() + .filter(not(ImmutableMap.of(2, 3)::containsKey)) + .collect(toImmutableSet()), + ImmutableSet.of(4).stream() + .filter(v -> !ImmutableMap.of(5, 6).containsKey(v)) + .collect(toImmutableSet())); + } + + ImmutableSet> testSetsDifferenceMultimap() { + return ImmutableSet.of( + ImmutableSet.of(1).stream() + .filter(not(ImmutableSetMultimap.of(2, 3)::containsKey)) + .collect(toImmutableSet()), + ImmutableSet.of(4).stream() + .filter(v -> !ImmutableSetMultimap.of(5, 6).containsKey(v)) + .collect(toImmutableSet())); + } + ImmutableSet testSetsIntersection() { - ImmutableSet set = ImmutableSet.of(1); - return ImmutableSet.of(2).stream().filter(set::contains).collect(toImmutableSet()); + return ImmutableSet.of(1).stream() + .filter(ImmutableSet.of(2)::contains) + .collect(toImmutableSet()); } ImmutableSet testSetsIntersectionMap() { - ImmutableMap map = ImmutableMap.of(1, 2); - return ImmutableSet.of(3).stream().filter(map::containsKey).collect(toImmutableSet()); + return ImmutableSet.of(1).stream() + .filter(ImmutableMap.of(2, 3)::containsKey) + .collect(toImmutableSet()); } ImmutableSet testSetsIntersectionMultimap() { - ImmutableMultimap multimap = ImmutableMultimap.of(1, 2); - return ImmutableSet.of(3).stream().filter(multimap::containsKey).collect(toImmutableSet()); + return ImmutableSet.of(1).stream() + .filter(ImmutableSetMultimap.of(2, 3)::containsKey) + .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 367e2e84fe..f60aa49842 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 @@ -1,11 +1,12 @@ package tech.picnic.errorprone.refasterrules; import static com.google.common.collect.ImmutableSet.toImmutableSet; +import static java.util.function.Predicate.not; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.ImmutableSetMultimap; import com.google.common.collect.Sets; import com.google.common.collect.Streams; import java.util.Arrays; @@ -17,7 +18,7 @@ final class ImmutableSetRulesTest implements RefasterRuleCollectionTestCase { @Override public ImmutableSet elidedTypesAndStaticImports() { - return ImmutableSet.of(Arrays.class, Collections.class, Streams.class); + return ImmutableSet.of(Arrays.class, Collections.class, Streams.class, not(null)); } ImmutableSet.Builder testImmutableSetBuilder() { @@ -70,18 +71,35 @@ Set testImmutableSetOf5() { return ImmutableSet.of(1, 2, 3, 4, 5); } + ImmutableSet> testSetsDifference() { + return ImmutableSet.of( + Sets.difference(ImmutableSet.of(1), ImmutableSet.of(2)).immutableCopy(), + Sets.difference(ImmutableSet.of(3), ImmutableSet.of(4)).immutableCopy()); + } + + ImmutableSet> testSetsDifferenceMap() { + return ImmutableSet.of( + Sets.difference(ImmutableSet.of(1), ImmutableMap.of(2, 3).keySet()).immutableCopy(), + Sets.difference(ImmutableSet.of(4), ImmutableMap.of(5, 6).keySet()).immutableCopy()); + } + + ImmutableSet> testSetsDifferenceMultimap() { + return ImmutableSet.of( + Sets.difference(ImmutableSet.of(1), ImmutableSetMultimap.of(2, 3).keySet()).immutableCopy(), + Sets.difference(ImmutableSet.of(4), ImmutableSetMultimap.of(5, 6).keySet()) + .immutableCopy()); + } + ImmutableSet testSetsIntersection() { - ImmutableSet set = ImmutableSet.of(1); - return Sets.intersection(ImmutableSet.of(2), set).immutableCopy(); + return Sets.intersection(ImmutableSet.of(1), ImmutableSet.of(2)).immutableCopy(); } ImmutableSet testSetsIntersectionMap() { - ImmutableMap map = ImmutableMap.of(1, 2); - return Sets.intersection(ImmutableSet.of(3), map.keySet()).immutableCopy(); + return Sets.intersection(ImmutableSet.of(1), ImmutableMap.of(2, 3).keySet()).immutableCopy(); } ImmutableSet testSetsIntersectionMultimap() { - ImmutableMultimap multimap = ImmutableMultimap.of(1, 2); - return Sets.intersection(ImmutableSet.of(3), multimap.keySet()).immutableCopy(); + return Sets.intersection(ImmutableSet.of(1), ImmutableSetMultimap.of(2, 3).keySet()) + .immutableCopy(); } } From 215622d4972f313c01ba4d54041c55968fd91ac6 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Tue, 2 May 2023 12:51:54 +0200 Subject: [PATCH 4/4] 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 a5af1a00e4..628bb1cf6b 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 09fdae535b..53e7c41556 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 f60aa49842..74a6c9d67e 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(); + } }