diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/EqualityRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/EqualityRules.java index f96c489164..6f5da60794 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/EqualityRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/EqualityRules.java @@ -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> { + // Primitive value comparisons are not matched, because Error Prone flags those out of the box. + static final class EnumReferenceEquality> { /** * 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 @@ -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> { + /** Prefer reference-based equality for enums. */ + static final class EnumReferenceEqualityLambda> { @BeforeTemplate - Predicate before(T a) { - return Refaster.anyOf(isEqual(a), a::equals); + Predicate before(T e) { + return Refaster.anyOf(isEqual(e), e::equals); } @AfterTemplate - Predicate after(T a) { - return v -> v == a; + Predicate after(T e) { + return v -> v == e; } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/EqualityRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/EqualityRulesTestInput.java index c2cd7335bf..986dabad17 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/EqualityRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/EqualityRulesTestInput.java @@ -18,7 +18,7 @@ public ImmutableSet elidedTypesAndStaticImports() { return ImmutableSet.of(Objects.class, Optional.class, isEqual(null), not(null)); } - ImmutableSet testPrimitiveOrReferenceEquality() { + ImmutableSet testEnumReferenceEquality() { return ImmutableSet.of( RoundingMode.UP.equals(RoundingMode.DOWN), Objects.equals(RoundingMode.UP, RoundingMode.DOWN), @@ -28,18 +28,16 @@ ImmutableSet testPrimitiveOrReferenceEquality() { RoundingMode.UP.ordinal() != RoundingMode.DOWN.ordinal()); } + ImmutableSet> 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 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; } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/EqualityRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/EqualityRulesTestOutput.java index 32af82fbe8..b89decfb0d 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/EqualityRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/EqualityRulesTestOutput.java @@ -18,7 +18,7 @@ public ImmutableSet elidedTypesAndStaticImports() { return ImmutableSet.of(Objects.class, Optional.class, isEqual(null), not(null)); } - ImmutableSet testPrimitiveOrReferenceEquality() { + ImmutableSet testEnumReferenceEquality() { return ImmutableSet.of( RoundingMode.UP == RoundingMode.DOWN, RoundingMode.UP == RoundingMode.DOWN, @@ -28,18 +28,16 @@ ImmutableSet testPrimitiveOrReferenceEquality() { RoundingMode.UP != RoundingMode.DOWN); } + ImmutableSet> 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 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; }