Skip to content

Commit

Permalink
Introduce CollectionContains Refaster rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Aug 29, 2023
1 parent 48e2f57 commit 5877e71
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ int after(Collection<T> collection) {
}
}

/** Prefer {@link Collection#contains(Object)} over more contrived alternatives. */
static final class CollectionContains<T, S> {
@BeforeTemplate
boolean before(Collection<T> collection, S value) {
return collection.stream().anyMatch(value::equals);
}

@AfterTemplate
boolean after(Collection<T> 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}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ ImmutableSet<Integer> 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"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ ImmutableSet<Integer> 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"));
}
Expand Down

0 comments on commit 5877e71

Please sign in to comment.