Skip to content

Commit

Permalink
Issue #11712: Support yield in FallThrough
Browse files Browse the repository at this point in the history
  • Loading branch information
KENNYSOFT authored and strkkk committed Jun 10, 2022
1 parent 90e7a03 commit 07e5a44
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* <p>
* Checks for fall-through in {@code switch} statements.
* Finds locations where a {@code case} <b>contains</b> Java code but lacks a
* {@code break}, {@code return}, {@code throw} or {@code continue} statement.
* {@code break}, {@code return}, {@code yield}, {@code throw} or {@code continue} statement.
* </p>
* <p>
* The check honors special comments to suppress the warning.
Expand Down Expand Up @@ -77,7 +77,7 @@
* case 1:
* i++;
* case 2: // violation, previous case contains code but lacks
* // break, return, throw or continue statement
* // break, return, yield, throw or continue statement
* i++;
* break;
* case 3: // OK
Expand All @@ -97,6 +97,20 @@
* }
* }
* }
* public int bar() {
* int i = 0;
* return switch (i) {
* case 1:
* i++;
* case 2: // violation, previous case contains code but lacks
* // break, return, yield, throw or continue statement
* case 3: // OK, case does not contain code
* i++;
* yield 11;
* default: // OK
* yield -1;
* };
* }
* </pre>
* <p>
* Example how to suppress violations by comment:
Expand Down Expand Up @@ -279,6 +293,7 @@ private boolean isTerminated(final DetailAST ast, boolean useBreak,

switch (ast.getType()) {
case TokenTypes.LITERAL_RETURN:
case TokenTypes.LITERAL_YIELD:
case TokenTypes.LITERAL_THROW:
terminated = true;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ equals.noEquals=Wenn ''hashCode()'' definiert wird, muss auch ''equals()'' defin
equals.noHashCode=Wenn ''equals()'' definiert wird, muss auch ''hashCode()'' definiert werden.
equalsIgnoreCase.avoid.null=Bei einem equalsIgnoreCase()-Vergleich sollten String-Literale auf der linken Seite stehen.
explicit.init=Die Variable ''{0}'' wird explizit mit ''{1}'' initialisiert (was dem Standardwert ihres Typs entspricht).
fall.through=Fall-through vom vorherigen switch-Zweig ohne break/return/throw/continue-Anweisung.
fall.through.last=Fall-through vom letzten switch-Zweig ohne break/return/throw/continue-Anweisung.
fall.through=Fall-through vom vorherigen switch-Zweig ohne break/return/yield/throw/continue-Anweisung.
fall.through.last=Fall-through vom letzten switch-Zweig ohne break/return/yield/throw/continue-Anweisung.
final.variable=Die Variable ''{0}'' sollte als final deklariert werden.
hidden.field=Die Variable ''{0}'' verbirgt ein Feld.
illegal.catch=Die Exception ''{0}'' darf nicht abgefangen werden.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ equals.noEquals=La définition de la méthode ''hashCode()'' doit toujours être
equals.noHashCode=La définition de la méthode ''equals()'' doit toujours être accompagnée de la définition de la méthode ''hashCode()''.
equalsIgnoreCase.avoid.null=Les chaînes littérales devraient être sur le côté gauche d''une comparaison ''equalsIgnoreCase''.
explicit.init=L''initialisation explicite de la variable ''{0}'' à la valeur ''{1}'' est inutile, c''est la valeur par défaut pour ce type.
fall.through=Le cas précédent du "switch" ne contient pas de break, return, throw ou continue.
fall.through.last=Le dernier cas du "switch" ne contient pas de break, return, throw ou continue.
fall.through=Le cas précédent du "switch" ne contient pas de break, return, yield, throw ou continue.
fall.through.last=Le dernier cas du "switch" ne contient pas de break, return, yield, throw ou continue.
final.variable=La variable ''{0}'' devrait être finale.
hidden.field=''{0}'' masque un attribut.
illegal.catch=Catcher l''exception ''{0}'' est prohibé.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<description>&lt;p&gt;
Checks for fall-through in {@code switch} statements.
Finds locations where a {@code case} &lt;b&gt;contains&lt;/b&gt; Java code but lacks a
{@code break}, {@code return}, {@code throw} or {@code continue} statement.
{@code break}, {@code return}, {@code yield}, {@code throw} or {@code continue} statement.
&lt;/p&gt;
&lt;p&gt;
The check honors special comments to suppress the warning.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,14 @@ public void testFallThroughNoElse() throws Exception {
expected);
}

@Test
public void testYield() throws Exception {
final String[] expected = {
"19:9: " + getCheckMessage(MSG_FALL_THROUGH),
};
verifyWithInlineConfigParser(
getNonCompilablePath("InputFallThrough3.java"),
expected);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
FallThrough
checkLastCaseGroup = (default)false
reliefPattern = (default)falls?[ -]?thr(u|ough)
*/

//non-compiled with javac: Compilable with Java14
package com.puppycrawl.tools.checkstyle.checks.coding.fallthrough;

public class InputFallThrough3
{
int hasYield() {
int i = 0;
return switch (hashCode()) {
case 1:
i++;
case 2: // violation 'Fall through from previous branch of the switch statement.'
yield 2;
case 3: // ok
// fall through
case 4: // ok
default: // ok
yield -1;
};
}
}
18 changes: 16 additions & 2 deletions src/xdocs/config_coding.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1483,7 +1483,7 @@ public class Test {
Checks for fall-through in <code>switch</code>
statements. Finds locations where a <code>case</code>
<b>contains</b> Java code but lacks a <code>break</code>, <code>return</code>,
<code>throw</code> or <code>continue</code>
<code>yield</code>, <code>throw</code> or <code>continue</code>
statement.
</p>
<p>
Expand Down Expand Up @@ -1555,7 +1555,7 @@ public void foo() throws Exception {
case 1:
i++;
case 2: // violation, previous case contains code but lacks
// break, return, throw or continue statement
// break, return, yield, throw or continue statement
i++;
break;
case 3: // OK
Expand All @@ -1575,6 +1575,20 @@ public void foo() throws Exception {

}
}
}
public int bar() {
int i = 0;
return switch (i) {
case 1:
i++;
case 2: // violation, previous case contains code but lacks
// break, return, yield, throw or continue statement
case 3: // OK, case does not contain code
i++;
yield 11;
default: // OK
yield -1;
};
}
</source>
<p>
Expand Down

0 comments on commit 07e5a44

Please sign in to comment.