Back to Intellij Community

EnhancedSwitchMigration

java/java-impl/resources/inspectionDescriptions/EnhancedSwitchMigration.html

2025.3-rc-21.0 KB
Original Source

Reports switch statements that can be automatically replaced with enhanced switch statements or expressions.

Example:

double getPrice(String fruit) {
    // Switch statement can be replaced with enhanced 'switch'
    switch (fruit) {
      case "Apple":
        return 1.0;
      case "Orange":
        return 1.5;
      case "Mango":
        return 2.0;
      default:
        throw new IllegalArgumentException();
    }
  }

After the quick-fix is applied:

double getPrice(String fruit) {
    return switch (fruit) {
      case "Apple" -> 1.0;
      case "Orange" -> 1.5;
      case "Mango" -> 2.0;
      default -> throw new IllegalArgumentException();
    };
  }
  • Use the Show warning only if conversion to expression is possible option not to warn about conversion to switch statement.
  • Use the Maximum number of statements in one branch to convert to switch expression option warn about conversion to expression only if each branch has less than the given number of statements.

New in 2019.1