Skip to content

MinimumSwitchCases does not redeclare variables in new blocks #687

Closed

Description

@arthurvl

Suppose we have a switch case:

switch (someInt) {
    case 1:
            String data = getSomeString();
            doThingOneWith(data);
            break;
    case 2:
            data = getSomeOtherString();
            doThingTwoWith(data);
            break;
}

That will currently be rewritten by MinimumSwitchCases to

if (someInt == 1) {
        String data = getSomeString();
        doThingOneWith(data);
} else if (someInt == 2) {
        data = getSomeOtherString();
        doThingTwoWith(data);
}

which does not compile due to data not being declared in the else block's scope.

Activity

  1. added
    bug
    on Mar 1, 2023
  2. moved this to Backlog in OpenRewriteon Mar 1, 2023
  3. timtebeek commented on Mar 1, 2023

    @timtebeek
    Member

    Interesting case; at first glance I was surprised the first one compiles despite the break statement. Thank you for reporting this issue!

    Replicated with the following test, which should fit right into MinimumSwitchCasesTest.java

    @Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/687")
    @Test
    void variableResuseBetweenSwitchCases() {
        rewriteRun(
          java(
            """
              class Snippet {
                  void snippet(int someInt) {
                      switch (someInt) {
                          case 1:
                              String data = getSomeString();
                              doThingOneWith(data);
                              break;
                          case 2:
                              data = getSomeOtherString();
                              doThingTwoWith(data);
                              break;
                      }
                  }
                
                  private String getSomeString() {
                      return null;
                  }
                
                  private String getSomeOtherString() {
                      return null;
                  }
                
                  private void doThingOneWith(String data) {
                  }
                
                  private void doThingTwoWith(String data) {
                  }
              }
              """,
            """
              class Snippet {
                  void snippet(int someInt) {
                      if (someInt == 1) {
                          String data = getSomeString();
                          doThingOneWith(data);
                      } else if (someInt == 2) {
                          String data = getSomeOtherString();
                          doThingTwoWith(data);
                      }
                  }
                
                  private String getSomeString() {
                      return null;
                  }
                
                  private String getSomeOtherString() {
                      return null;
                  }
                
                  private void doThingOneWith(String data) {
                  }
                
                  private void doThingTwoWith(String data) {
                  }
              }
              """
          )
        );
    }
  4. added
    bug
    on Aug 5, 2025
  5. changed the title [-]MinimumSwitchCases does not redeclare variables in new blocks[/-] [+]`MinimumSwitchCases` does not redeclare variables in new blocks[/+] on Aug 5, 2025
  6. self-assigned this
    on Aug 5, 2025
  7. added 2 commits that reference this issue on Aug 5, 2025
    baf7b11
    ca643b8
  8. moved this from Backlog to Done in OpenRewriteon Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugtest provided

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions