diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/LexicographicalAnnotationAttributeListing.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/LexicographicalAnnotationAttributeListing.java index fa92cf5ead..c3d7051274 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/LexicographicalAnnotationAttributeListing.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/LexicographicalAnnotationAttributeListing.java @@ -51,6 +51,9 @@ *

The idea behind this checker is that maintaining a sorted sequence simplifies conflict * resolution, and can even avoid it if two branches add the same entry. */ +// XXX: In some places we declare a `@SuppressWarnings` annotation with a final value of +// `key-to-resolve-AnnotationUseStyle-and-TrailingComment-check-conflict`. That entry must stay +// last. Consider adding (generic?) support for such cases. @AutoService(BugChecker.class) @BugPattern( summary = "Where possible, sort annotation array attributes lexicographically", 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 7a9802cb1f..cd341e791d 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 @@ -35,13 +35,21 @@ private CollectionRules() {} */ static final class CollectionIsEmpty { @BeforeTemplate - @SuppressWarnings("java:S1155" /* This violation will be rewritten. */) + @SuppressWarnings({ + "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. */, + "key-to-resolve-AnnotationUseStyle-and-TrailingComment-check-conflict" + }) boolean before(Collection collection) { return Refaster.anyOf( collection.size() == 0, collection.size() <= 0, collection.size() < 1, - Iterables.isEmpty(collection)); + Iterables.isEmpty(collection), + collection.stream().findAny().isEmpty(), + collection.stream().findFirst().isEmpty()); } @BeforeTemplate @@ -337,7 +345,9 @@ Iterator after(ImmutableCollection collection) { /** * Don't use the ternary operator to extract the first element of a possibly-empty {@link - * Collection} as an {@link Optional}. + * Collection} as an {@link Optional}, and (when applicable) prefer {@link Stream#findFirst()} + * over {@link Stream#findAny()} to communicate that the collection's first element (if any, + * according to iteration order) will be returned. */ static final class OptionalFirstCollectionElement { @BeforeTemplate 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 735587515c..186da035b7 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 @@ -29,7 +29,9 @@ ImmutableSet testCollectionIsEmpty() { ImmutableSet.of(5).size() > 0, ImmutableSet.of(6).size() >= 1, Iterables.isEmpty(ImmutableSet.of(7)), - ImmutableSet.of(8).asList().isEmpty()); + ImmutableSet.of(8).stream().findAny().isEmpty(), + ImmutableSet.of(9).stream().findFirst().isEmpty(), + ImmutableSet.of(10).asList().isEmpty()); } ImmutableSet testCollectionSize() { 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 cf2040e33b..ba09e13e63 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 @@ -29,7 +29,9 @@ ImmutableSet testCollectionIsEmpty() { !ImmutableSet.of(5).isEmpty(), !ImmutableSet.of(6).isEmpty(), ImmutableSet.of(7).isEmpty(), - ImmutableSet.of(8).isEmpty()); + ImmutableSet.of(8).isEmpty(), + ImmutableSet.of(9).isEmpty(), + ImmutableSet.of(10).isEmpty()); } ImmutableSet testCollectionSize() {