Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compact and replace StreamIs{,Not}Empty Refaster rules #1028

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static final class CollectionIsEmpty<T> {
"java:S1155" /* This violation will be rewritten. */,
"LexicographicalAnnotationAttributeListing" /* `key-*` entry must remain last. */,
"OptionalFirstCollectionElement" /* This is a more specific template. */,
"StreamIsEmpty" /* This is a more specific template. */,
"StreamFindAnyIsEmpty" /* This is a more specific template. */,
"key-to-resolve-AnnotationUseStyle-and-TrailingComment-check-conflict"
})
boolean before(Collection<T> collection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.AlsoNegation;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.Matches;
import com.google.errorprone.refaster.annotation.MayOptionallyUse;
Expand Down Expand Up @@ -256,7 +257,7 @@ Optional<S> after(Stream<T> stream, Function<? super T, S> function) {
// XXX: This rule assumes that any matched `Collector` does not perform any filtering.
// (Perhaps we could add a `@Matches` guard that validates that the collector expression does not
// contain a `Collectors#filtering` call. That'd still not be 100% accurate, though.)
static final class StreamIsEmpty<T, K, V, C extends Collection<K>, M extends Map<K, V>> {
static final class StreamFindAnyIsEmpty<T, K, V, C extends Collection<K>, M extends Map<K, V>> {
@BeforeTemplate
boolean before(Stream<T> stream, Collector<? super T, ?, ? extends C> collector) {
return Refaster.anyOf(
Expand All @@ -274,20 +275,20 @@ boolean before2(Stream<T> stream, Collector<? super T, ?, ? extends M> collector
}

@AfterTemplate
@AlsoNegation
boolean after(Stream<T> stream) {
return stream.findAny().isEmpty();
}
}

/** In order to test whether a stream has any element, simply try to find one. */
static final class StreamIsNotEmpty<T> {
/**
* Prefer {@link Stream#findAny()} over {@link Stream#findFirst()} if one only cares whether the
* stream is nonempty.
*/
static final class StreamFindAnyIsPresent<T> {
@BeforeTemplate
boolean before(Stream<T> stream) {
return Refaster.anyOf(
stream.count() != 0,
stream.count() > 0,
stream.count() >= 1,
stream.findFirst().isPresent());
return stream.findFirst().isPresent();
}

@AfterTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ ImmutableSet<Optional<Integer>> testStreamMapFirst() {
Stream.of("bar").map(String::length).findFirst());
}

ImmutableSet<Boolean> testStreamIsEmpty() {
ImmutableSet<Boolean> testStreamFindAnyIsEmpty() {
return ImmutableSet.of(
Stream.of(1).count() == 0,
Stream.of(2).count() <= 0,
Expand All @@ -131,15 +131,14 @@ ImmutableSet<Boolean> testStreamIsEmpty() {
Stream.of(7).collect(collectingAndThen(toImmutableList(), ImmutableList::isEmpty)),
Stream.of(8).collect(collectingAndThen(toImmutableMap(k -> k, v -> v), Map::isEmpty)),
Stream.of(9)
.collect(collectingAndThen(toImmutableMap(k -> k, v -> v), ImmutableMap::isEmpty)));
.collect(collectingAndThen(toImmutableMap(k -> k, v -> v), ImmutableMap::isEmpty)),
Stream.of(10).count() != 0,
Stream.of(11).count() > 0,
Stream.of(12).count() >= 1);
}

ImmutableSet<Boolean> testStreamIsNotEmpty() {
return ImmutableSet.of(
Stream.of(1).count() != 0,
Stream.of(2).count() > 0,
Stream.of(3).count() >= 1,
Stream.of(4).findFirst().isPresent());
boolean testStreamFindAnyIsPresent() {
return Stream.of(1).findFirst().isPresent();
}

ImmutableSet<Optional<String>> testStreamMin() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ ImmutableSet<Optional<Integer>> testStreamMapFirst() {
Stream.of("bar").findFirst().map(String::length));
}

ImmutableSet<Boolean> testStreamIsEmpty() {
ImmutableSet<Boolean> testStreamFindAnyIsEmpty() {
return ImmutableSet.of(
Stream.of(1).findAny().isEmpty(),
Stream.of(2).findAny().isEmpty(),
Expand All @@ -131,15 +131,14 @@ ImmutableSet<Boolean> testStreamIsEmpty() {
Stream.of(6).findAny().isEmpty(),
Stream.of(7).findAny().isEmpty(),
Stream.of(8).findAny().isEmpty(),
Stream.of(9).findAny().isEmpty());
Stream.of(9).findAny().isEmpty(),
!Stream.of(10).findAny().isEmpty(),
!Stream.of(11).findAny().isEmpty(),
!Stream.of(12).findAny().isEmpty());
}

ImmutableSet<Boolean> testStreamIsNotEmpty() {
return ImmutableSet.of(
Stream.of(1).findAny().isPresent(),
Stream.of(2).findAny().isPresent(),
Stream.of(3).findAny().isPresent(),
Stream.of(4).findAny().isPresent());
boolean testStreamFindAnyIsPresent() {
return Stream.of(1).findAny().isPresent();
}

ImmutableSet<Optional<String>> testStreamMin() {
Expand Down