rewrite-static-analysis icon indicating copy to clipboard operation
rewrite-static-analysis copied to clipboard

`MinimumSwitchCases` does not redeclare variables in new blocks

Open arthurvl opened this issue 2 years ago • 1 comments

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.

arthurvl avatar Mar 01 '23 09:03 arthurvl

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) {
              }
          }
          """
      )
    );
}

timtebeek avatar Mar 01 '23 09:03 timtebeek