RefactorSwitch
This switch can be refactored to be more readable

Severity
WARNING

The problem

This checker aims to improve the readability of new-style arrow switches by simplifying and refactoring them.

Simplifications:

Factoring out return switch:

enum SideOfCoin {OBVERSE, REVERSE};

private String renderName(SideOfCoin sideOfCoin) {
  switch(sideOfCoin) {
    case OBVERSE -> {
      return "Heads";
    }
    case REVERSE -> {
      return "Tails";
    }
  }
  // This should never happen, but removing this will cause a compile-time error
  throw new RuntimeException("Unknown side of coin");
}

The transformed code is simpler and elides the “should never happen” handler.

enum SideOfCoin {OBVERSE, REVERSE};

private String renderName(SideOfCoin sideOfCoin) {
  return switch(sideOfCoin) {
    case OBVERSE -> "Heads";
    case REVERSE -> "Tails";
  };
}

Factoring out assignment switch:

enum Suit {HEARTS, CLUBS, SPADES, DIAMONDS};

private void updateScore(Suit suit) {
  int score = 0;
  switch(suit) {
    case HEARTS, DIAMONDS -> {
      score = -1;
    }
    case SPADES -> {
      score = 2;
    }
    case CLUBS -> {
      score = 3;
    }
  }
}

Which can be consolidated:

enum Suit {HEARTS, CLUBS, SPADES, DIAMONDS};

private void updateScore(Suit suit) {
   int score = switch(suit) {
       case HEARTS, DIAMONDS -> -1;
       case SPADES -> 2;
       case CLUBS -> 3;
    };
}

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("RefactorSwitch") to the enclosing element.