Skip to content

Commit

Permalink
Issue checkstyle#14620: LITERAL_CASE token support in RightCurlyCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
mahfouz72 committed Mar 7, 2024
1 parent 12a18c9 commit 250cf93
Show file tree
Hide file tree
Showing 9 changed files with 614 additions and 4 deletions.
1 change: 1 addition & 0 deletions config/checkstyle-checks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@
<property name="tokens" value="ENUM_DEF"/>
<property name="tokens" value="RECORD_DEF"/>
<property name="tokens" value="COMPACT_CTOR_DEF"/>
<property name="tokens" value="LITERAL_CASE"/>
<property name="option" value="alone"/>
</module>
<module name="RightCurly">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
/**
* <p>
* Checks the placement of right curly braces (<code>'}'</code>) for code blocks. This check
* supports if-else, try-catch-finally blocks, switch statements, while-loops, for-loops,
* supports if-else, try-catch-finally blocks, switch statements, switch cases, while-loops, for-loops,
* method definitions, class definitions, constructor definitions,
* instance, static initialization blocks, annotation definitions and enum definitions.
* For right curly brace of expression blocks of arrays, lambdas and class instances
Expand Down Expand Up @@ -155,6 +155,7 @@ public int[] getAcceptableTokens() {
TokenTypes.RECORD_DEF,
TokenTypes.COMPACT_CTOR_DEF,
TokenTypes.LITERAL_SWITCH,
TokenTypes.LITERAL_CASE,
};
}

Expand Down Expand Up @@ -425,6 +426,9 @@ private static Details getDetails(DetailAST ast) {
case TokenTypes.LITERAL_SWITCH:
details = getDetailsForSwitch(ast);
break;
case TokenTypes.LITERAL_CASE:
details = getDetailsForCase(ast);
break;
default:
details = getDetailsForOthers(ast);
break;
Expand Down Expand Up @@ -453,6 +457,28 @@ private static Details getDetailsForSwitch(DetailAST switchNode) {
return new Details(lcurly, rcurly, nextToken, true);
}

/**
* Collects details about case statements.
*
* @param caseNode case statement to gather details about
* @return new Details about given case statement
*/
private static Details getDetailsForCase(DetailAST caseNode) {
final DetailAST caseParent = caseNode.getParent();
final int parentType = caseParent.getType();
final DetailAST ast = caseParent.findFirstToken(TokenTypes.SLIST);
DetailAST lcurly = (parentType == TokenTypes.CASE_GROUP) ? ast.getFirstChild() : ast;
DetailAST rcurly = null;
// case statement may have no braces but has SLIST token
if(lcurly != null && !lcurly.getText().equals("{")) {
lcurly = null;
}
if (lcurly != null) {
rcurly = lcurly.getLastChild();
}
return new Details(lcurly, rcurly, getNextToken(caseParent), true);
}

/**
* Check whether switch is expression or not.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
parent="com.puppycrawl.tools.checkstyle.TreeWalker">
<description>&lt;p&gt;
Checks the placement of right curly braces (&lt;code&gt;'}'&lt;/code&gt;) for code blocks. This check
supports if-else, try-catch-finally blocks, switch statements, while-loops, for-loops,
supports if-else, try-catch-finally blocks, switch statements, switch cases, while-loops, for-loops,
method definitions, class definitions, constructor definitions,
instance, static initialization blocks, annotation definitions and enum definitions.
For right curly brace of expression blocks of arrays, lambdas and class instances
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -821,4 +821,63 @@ public void testSwitchExpression7() throws Exception {
verifyWithInlineConfigParser(
getNonCompilablePath("InputRightCurlyTestSwitchExpression7.java"), expected);
}

@Test
public void testCaseBlocksInSwitchStatement1() throws Exception {
final String[] expected = {
"33:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"44:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"73:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"78:44: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 44),
"90:39: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 39),
"97:44: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 44),
"106:35: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 35),
"118:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"127:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"139:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"142:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"150:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"150:34: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 34),
"170:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"170:26: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 26),
};
verifyWithInlineConfigParser(
getPath("InputRightCurlyTestCaseBlocksInSwitchStatement.java"), expected);
}

@Test
public void testCaseBlocksInSwitchStatement2() throws Exception {
final String[] expected = {
"33:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"44:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"73:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"78:44: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 44),
"118:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"127:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"139:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"142:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"150:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"170:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
};
verifyWithInlineConfigParser(
getPath("InputRightCurlyTestCaseBlocksInSwitchStatement2.java"), expected);
}

@Test
public void testCaseBlocksInSwitchStatement3() throws Exception {
final String[] expected = {
"33:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"44:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"73:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"78:44: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 44),
"118:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"127:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"139:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"142:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"150:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"170:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
};
verifyWithInlineConfigParser(
getPath("InputRightCurlyTestCaseBlocksInSwitchStatement3.java"), expected);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
RightCurly
option = ALONE
tokens = LITERAL_CASE
*/
package com.puppycrawl.tools.checkstyle.checks.blocks.rightcurly;

public class InputRightCurlyTestCaseBlocksInSwitchStatement {
public static void test0() {
int mode = 0;
switch (mode) {
case 1: {
int x = 1;
break;
}
case 2: {
int x = 0;
break;
}
case 3: {
break;
}
}
}

public static void test() {
int mode = 0;
switch (mode) {
case 1:{
int x = 1;
break;
} default : // violation '}' at column 13 should be alone on a line'
int x = 0;
}
}

public static void test1() {
int k = 0;
switch (k) {
case 1:{
int x = 1;
break;
} case 2: // violation '}' at column 13 should be alone on a line'
int x = 2;
break;
}
}

public static void test2() {
int k = 0;
switch (k) {
case 1:{
int x = 1;
break;
}
case 2:
int x = 2;
break;
}
}

public static void test3() {
int k = 0;
switch (k) {
case 1:{
int x = 1;
break;
}
case 2:{
int x = 2;
break;
} } // violation '}' at column 13 should be alone on a line'
}

public static void test4() {
int mode = 0;
switch (mode) { case 0: {int x = 1;} break; default : int x = 5; }
// violation above '}' at column 44 should be alone on a line'
}

public static void test5() {
int mode = 0;
switch (mode) { case 0: int x = 1;}
}

public static void test6() {
int mode = 0;
switch (mode) {
case 0: {int x = 1; break;} // violation '}' at column 39 should be alone on a line'
default : break;
}
}

public static void test7() {
int mode = 0;
switch (mode) { case 0: {int x = 1;} // violation '}' at column 44 should be alone on a line'
break; default : break; }
}

public static void test8() {
int mode = 0;
switch (mode) {
case 0: int x = 1;
case 1: x = 1; break;
case 2: {x = 1; break;} // violation '}' at column 35 should be alone on a line'
default : x = 5;
}
}

public static void test9() {
int mode = 0;
switch (mode) {
case 0: int x = 1;
case 1: x = 1; break;
case 2: {
x = 1; break;
} default : x = 5; // violation '}' at column 13 should be alone on a line'
}
}

public static void test10() {
int mode = 0;
switch (mode) {
case 0: {

} case 1: int x = 1; break; // violation '}' at column 13 should be alone on a line'
case 2: {

}
default : x = 5;
}
}
public static void test11() {
int mode = 0;
switch (mode) {
case 0: {

} case 1: int x = 1; break; // violation '}' at column 13 should be alone on a line'
case 2: {

} default : x = 5; // violation '}' at column 13 should be alone on a line'
}
}
public static void test12() {
int mode = 0;
switch (mode) {
case 0: {

} case 1: {int x = 1;} break; // 2 violations
}
}
public static void test13() {
int mode = 0;
switch (mode) {
case 0: {

}
case 1: {

}
default : break;
}
}
public static void test14() {
int mode = 0;
switch (mode) {
case 0: {

} case 1: { } // 2 violations
default : {break;}
}
}
}

0 comments on commit 250cf93

Please sign in to comment.