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 11, 2022
1 parent 7edae10 commit 434d66d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
9 changes: 0 additions & 9 deletions .ci/pitest-suppressions/pitest-coding-1-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,4 @@
<description>replaced call to com/puppycrawl/tools/checkstyle/api/DetailAST::getNextSibling with receiver</description>
<lineContent>DetailAST exprBetweenBrackets = openingBracket.getNextSibling();</lineContent>
</mutation>

<mutation unstable="false">
<sourceFile>VariableDeclarationUsageDistanceCheck.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck</mutatedClass>
<mutatedMethod>lambda$getFirstCaseGroupOrSwitchRule$2</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.experimental.NakedReceiverMutator</mutator>
<description>replaced call to com/puppycrawl/tools/checkstyle/api/DetailAST::findFirstToken with receiver</description>
<lineContent>.orElseGet(() -&gt; block.findFirstToken(TokenTypes.SWITCH_RULE));</lineContent>
</mutation>
</suppressedMutations>
Original file line number Diff line number Diff line change
Expand Up @@ -814,13 +814,15 @@ private static DetailAST getFirstNodeInsideSwitchBlock(
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) {
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 @@ -314,6 +314,7 @@ public void testVariableDeclarationUsageDistanceSwitchExpressions2() throws Exce
"78:17: " + getCheckMessage(MSG_KEY, "m", 3, maxDistance),
"79:17: " + getCheckMessage(MSG_KEY, "n", 2, maxDistance),
"100:17: " + getCheckMessage(MSG_KEY, "count", 3, maxDistance),
"110:9: " + getCheckMessage(MSG_KEY, "i", 2, maxDistance),
};

final String filename = "InputVariableDeclarationUsageDistanceCheckSwitchExpressions2.java";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,15 @@ public boolean equals(Object obj) {
}
return false;
}

void issue11973() {
int i = -1; // violation 'Distance between.*'i'.*and.*first usage is 2, but allowed 1.'
int x = -1;
switch (i) {
case 1 -> {
x++;
i++;
}
}
}
}
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 434d66d

Please sign in to comment.