Skip to content

FallThrough behaves weirdly for switch expressions inside switch statements #14

Closed

Description

@SplotyCode

Given the following code:

public class Reproducer {
  void statement() {

  }

  void test() {
    switch (2) {
      case 0:
        switch (3) {
          case 1 -> statement();
          case 3 -> {
            statement();
            statement();
          }
        }
      case 2:
        statement();
        break;
    }
  }
} 

Rewrite adds 3 break statements:

public class Reproducer {
  void statement() {

  }

  void test() {
    switch (2) {
      case 0:
      switch (3) {
        case 1 -> statement();
        case 3 -> {
          statement();
          statement();
          break; (1)
        }
        break; (2)
      }
      break; (3)
      case 2:
        statement();
        break;
    }
  }
}

I labeled the added breaks in brackets

  • Break 1: unnecessary as it is in a switch expression and not statement (in normal switch expressions it will not add this)
  • Break 2: Does not compile "Statement must be prepended with case label"
  • Break 3: I guess this one is correct. I believe FallThrough like this should never have been a thing in java, but i dont like that a cleanup task drastically changes logic (as a User i wonder now if cleanup it also changed logic somewere else?)

In this example rewrite does nothing (which is correct) just wanted to point out that the Problem seems to be using switch expressions in switch statements.

void doesWork() {
  switch (2) {
    case 0 -> statement();
    case 1 -> {
      statement();
      statement();
    }
  }
}

Activity

  1. added
    bug
    on Feb 20, 2023
  2. added this to the milestone on May 30, 2023
  3. removed this from the milestone on Nov 30, 2023
  4. added a commit that references this issue on Apr 9, 2026
  5. 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

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions