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 10, 2024
1 parent 66115b8 commit 0d19af8
Show file tree
Hide file tree
Showing 23 changed files with 1,267 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6823,4 +6823,26 @@
method return type: @Initialized @NonNull NodeInfo
</details>
</checkerFrameworkError>

<checkerFrameworkError unstable="false">
<fileName>src/main/java/com/puppycrawl/tools/checkstyle/checks/blocks/RightCurlyCheck.java</fileName>
<specifier>argument</specifier>
<message>incompatible argument for parameter lcurly of Details constructor.</message>
<lineContent>return new Details(lcurly, rcurly, getNextToken(caseParent), true);</lineContent>
<details>
found : @Initialized @Nullable DetailAST
required: @Initialized @NonNull DetailAST
</details>
</checkerFrameworkError>

<checkerFrameworkError unstable="false">
<fileName>src/main/java/com/puppycrawl/tools/checkstyle/checks/blocks/RightCurlyCheck.java</fileName>
<specifier>argument</specifier>
<message>incompatible argument for parameter rcurly of Details constructor.</message>
<lineContent>return new Details(lcurly, rcurly, getNextToken(caseParent), true);</lineContent>
<details>
found : @Initialized @Nullable DetailAST
required: @Initialized @NonNull DetailAST
</details>
</checkerFrameworkError>
</suppressedErrors>
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 @@ -130,4 +130,19 @@ public void testRightCurlySwitch() throws Exception {
final Integer[] warnList = getLinesWithWarn(filePath);
verify(checkConfig, filePath, expected, warnList);
}

@Test
public void testRightCurlySwitchCases() throws Exception {
final String[] expected = {
"25:13: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 13),
"34:28: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 28),
"57:33: " + getCheckMessage(RightCurlyCheck.class, MSG_KEY_LINE_ALONE, "}", 33),
};

final Configuration checkConfig = createTreeWalkerConfig(getModuleConfigsByIds(MODULES));
final String filePath = getPath("InputRightCurlySwitchCasesBlocks.java");

final Integer[] warnList = getLinesWithWarn(filePath);
verify(checkConfig, filePath, expected, warnList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.google.checkstyle.test.chapter4formatting.rule412nonemptyblocks;

public class InputRightCurlySwitchCasesBlocks {

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

public static void test() {
int mode = 0;
switch (mode) {
case 1:{
int x = 1;
break;
} default : // warn
int x = 0;
}
}

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

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

public static void test3() {
int k = 0;
switch (k) {
case 1:{
int x = 1;
}
case 2: { int x = 2;} // warn
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Arrays;
import java.util.Locale;


import com.puppycrawl.tools.checkstyle.StatelessCheck;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
Expand All @@ -32,8 +33,8 @@
/**
* <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,
* method definitions, class definitions, constructor definitions,
* 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
* please follow issue
Expand Down Expand Up @@ -155,6 +156,7 @@ public int[] getAcceptableTokens() {
TokenTypes.RECORD_DEF,
TokenTypes.COMPACT_CTOR_DEF,
TokenTypes.LITERAL_SWITCH,
TokenTypes.LITERAL_CASE,
};
}

Expand Down Expand Up @@ -425,6 +427,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 +458,36 @@ 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 = ast;
if (parentType == TokenTypes.CASE_GROUP) {
if (ast != null && ast.getFirstChild().getType() == TokenTypes.SLIST) {
lcurly = ast.getFirstChild();
}
else {
lcurly = null;
}
}

if (isSwitchExpression(caseParent)) {
lcurly = null;
}
DetailAST rcurly = 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,8 +6,8 @@
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,
method definitions, class definitions, constructor definitions,
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
please follow issue
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/google_checks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
<property name="tokens"
value="CLASS_DEF, METHOD_DEF, CTOR_DEF, LITERAL_FOR, LITERAL_WHILE, STATIC_INIT,
INSTANCE_INIT, ANNOTATION_DEF, ENUM_DEF, INTERFACE_DEF, RECORD_DEF,
COMPACT_CTOR_DEF, LITERAL_SWITCH"/>
COMPACT_CTOR_DEF, LITERAL_SWITCH, LITERAL_CASE"/>
</module>
<module name="SuppressionXpathSingleFilter">
<!-- suppresion is required till https://github.com/checkstyle/checkstyle/issues/7541 -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -821,4 +821,150 @@ 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:42: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 42),
"107:35: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 35),
};
verifyWithInlineConfigParser(
getPath("InputRightCurlyTestCaseBlocksInSwitchStatement.java"), expected);
}

@Test
public void testCaseBlocksInSwitchStatement2() throws Exception {
final String[] expected = {
"17:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"26:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"38:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"41:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"49:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"49:34: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 34),
"69:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"69:26: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 26),
"76:34: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 34),
"76:47: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 47),
};
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),
};
verifyWithInlineConfigParser(
getPath("InputRightCurlyTestCaseBlocksInSwitchStatement3.java"), expected);
}

@Test
public void testCaseBlocksInSwitchStatement4() throws Exception {
final String[] expected = {
"18:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"27:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"39:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"42:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"50:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"70:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"77:34: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 34),
"77:47: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 47),
};
verifyWithInlineConfigParser(
getPath("InputRightCurlyTestCaseBlocksInSwitchStatement4.java"), expected);
}

@Test
public void testCaseBlocksInSwitchStatement5() 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),
};
verifyWithInlineConfigParser(
getPath("InputRightCurlyTestCaseBlocksInSwitchStatement5.java"), expected);
}

@Test
public void testCaseBlocksInSwitchStatement6() throws Exception {
final String[] expected = {
"18:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"27:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"39:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"42:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"50:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"70:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"77:34: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 34),
"77:47: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 47),
};
verifyWithInlineConfigParser(
getPath("InputRightCurlyTestCaseBlocksInSwitchStatement6.java"), expected);
}

@Test
public void testCaseBlocksWithSwitchRule1() throws Exception {
final String[] expected = {
"32:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"44:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"45:27: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 27),
"56:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"69:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"74:46: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 46),
"83:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"83:36: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 36),
"93:31: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 31),
"94:31: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 31),
};
verifyWithInlineConfigParser(
getNonCompilablePath("InputRightCurlyTestCaseBlocksWithSwitchRule.java"),
expected);
}

@Test
public void testCaseBlocksWithSwitchRule2() throws Exception {
final String[] expected = {
"41:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"53:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"54:27: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 27),
"65:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"78:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"83:46: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 46),
"92:13: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 13),
"103:31: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 31),
};
verifyWithInlineConfigParser(
getNonCompilablePath("InputRightCurlyTestCaseBlocksWithSwitchRule2.java"),
expected);
}

@Test
public void testCaseBlocksWithSwitchExpression() throws Exception {
final String[] expected = {
"63:31: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 31),
"87:42: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 42),
};
verifyWithInlineConfigParser(
getNonCompilablePath("InputRightCurlyTestCaseBlocksWithSwitchExpression.java"),
expected);
}

@Test
public void testCaseBlocksWithSwitchExpression2() throws Exception {
final String[] expected = {
"63:31: " + getCheckMessage(MSG_KEY_LINE_ALONE, "}", 31),
};
verifyWithInlineConfigParser(
getNonCompilablePath("InputRightCurlyTestCaseBlocksWithSwitchExpression2.java"),
expected);
}
}

0 comments on commit 0d19af8

Please sign in to comment.