Skip to content

CompareEnumsWithEqualityOperator needs parenthesis in some boolean expressions #657

@protocol7

Description

@protocol7

What version of OpenRewrite are you using?

  • org.openrewrite:rewrite-core:8.58.0
  • org.openrewrite.recipe:rewrite-static-analysis:2.12.0

What is the smallest, simplest way to reproduce the problem?

In some boolean expressions, the output of CompareEnumsWithEqualityOperator needs to be wrapped in parentheses to ensure that the code produced is valid. #3 is another example of this, and here another one found in our code base.

  @Test
  public void enumEqual() {
    rewriteRun(
        spec -> spec.recipe(new CompareEnumsWithEqualityOperator()),
        java(
            """
            package com.helloworld;

            import java.time.DayOfWeek;

            public static class Foo {
              boolean foo = true == DayOfWeek.MONDAY.equals(DayOfWeek.TUESDAY);
            }
            """, """
            package com.helloworld;

            import java.time.DayOfWeek;

            public static class Foo {
              boolean foo = true == (DayOfWeek.MONDAY == DayOfWeek.TUESDAY);
            }
            """));
  }
org.opentest4j.AssertionFailedError: [Unexpected result in "com/helloworld/Foo.java":
diff --git a/com/helloworld/Foo.java b/com/helloworld/Foo.java
index daf2906..11a6884 100644
--- a/com/helloworld/Foo.java
+++ b/com/helloworld/Foo.java
@@ -3,5 +3,5 @@
 import java.time.DayOfWeek;

 public static class Foo {
-  boolean foo = true == (DayOfWeek.MONDAY == DayOfWeek.TUESDAY);
+  boolean foo = true == DayOfWeek.MONDAY == DayOfWeek.TUESDAY;
 }
\ No newline at end of file
]
expected:
  "package com.helloworld;

  import java.time.DayOfWeek;

  public static class Foo {
    boolean foo = true == (DayOfWeek.MONDAY == DayOfWeek.TUESDAY);
  }"
 but was:
  "package com.helloworld;

  import java.time.DayOfWeek;

  public static class Foo {
    boolean foo = true == DayOfWeek.MONDAY == DayOfWeek.TUESDAY;
  }"
	at org.openrewrite.test.RewriteTest.assertContentEquals(RewriteTest.java:622)
	at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:511)
	at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:130)

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions