From 9b9b9ffd48212c221ff66473cff4dbe12a691fb6 Mon Sep 17 00:00:00 2001 From: Giovanni Zotta Date: Sat, 10 Feb 2024 22:07:09 +0100 Subject: [PATCH 1/2] Extend --- .../tech/picnic/errorprone/refasterrules/CollectionRules.java | 3 ++- .../errorprone/refasterrules/CollectionRulesTestInput.java | 3 ++- .../errorprone/refasterrules/CollectionRulesTestOutput.java | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) 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..bdcffa8661 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 @@ -41,7 +41,8 @@ boolean before(Collection collection) { collection.size() == 0, collection.size() <= 0, collection.size() < 1, - Iterables.isEmpty(collection)); + Iterables.isEmpty(collection), + collection.stream().findAny().isEmpty()); } @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..56f194cd4d 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,8 @@ 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).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..96719dcdbf 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,8 @@ 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 testCollectionSize() { From 095e3f1061d1be7934481d672cd9b34b19d3fe88 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sun, 11 Feb 2024 12:02:30 +0100 Subject: [PATCH 2/2] Suggestions --- ...LexicographicalAnnotationAttributeListing.java | 3 +++ .../errorprone/refasterrules/CollectionRules.java | 15 ++++++++++++--- .../refasterrules/CollectionRulesTestInput.java | 3 ++- .../refasterrules/CollectionRulesTestOutput.java | 3 ++- 4 files changed, 19 insertions(+), 5 deletions(-) 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 bdcffa8661..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,14 +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), - collection.stream().findAny().isEmpty()); + collection.stream().findAny().isEmpty(), + collection.stream().findFirst().isEmpty()); } @BeforeTemplate @@ -338,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 56f194cd4d..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 @@ -30,7 +30,8 @@ ImmutableSet testCollectionIsEmpty() { ImmutableSet.of(6).size() >= 1, Iterables.isEmpty(ImmutableSet.of(7)), ImmutableSet.of(8).stream().findAny().isEmpty(), - ImmutableSet.of(9).asList().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 96719dcdbf..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 @@ -30,7 +30,8 @@ ImmutableSet testCollectionIsEmpty() { !ImmutableSet.of(6).isEmpty(), ImmutableSet.of(7).isEmpty(), ImmutableSet.of(8).isEmpty(), - ImmutableSet.of(9).isEmpty()); + ImmutableSet.of(9).isEmpty(), + ImmutableSet.of(10).isEmpty()); } ImmutableSet testCollectionSize() {