mirror of
https://github.com/jlengrand/error-prone-support.git
synced 2026-03-10 08:11:25 +00:00
Compact and replace StreamIs{,Not}Empty Refaster rules (#1028)
The new `StreamFindAnyIs{Empty,Present}` rules are simpler thanks to the
use of `@AlsoNegation`. In some cases an additional application of the
`OptionalIsEmpty` rule will be required.
This commit is contained in:
committed by
GitHub
parent
52fe79c343
commit
f8cac19330
@@ -39,7 +39,7 @@ final class CollectionRules {
|
||||
"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) {
|
||||
|
||||
@@ -25,6 +25,7 @@ import com.google.common.collect.Streams;
|
||||
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;
|
||||
@@ -256,7 +257,7 @@ final class StreamRules {
|
||||
// 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(
|
||||
@@ -274,20 +275,20 @@ final class StreamRules {
|
||||
}
|
||||
|
||||
@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
|
||||
|
||||
@@ -120,7 +120,7 @@ final class StreamRulesTest implements RefasterRuleCollectionTestCase {
|
||||
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,
|
||||
@@ -131,15 +131,14 @@ final class StreamRulesTest implements RefasterRuleCollectionTestCase {
|
||||
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() {
|
||||
|
||||
@@ -121,7 +121,7 @@ final class StreamRulesTest implements RefasterRuleCollectionTestCase {
|
||||
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(),
|
||||
@@ -131,15 +131,14 @@ final class StreamRulesTest implements RefasterRuleCollectionTestCase {
|
||||
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() {
|
||||
|
||||
Reference in New Issue
Block a user