Permalink
Browse files

Issue #4159: LITERAL_DEFAULT is now processed by EmptyBlockCheck. Cor…

…responding UTs added
  • Loading branch information...
ps-sp committed Apr 11, 2017
1 parent 4cef3f9 commit 47487004118aec2324ab35c8d381e862b29a01bf
@@ -238,7 +238,7 @@ private static boolean checkIsAllLinesAreWhitespace(String[] lines, int lineFrom
private static DetailAST findLeftCurly(DetailAST ast) {
final DetailAST leftCurly;
final DetailAST slistAST = ast.findFirstToken(TokenTypes.SLIST);
if (ast.getType() == TokenTypes.LITERAL_CASE
if (ast.getParent().getType() == TokenTypes.CASE_GROUP

This comment has been minimized.

Show comment
Hide comment
@romani

romani Apr 11, 2017

Please do exact check for default literal. To avoid any problem with weird user code or any future extension of switch syntax . Be exact.

@romani

romani Apr 11, 2017

Please do exact check for default literal. To avoid any problem with weird user code or any future extension of switch syntax . Be exact.

&& ast.getNextSibling() != null
&& ast.getNextSibling().getFirstChild().getType() == TokenTypes.SLIST) {
leftCurly = ast.getNextSibling().getFirstChild();
@@ -189,4 +189,39 @@ public void testForbidCaseWithoutStmt() throws Exception {
};
verify(checkConfig, getPath("InputEmptyCase.java"), expected);
}
@Test
public void testAllowEmptyDefaultWithText() throws Exception {
final DefaultConfiguration checkConfig = createCheckConfig(EmptyBlockCheck.class);
checkConfig.addAttribute("option", BlockOption.TEXT.toString());
checkConfig.addAttribute("tokens", "LITERAL_DEFAULT");
final String[] expected = {
"5:30: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
"11:13: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
"36:22: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
"44:47: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
"50:22: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
"78:13: " + getCheckMessage(MSG_KEY_BLOCK_EMPTY, "default"),
};
verify(checkConfig, getPath("InputEmptyDefault.java"), expected);
}
@Test
public void testForbidDefaultWithoutStatement() throws Exception {
final DefaultConfiguration checkConfig = createCheckConfig(EmptyBlockCheck.class);
checkConfig.addAttribute("option", BlockOption.STMT.toString());
checkConfig.addAttribute("tokens", "LITERAL_DEFAULT");
final String[] expected = {
"5:30: " + getCheckMessage(MSG_KEY_BLOCK_NO_STMT, "default"),
"11:13: " + getCheckMessage(MSG_KEY_BLOCK_NO_STMT, "default"),
"15:13: " + getCheckMessage(MSG_KEY_BLOCK_NO_STMT, "default"),
"26:30: " + getCheckMessage(MSG_KEY_BLOCK_NO_STMT, "default"),
"36:22: " + getCheckMessage(MSG_KEY_BLOCK_NO_STMT, "default"),
"44:47: " + getCheckMessage(MSG_KEY_BLOCK_NO_STMT, "default"),
"50:22: " + getCheckMessage(MSG_KEY_BLOCK_NO_STMT, "default"),
"65:22: " + getCheckMessage(MSG_KEY_BLOCK_NO_STMT, "default"),
"78:13: " + getCheckMessage(MSG_KEY_BLOCK_NO_STMT, "default"),
};
verify(checkConfig, getPath("InputEmptyDefault.java"), expected);
}
}
@@ -0,0 +1,89 @@
public class InputEmptyDefault {
void method1(int a) {
switch (a) {}
switch (a) {default: ; } // no violation
switch (a) {default: {}} // violation
switch (a) {
default:
}
switch (a) {
default: // violation
{}
}
switch (a) {
default: // violation if checking statements
{ // text
}
}
}
void method2(int a) {
switch (a) {
case 1:a++;
case 2:a++;
default: // no violation
switch (a) {
default: { // violation if checking for statements
}
}
}
}
void method3(int a, int b) {
switch (a) {
case 1: break;
default: {} method2(a); // violation
}
switch (b) {
case 2: break;
default: method2(b); {} // no violation
}
switch (a+b) {case 1: break; default: {} ; } // violation
}
void method4(int a, int b) {
switch (a) {
case 1:
default: {} // violation
}
switch (b) {
case 1:
default: // no violation
}
switch (a+b) {
default: // no violation
case 1: { }
}
switch (a-b) {
case 1:
default: { // violation if checking statements
} ;
case 2: { }
}
}
void method5(int a, int b) {
switch (a) {
case 1:
case 2:
case 3:
default: // violation
{
}
}
switch (b) {
default: // no violation
case 1:
case 2: { } method2(b);
case 3:
}
}
}

0 comments on commit 4748700

Please sign in to comment.