Skip to content

FallThrough behaves weirdly for switch expressions inside switch statements #14

@SplotyCode

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();
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions