Skip to content

Commit

Permalink
Issue checkstyle#11720: Kill surviving mutation in VariableDeclaratio…
Browse files Browse the repository at this point in the history
…nUsageDistanceCheck in getFirstNodeInsideIfBlock
  • Loading branch information
Vyom-Yadav committed Aug 10, 2022
1 parent 1c92749 commit 4bf25e9
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 72 deletions.
47 changes: 1 addition & 46 deletions .ci/pitest-suppressions/pitest-coding-1-suppressions.xml
Expand Up @@ -126,51 +126,6 @@
<lineContent>switch (examineNode.getType()) {</lineContent>
</mutation>

<mutation unstable="false">
<sourceFile>VariableDeclarationUsageDistanceCheck.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck</mutatedClass>
<mutatedMethod>getFirstNodeInsideIfBlock</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.experimental.NakedReceiverMutator</mutator>
<description>replaced call to com/puppycrawl/tools/checkstyle/api/DetailAST::getLastChild with receiver</description>
<lineContent>currentNode = currentNode.getLastChild();</lineContent>
</mutation>

<mutation unstable="false">
<sourceFile>VariableDeclarationUsageDistanceCheck.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck</mutatedClass>
<mutatedMethod>getFirstNodeInsideIfBlock</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.NonVoidMethodCallMutator</mutator>
<description>removed call to com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheck::isChild</description>
<lineContent>else if (isChild(currentNode, variable)) {</lineContent>
</mutation>

<mutation unstable="false">
<sourceFile>VariableDeclarationUsageDistanceCheck.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck</mutatedClass>
<mutatedMethod>getFirstNodeInsideIfBlock</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_EQUAL_ELSE</mutator>
<description>removed conditional - replaced equality check with false</description>
<lineContent>else if (isChild(currentNode, variable)) {</lineContent>
</mutation>

<mutation unstable="false">
<sourceFile>VariableDeclarationUsageDistanceCheck.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck</mutatedClass>
<mutatedMethod>getFirstNodeInsideIfBlock</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.NonVoidMethodCallMutator</mutator>
<description>removed call to com/puppycrawl/tools/checkstyle/api/DetailAST::getType</description>
<lineContent>if (currentNode.getType() == TokenTypes.LITERAL_IF) {</lineContent>
</mutation>

<mutation unstable="false">
<sourceFile>VariableDeclarationUsageDistanceCheck.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck</mutatedClass>
<mutatedMethod>getFirstNodeInsideIfBlock</mutatedMethod>
<mutator>org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_EQUAL_ELSE</mutator>
<description>removed conditional - replaced equality check with false</description>
<lineContent>if (currentNode.getType() == TokenTypes.LITERAL_IF) {</lineContent>
</mutation>

<mutation unstable="false">
<sourceFile>VariableDeclarationUsageDistanceCheck.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck</mutatedClass>
Expand Down Expand Up @@ -228,7 +183,7 @@
<mutation unstable="false">
<sourceFile>VariableDeclarationUsageDistanceCheck.java</sourceFile>
<mutatedClass>com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck</mutatedClass>
<mutatedMethod>lambda$getFirstCaseGroupOrSwitchRule$1</mutatedMethod>
<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>
Expand Down
Expand Up @@ -761,36 +761,25 @@ private static DetailAST getFirstNodeInsideIfBlock(
DetailAST firstNodeInsideBlock = null;

if (!isVariableInOperatorExpr(block, variable)) {
DetailAST currentNode = block.getLastChild();
final List<DetailAST> variableUsageExpressions =
new ArrayList<>();

while (currentNode != null
&& currentNode.getType() == TokenTypes.LITERAL_ELSE) {
final DetailAST previousNode =
currentNode.getPreviousSibling();

// Checking variable usage inside IF block.
if (isChild(previousNode, variable)) {
variableUsageExpressions.add(previousNode);
}
final Optional<DetailAST> slistToken = TokenUtil
.findFirstTokenByPredicate(block, token -> token.getType() == TokenTypes.SLIST);
final DetailAST currentNode = block.getLastChild();
DetailAST previousNode = currentNode.getPreviousSibling();

// Looking into ELSE block, get its first child and analyze it.
currentNode = currentNode.getFirstChild();
if (slistToken.isEmpty()
&& currentNode.getType() == TokenTypes.LITERAL_ELSE) {

if (currentNode.getType() == TokenTypes.LITERAL_IF) {
currentNode = currentNode.getLastChild();
}
else if (isChild(currentNode, variable)) {
variableUsageExpressions.add(currentNode);
currentNode = null;
}
// Is if statement without '{}' and has a following else branch,
// then change previousNode to the if statement body.
previousNode = previousNode.getPreviousSibling();
}

final List<DetailAST> variableUsageExpressions = new ArrayList<>();
if (isChild(previousNode, variable)) {
variableUsageExpressions.add(previousNode);
}

// If IF block doesn't include ELSE then analyze variable usage
// only inside IF block.
if (currentNode != null
&& isChild(currentNode, variable)) {
if (isChild(currentNode, variable)) {
variableUsageExpressions.add(currentNode);
}

Expand Down
Expand Up @@ -86,6 +86,13 @@ public void testGeneralLogic2() throws Exception {
"17:9: " + getCheckMessage(MSG_KEY, "first", 5, 1),
"29:9: " + getCheckMessage(MSG_KEY, "allInvariants", 2, 1),
"59:9: " + getCheckMessage(MSG_KEY, "a", 2, 1),
"68:9: " + getCheckMessage(MSG_KEY, "a", 4, 1),
"78:9: " + getCheckMessage(MSG_KEY, "a", 2, 1),
"83:9: " + getCheckMessage(MSG_KEY, "b", 2, 1),
"90:9: " + getCheckMessage(MSG_KEY, "c", 3, 1),
"101:9: " + getCheckMessage(MSG_KEY, "b", 2, 1),
"102:9: " + getCheckMessage(MSG_KEY, "c", 3, 1),
"103:9: " + getCheckMessage(MSG_KEY, "d", 4, 1),
};
verifyWithInlineConfigParser(
getPath("InputVariableDeclarationUsageDistanceGeneral2.java"), expected);
Expand Down
Expand Up @@ -63,4 +63,52 @@ void method() throws Exception {
a.equals("");
}
}

void method2() {
int a = 12; // violation
if (true) {
method2();
p();
method2();
a++;
}
}

void checkIfStatementWithoutParen() {
int a = 12; // violation
method2();
if (true)
a++;

int b = 12; // violation
method2();
if (false)
method2();
else if(true)
b++;

int c = 12; // violation
method2();
p();
if (true)
c++;
else
method2();
}

void testConsecutiveIfStatements() {
int a = 12; // ok
int b = 13; // violation
int c = 14; // violation
int d = 15; // violation

if (true)
a++;
if (true)
b++;
if (false)
c++;
if (true)
d++;
}
}

0 comments on commit 4bf25e9

Please sign in to comment.