Skip to content

Commit

Permalink
Suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 authored and rickie committed Feb 15, 2024
1 parent 9b9b9ff commit 095e3f1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
* <p>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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,21 @@ private CollectionRules() {}
*/
static final class CollectionIsEmpty<T> {
@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<T> 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
Expand Down Expand Up @@ -338,7 +345,9 @@ Iterator<T> after(ImmutableCollection<T> 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<T> {
@BeforeTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ ImmutableSet<Boolean> 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<Integer> testCollectionSize() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ ImmutableSet<Boolean> 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<Integer> testCollectionSize() {
Expand Down

0 comments on commit 095e3f1

Please sign in to comment.