Skip to main content Link Search Menu Expand Document (external link)

EqualityRules

SUGGESTION

Simplification

View source code on GitHub

Suppression

Disable all rules by adding -XepOpt:Refaster:NamePattern=^(?!(EqualityRules.*)) as compiler argument.

Table of contents
  1. PrimitiveOrReferenceEquality
  2. EqualsPredicate
  3. DoubleNegation
  4. Negation
  5. IndirectDoubleNegation

PrimitiveOrReferenceEquality

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("PrimitiveOrReferenceEquality") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(EqualityRules\.PrimitiveOrReferenceEquality)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 ImmutableSet<Boolean> testPrimitiveOrReferenceEquality() {
     return ImmutableSet.of(
-        RoundingMode.UP.equals(RoundingMode.DOWN),
-        Objects.equals(RoundingMode.UP, RoundingMode.DOWN),
-        !RoundingMode.UP.equals(RoundingMode.DOWN),
-        !Objects.equals(RoundingMode.UP, RoundingMode.DOWN));
+        RoundingMode.UP == RoundingMode.DOWN,
+        RoundingMode.UP == RoundingMode.DOWN,
+        RoundingMode.UP != RoundingMode.DOWN,
+        RoundingMode.UP != RoundingMode.DOWN);
   }

EqualsPredicate

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("EqualsPredicate") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(EqualityRules\.EqualsPredicate)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 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));
+    return Stream.of("foo").anyMatch("bar"::equals);
   }

DoubleNegation

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("DoubleNegation") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(EqualityRules\.DoubleNegation)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 boolean testDoubleNegation() {
-    return !!Boolean.TRUE;
+    return Boolean.TRUE;
   }

Negation

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("Negation") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(EqualityRules\.Negation)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 @SuppressWarnings("SimplifyBooleanExpression")
   ImmutableSet<Boolean> testNegation() {
     return ImmutableSet.of(
-        true ? !false : false,
-        !(true == false),
-        !((byte) 3 == (byte) 4),
-        !((char) 3 == (char) 4),
-        !((short) 3 == (short) 4),
-        !(3 == 4),
-        !(3L == 4L),
-        !(3F == 4F),
-        !(3.0 == 4.0),
-        !(BoundType.OPEN == BoundType.CLOSED));
+        true != false,
+        true != false,
+        (byte) 3 != (byte) 4,
+        (char) 3 != (char) 4,
+        (short) 3 != (short) 4,
+        3 != 4,
+        3L != 4L,
+        3F != 4F,
+        3.0 != 4.0,
+        BoundType.OPEN != BoundType.CLOSED);
   }

IndirectDoubleNegation

SUGGESTION

Simplification

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("IndirectDoubleNegation") to the enclosing element.

Disable this rule by adding -XepOpt:Refaster:NamePattern=^(?!(EqualityRules\.IndirectDoubleNegation)) as compiler argument.

Samples

Shows the difference in example code before and after the Refaster rule is applied.

 @SuppressWarnings("SimplifyBooleanExpression")
   ImmutableSet<Boolean> testIndirectDoubleNegation() {
     return ImmutableSet.of(
-        true ? false : !false,
-        !(true != false),
-        !((byte) 3 != (byte) 4),
-        !((char) 3 != (char) 4),
-        !((short) 3 != (short) 4),
-        !(3 != 4),
-        !(3L != 4L),
-        !(3F != 4F),
-        !(3.0 != 4.0),
-        !(BoundType.OPEN != BoundType.CLOSED));
+        true == false,
+        true == false,
+        (byte) 3 == (byte) 4,
+        (char) 3 == (char) 4,
+        (short) 3 == (short) 4,
+        3 == 4,
+        3L == 4L,
+        3F == 4F,
+        3.0 == 4.0,
+        BoundType.OPEN == BoundType.CLOSED);
   }

Copyright © 2017-2023 Picnic Technologies BV