All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.java.rules.java.S6205.html Maven / Gradle / Ivy

There is a newer version: 8.10.0.38194
Show newest version

Why is this an issue?

In Switch Expressions, an arrow label consisting of a block with a single yield can be simplified to directly return the value, resulting in cleaner code.

Similarly, for Switch Statements and arrow labels, a break in a block is always redundant and should not be used. Furthermore, if the resulting block contains only one statement, the curly braces of that block can also be omitted.

This rule reports an issue when a case of a Switch Expression contains a block with a single yield or when a Switch Statement contains a block with a break.

Noncompliant code example

int i = switch (mode) {
  case "a" -> {        // Noncompliant: Remove the redundant block and yield.
    yield 1;
  }
  default -> {         // Noncompliant: Remove the redundant block and yield.
    yield 2;
  }
};

switch (mode) {
  case "a" -> {        // Noncompliant: Remove the redundant block and break.
    result = 1;
    break;
  }
  default -> {         // Noncompliant: Remove the redundant break.
    doSomethingElse();
    result = 2;
    break;
  }
}

Compliant solution

int i = switch (mode) {
  case "a" -> 1;
  default -> 2;
};

switch (mode) {
  case "a" -> result = 1;
  default -> {
   doSomethingElse();
   result = 2;
 }
}

Resources





© 2015 - 2025 Weber Informatics LLC | Privacy Policy