rspec/rules/S6205/java/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

79 lines
1.6 KiB
Plaintext

== 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
[source,java]
----
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
[source,java]
----
int i = switch (mode) {
case "a" -> 1;
default -> 2;
};
switch (mode) {
case "a" -> result = 1;
default -> {
doSomethingElse();
result = 2;
}
}
----
== Resources
* https://openjdk.java.net/jeps/361[JEP 361: Switch Expressions]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Remove the redundant [block and yield]/ [block and break]/[break].
=== Highlighting
"yield"|"break" keyword.
endif::env-github,rspecator-view[]