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 78584f7 commit 40df68e
Show file tree
Hide file tree
Showing 5 changed files with 107 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 @@ -309,4 +309,18 @@ public void testVariableDeclarationUsageDistanceSwitchExpressions() throws Excep
verifyWithInlineConfigParser(getNonCompilablePath(filename), expected);
}

@Test
public void testVariableDeclarationUsageDistanceSwitchExpressions2() throws Exception {
final int maxDistance = 1;
final String[] expected = {
"16:9: " + getCheckMessage(MSG_KEY, "i", 2, maxDistance),
"37:9: " + getCheckMessage(MSG_KEY, "day", 3, maxDistance),
"48:9: " + getCheckMessage(MSG_KEY, "day1", 3, maxDistance),
"58:9: " + getCheckMessage(MSG_KEY, "day2", 4, maxDistance),
};

final String filename = "InputVariableDeclarationUsageDistanceCheckSwitchExpressions2.java";
verifyWithInlineConfigParser(getNonCompilablePath(filename), expected);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
VariableDeclarationUsageDistance
allowedDistance = 1
ignoreVariablePattern = (default)
validateBetweenScopes = true
ignoreFinal = false
*/

//non-compiled with javac: Compilable with Java14
package com.puppycrawl.tools.checkstyle.checks.coding.variabledeclarationusagedistance;

public class InputVariableDeclarationUsageDistanceCheckSwitchExpressions2 {
void issue11973() {
int i = -1; // violation 'Distance between.*'i'.*and.*first usage is 2, but allowed 1.'
int x = -1; // ok
switch (i) {
case 1 -> {
x++;
i++;
}
};

int a = -1; // ok
switch (a) {
case 1 -> 2;
case 2 -> a;
};

int b = -1; // ok
switch (b) {
case 1 -> b;
case 2 -> 1;
};

int day = 1; // violation 'Distance between.*'day'.*and.*first usage is 3, but allowed 1.'
int c = -1;
switch (c) {
case 1:
case 2:
case 3:
c++;
c--;
day++;
};

int day1 = 1; // violation 'Distance between.*'day1'.*and.*first usage is 3, but allowed 1.'
int cc = -1;
switch (cc) {
case 1 -> {
cc++;
cc--;
yield day1;
}
};

int day2 = 1; // violation 'Distance between.*'day2'.*and.*first usage is 4, but allowed 1.'
int ccc = -1;
switch (ccc) {
case 1 -> {
ccc++;
ccc--;
yield ccc;
}
case 2 -> {
yield 2;
}
case 3 -> {
ccc++;
ccc--;
ccc++;
day2++;
yield 3;
}
};
}
}
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 40df68e

Please sign in to comment.