Skip to content

Commit

Permalink
Issue #11720: Kill surviving mutation in VariableDeclarationUsageDist…
Browse files Browse the repository at this point in the history
…anceCheck in getFirstNodeInsideIfBlock
  • Loading branch information
Vyom-Yadav authored and baratali committed Aug 21, 2022
1 parent 60c6b19 commit a97c3e8
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 73 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,37 +761,26 @@ 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 lastNode = block.getLastChild();
DetailAST previousNode = lastNode.getPreviousSibling();

// Looking into ELSE block, get its first child and analyze it.
currentNode = currentNode.getFirstChild();
if (slistToken.isEmpty()
&& lastNode.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)) {
variableUsageExpressions.add(currentNode);
if (isChild(lastNode, variable)) {
variableUsageExpressions.add(lastNode);
}

// If variable usage exists in several related blocks, then
Expand Down
Expand Up @@ -91,6 +91,23 @@ public void testGeneralLogic2() throws Exception {
getPath("InputVariableDeclarationUsageDistanceGeneral2.java"), expected);
}

@Test
public void testIfStatements() throws Exception {
final String[] expected = {
"18:9: " + getCheckMessage(MSG_KEY, "a", 4, 1),
"28:9: " + getCheckMessage(MSG_KEY, "a", 2, 1),
"32:9: " + getCheckMessage(MSG_KEY, "b", 2, 1),
"38:9: " + getCheckMessage(MSG_KEY, "c", 3, 1),
"49:9: " + getCheckMessage(MSG_KEY, "b", 2, 1),
"50:9: " + getCheckMessage(MSG_KEY, "c", 3, 1),
"51:9: " + getCheckMessage(MSG_KEY, "d", 4, 1),
"63:9: " + getCheckMessage(MSG_KEY, "a", 4, 1),

};
verifyWithInlineConfigParser(
getPath("InputVariableDeclarationUsageDistanceIfStatements.java"), expected);
}

@Test
public void testDistance() throws Exception {
final String[] expected = {
Expand Down
@@ -0,0 +1,72 @@
/*
VariableDeclarationUsageDistance
allowedDistance = 1
ignoreVariablePattern = (default)
validateBetweenScopes = true
ignoreFinal = false
*/

package com.puppycrawl.tools.checkstyle.checks.coding.variabledeclarationusagedistance;

import java.util.HashSet;
import java.util.Set;

public class InputVariableDeclarationUsageDistanceIfStatements {
void method2() {
int a = 12; // violation
if (true) {
method2();
checkIfStatementWithoutParen();
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();
checkIfStatementWithoutParen();
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++;
}

int testReturnStatement() {
int a = 1; // violation
testConsecutiveIfStatements();
testConsecutiveIfStatements();
testConsecutiveIfStatements();
if (true)
return a;

return 0;
}
}

0 comments on commit a97c3e8

Please sign in to comment.