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 Aug 11, 2022
1 parent af99c7d commit 83e8eb9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -824,14 +824,16 @@ private static DetailAST getFirstNodeInsideSwitchBlock(
final List<DetailAST> variableUsageExpressions =
new ArrayList<>();

// Checking variable usage inside all CASE_GROUP and SWITCH_RULE ast's.
TokenUtil.forEachChild(block, currentNode.getType(), node -> {
final DetailAST lastNodeInCaseGroup =
node.getLastChild();
if (isChild(lastNodeInCaseGroup, variable)) {
variableUsageExpressions.add(lastNodeInCaseGroup);
}
});
if (currentNode != null) {
// Checking variable usage inside all CASE_GROUP and SWITCH_RULE ast's.
TokenUtil.forEachChild(block, currentNode.getType(), node -> {
final DetailAST lastNodeInCaseGroup =
node.getLastChild();
if (isChild(lastNodeInCaseGroup, variable)) {
variableUsageExpressions.add(lastNodeInCaseGroup);
}
});
}

// If variable usage exists in several related blocks, then
// firstNodeInsideBlock = null, otherwise if variable usage exists
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1071,4 +1071,10 @@ private void n() {
New6.field = (int)a;
}

class SwitchStatement {
void issue11973() {
int i = -1;
switch (i) { } // ok
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1070,4 +1070,11 @@ private void n() {
New1.field = 3;
New1.field = (int)a;
}

class SwitchStatement {
void issue11973() {
int i = -1;
switch (i) { } // ok
}
}
}

0 comments on commit 83e8eb9

Please sign in to comment.