Skip to content

EqualsAvoidsNull not idempotent #472

Closed
@protocol7

Description

@protocol7

What version of OpenRewrite are you using?

  • org.openrewrite:rewrite-core:8.47.2
  • org.openrewrite.recipe:rewrite-static-analysis:2.3.0

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

EqualsAvoidsNull will non-idempotently swap places of constants and string literals. We see this in our daily runs of the recipe in that the same code will switch back and forth every other day. The test case below is a minimised version of our real code.

I assume that the preferred order would be to have the string literal first.

  @Test
  void equalsAvoidsNullNonIdempotent() {
    rewriteRun(
        spec -> spec.recipe(new EqualsAvoidsNull()),
        // language=java
        java(
            """
            package com.helloworld;

            public class Foo {
                private static final String FOO = "";

                public void foo() {
                    FOO.equals("");
                    "".equals(FOO);
                }
            }
            """));
  }
org.opentest4j.AssertionFailedError: [Expected recipe to complete in 0 cycles, but took at least one more cycle. Between the last two executed cycles there were changes to "com/helloworld/Foo.java"]
expected:
  "package com.helloworld;

  public class Foo {
      private static final String FOO = "";

      public void foo() {
          FOO.equals("");
          "".equals(FOO);
      }
  }"
 but was:
  "package com.helloworld;

  public class Foo {
      private static final String FOO = "";

      public void foo() {
          "".equals(FOO);
          FOO.equals("");
      }
  }"

Metadata

Metadata

Assignees

No one assigned

    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