From 92a4238a2f95a91290624acd01fc6a2be808ba65 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sat, 26 Aug 2023 14:36:03 +0300 Subject: [PATCH] Introduce `CollectionContains` Refaster rule --- .../errorprone/refasterrules/CollectionRules.java | 13 +++++++++++++ .../refasterrules/CollectionRulesTestInput.java | 4 ++++ .../refasterrules/CollectionRulesTestOutput.java | 4 ++++ 3 files changed, 21 insertions(+) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/CollectionRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/CollectionRules.java index ba6146025a..7a9802cb1f 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/CollectionRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/CollectionRules.java @@ -74,6 +74,19 @@ int after(Collection collection) { } } + /** Prefer {@link Collection#contains(Object)} over more contrived alternatives. */ + static final class CollectionContains { + @BeforeTemplate + boolean before(Collection collection, S value) { + return collection.stream().anyMatch(value::equals); + } + + @AfterTemplate + boolean after(Collection collection, S value) { + return collection.contains(value); + } + } + /** * Don't call {@link Iterables#addAll(Collection, Iterable)} when the elements to be added are * already part of a {@link Collection}. diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestInput.java index 369a84cba0..735587515c 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestInput.java @@ -36,6 +36,10 @@ ImmutableSet testCollectionSize() { return ImmutableSet.of(Iterables.size(ImmutableSet.of(1)), ImmutableSet.of(2).asList().size()); } + boolean testCollectionContains() { + return ImmutableSet.of("foo").stream().anyMatch("bar"::equals); + } + boolean testCollectionAddAllToCollectionExpression() { return Iterables.addAll(new ArrayList<>(), ImmutableSet.of("foo")); } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestOutput.java index 1f5380ec38..cf2040e33b 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/CollectionRulesTestOutput.java @@ -36,6 +36,10 @@ ImmutableSet testCollectionSize() { return ImmutableSet.of(ImmutableSet.of(1).size(), ImmutableSet.of(2).size()); } + boolean testCollectionContains() { + return ImmutableSet.of("foo").contains("bar"); + } + boolean testCollectionAddAllToCollectionExpression() { return new ArrayList<>().addAll(ImmutableSet.of("foo")); }