Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce EnumReferenceEqualityLambda Refaster rule #1239

Merged
merged 4 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.picnic.errorprone.refasterrules;

import static java.util.function.Predicate.isEqual;
import static java.util.function.Predicate.not;

import com.google.errorprone.annotations.CanIgnoreReturnValue;
Expand All @@ -19,9 +20,9 @@
final class EqualityRules {
private EqualityRules() {}

/** Prefer reference-based quality 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>> {
/** Prefer reference-based equality for enums. */
// 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 @@ -43,6 +44,19 @@ boolean after(T a, T b) {
}
}

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

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

/** Prefer {@link Object#equals(Object)} over the equivalent lambda function. */
// XXX: As it stands, this rule is a special case of what `MethodReferenceUsage` tries to achieve.
// If/when `MethodReferenceUsage` becomes production ready, we should simply drop this check.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.picnic.errorprone.refasterrules;

import static java.util.function.Predicate.isEqual;
import static java.util.function.Predicate.not;

import com.google.common.collect.BoundType;
Expand All @@ -14,10 +15,10 @@
final class EqualityRulesTest implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(Objects.class, Optional.class, not(null));
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 @@ -27,6 +28,10 @@ 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));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.picnic.errorprone.refasterrules;

import static java.util.function.Predicate.isEqual;
import static java.util.function.Predicate.not;

import com.google.common.collect.BoundType;
Expand All @@ -14,10 +15,10 @@
final class EqualityRulesTest implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(Objects.class, Optional.class, not(null));
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 @@ -27,6 +28,10 @@ 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));
Expand Down