Skip to content

Commit

Permalink
Issue #3655: Fix NPE in NeedBraces on single line default stmt
Browse files Browse the repository at this point in the history
  • Loading branch information
liscju committed Dec 21, 2016
1 parent d24405c commit a882e6a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
Expand Up @@ -442,9 +442,14 @@ private static boolean isSingleLineCase(DetailAST literalCase) {
private static boolean isSingleLineDefault(DetailAST literalDefault) {
boolean result = false;
final DetailAST slist = literalDefault.getNextSibling();
final DetailAST block = slist.getFirstChild();
if (block.getType() != TokenTypes.SLIST) {
result = literalDefault.getLineNo() == block.getLineNo();
if (slist == null) {
result = true;
}
else {
final DetailAST block = slist.getFirstChild();
if (block.getType() != TokenTypes.SLIST) {
result = literalDefault.getLineNo() == block.getLineNo();
}
}
return result;
}
Expand Down
Expand Up @@ -178,4 +178,14 @@ public void testAllowEmptyLoopBodyFalse() throws Exception {
};
verify(checkConfig, getPath("InputNeedBracesNoBodyLoops.java"), expected);
}

@Test
public void testEmptySingleLineDefaultStmt() throws Exception {
final DefaultConfiguration checkConfig = createCheckConfig(NeedBracesCheck.class);
checkConfig.addAttribute("tokens", "LITERAL_DEFAULT");
checkConfig.addAttribute("allowSingleLineStatement", "true");
final String[] expected = {
};
verify(checkConfig, getPath("InputNeedBracesEmptyDefault.java"), expected);
}
}
@@ -0,0 +1,9 @@
package com.puppycrawl.tools.checkstyle.checks.blocks;

public class InputNeedBracesEmptyDefault {
private static void main() {
switch (5) {
default:
}
}
}

0 comments on commit a882e6a

Please sign in to comment.