Skip to content

Commit

Permalink
Suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Jul 14, 2024
1 parent 982837e commit 3227949
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ final class EqualityRules {
private EqualityRules() {}

/** Prefer reference-based equality for enums. */
// Primitive value comparisons are not listed, because Error Prone flags those out of the box.
static final class PrimitiveOrReferenceEquality<T extends Enum<T>> {
// Primitive value comparisons are not matched, because Error Prone flags those out of the box.
static final class EnumReferenceEquality<T extends Enum<T>> {
/**
* Enums can be compared by reference. It is safe to do so even in the face of refactorings,
* because if the type is ever converted to a non-enum, then Error-Prone will complain about any
Expand All @@ -44,16 +44,16 @@ boolean after(T a, T b) {
}
}

/** Prefer reference-based equality for enums over more contrived equality checks. */
static final class EnumsReferenceEqualityLambda<T extends Enum<T>> {
/** Prefer reference-based equality for enums. */
static final class EnumReferenceEqualityLambda<T extends Enum<T>> {
@BeforeTemplate
Predicate<T> before(T a) {
return Refaster.anyOf(isEqual(a), a::equals);
Predicate<T> before(T e) {
return Refaster.anyOf(isEqual(e), e::equals);
}

@AfterTemplate
Predicate<T> after(T a) {
return v -> v == a;
Predicate<T> after(T e) {
return v -> v == e;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(Objects.class, Optional.class, isEqual(null), not(null));
}

ImmutableSet<Boolean> testPrimitiveOrReferenceEquality() {
ImmutableSet<Boolean> testEnumReferenceEquality() {
return ImmutableSet.of(
RoundingMode.UP.equals(RoundingMode.DOWN),
Objects.equals(RoundingMode.UP, RoundingMode.DOWN),
Expand All @@ -28,18 +28,16 @@ ImmutableSet<Boolean> testPrimitiveOrReferenceEquality() {
RoundingMode.UP.ordinal() != RoundingMode.DOWN.ordinal());
}

ImmutableSet<Predicate<RoundingMode>> testEnumReferenceEqualityLambda() {
return ImmutableSet.of(isEqual(RoundingMode.DOWN), RoundingMode.UP::equals);
}

boolean testEqualsPredicate() {
// XXX: When boxing is involved this rule seems to break. Example:
// Stream.of(1).anyMatch(e -> Integer.MIN_VALUE.equals(e));
return Stream.of("foo").anyMatch(s -> "bar".equals(s));
}

ImmutableSet<Boolean> testEnumsReferenceEqualityLambda() {
return ImmutableSet.of(
Stream.of(RoundingMode.UP).anyMatch(isEqual(RoundingMode.DOWN)),
Stream.of(RoundingMode.UP).anyMatch(RoundingMode.DOWN::equals));
}

boolean testDoubleNegation() {
return !!Boolean.TRUE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(Objects.class, Optional.class, isEqual(null), not(null));
}

ImmutableSet<Boolean> testPrimitiveOrReferenceEquality() {
ImmutableSet<Boolean> testEnumReferenceEquality() {
return ImmutableSet.of(
RoundingMode.UP == RoundingMode.DOWN,
RoundingMode.UP == RoundingMode.DOWN,
Expand All @@ -28,18 +28,16 @@ ImmutableSet<Boolean> testPrimitiveOrReferenceEquality() {
RoundingMode.UP != RoundingMode.DOWN);
}

ImmutableSet<Predicate<RoundingMode>> testEnumReferenceEqualityLambda() {
return ImmutableSet.of(v -> v == RoundingMode.DOWN, v -> v == RoundingMode.UP);
}

boolean testEqualsPredicate() {
// XXX: When boxing is involved this rule seems to break. Example:
// Stream.of(1).anyMatch(e -> Integer.MIN_VALUE.equals(e));
return Stream.of("foo").anyMatch("bar"::equals);
}

ImmutableSet<Boolean> testEnumsReferenceEqualityLambda() {
return ImmutableSet.of(
Stream.of(RoundingMode.UP).anyMatch(v -> v == RoundingMode.DOWN),
Stream.of(RoundingMode.UP).anyMatch(v -> v == RoundingMode.DOWN));
}

boolean testDoubleNegation() {
return Boolean.TRUE;
}
Expand Down

0 comments on commit 3227949

Please sign in to comment.