Skip to content

Commit

Permalink
Issue #11973: Fix NPE on empty switch statements in VariableDeclarati…
Browse files Browse the repository at this point in the history
…onUsageDistance
  • Loading branch information
stoyanK7 committed Sep 9, 2022
1 parent 9f974c0 commit 3fc7939
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -810,11 +810,15 @@ private static DetailAST getFirstNodeInsideIfBlock(
private static DetailAST getFirstNodeInsideSwitchBlock(
DetailAST block, DetailAST variable) {
final DetailAST currentNode = getFirstCaseGroupOrSwitchRule(block);
int currentNodeType = -1;
if (currentNode != null) {
currentNodeType = currentNode.getType();
}

final List<DetailAST> variableUsageExpressions =
new ArrayList<>();

// Checking variable usage inside all CASE_GROUP and SWITCH_RULE ast's.
TokenUtil.forEachChild(block, currentNode.getType(), node -> {
TokenUtil.forEachChild(block, currentNodeType, node -> {
final DetailAST lastNodeInCaseGroup =
node.getLastChild();
if (isChild(lastNodeInCaseGroup, variable)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,10 @@ void method() throws Exception {
a.equals("");
}
}

void issue11973() {
int i = -1;
switch (i) {
}
}
}

0 comments on commit 3fc7939

Please sign in to comment.