From 4149d9dd2927c986ae80b719ba633f405ce63867 Mon Sep 17 00:00:00 2001 From: Vyom-Yadav Date: Sat, 23 Jul 2022 23:59:25 +0530 Subject: [PATCH] Issue #11720: Kill surviving mutation in VariableDeclarationUsageDistanceCheck in getFirstNodeInsideIfBlock --- .../pitest-coding-1-suppressions.xml | 18 ----------------- ...VariableDeclarationUsageDistanceCheck.java | 20 ++++++------------- ...ableDeclarationUsageDistanceCheckTest.java | 1 + ...iableDeclarationUsageDistanceGeneral2.java | 10 ++++++++++ 4 files changed, 17 insertions(+), 32 deletions(-) diff --git a/.ci/pitest-suppressions/pitest-coding-1-suppressions.xml b/.ci/pitest-suppressions/pitest-coding-1-suppressions.xml index 87b789614ce..09c4a888602 100644 --- a/.ci/pitest-suppressions/pitest-coding-1-suppressions.xml +++ b/.ci/pitest-suppressions/pitest-coding-1-suppressions.xml @@ -153,15 +153,6 @@ else if (isChild(currentNode, variable)) { - - VariableDeclarationUsageDistanceCheck.java - com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck - getFirstNodeInsideIfBlock - org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_EQUAL_ELSE - removed conditional - replaced equality check with false - else if (isChild(currentNode, variable)) { - - VariableDeclarationUsageDistanceCheck.java com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck @@ -171,15 +162,6 @@ if (currentNode.getType() == TokenTypes.LITERAL_IF) { - - VariableDeclarationUsageDistanceCheck.java - com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck - getFirstNodeInsideIfBlock - org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_EQUAL_ELSE - removed conditional - replaced equality check with false - if (currentNode.getType() == TokenTypes.LITERAL_IF) { - - VariableDeclarationUsageDistanceCheck.java com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheck.java index 322c2e10cd7..537ccd560c6 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheck.java @@ -766,11 +766,9 @@ private static DetailAST getFirstNodeInsideIfBlock( if (!isVariableInOperatorExpr(block, variable)) { DetailAST currentNode = block.getLastChild(); - final List variableUsageExpressions = - new ArrayList<>(); + final List variableUsageExpressions = new ArrayList<>(); - while (currentNode != null - && currentNode.getType() == TokenTypes.LITERAL_ELSE) { + if (currentNode.getType() == TokenTypes.LITERAL_ELSE) { final DetailAST previousNode = currentNode.getPreviousSibling(); @@ -781,20 +779,14 @@ private static DetailAST getFirstNodeInsideIfBlock( // Looking into ELSE block, get its first child and analyze it. currentNode = currentNode.getFirstChild(); - - if (currentNode.getType() == TokenTypes.LITERAL_IF) { - currentNode = currentNode.getLastChild(); - } - else if (isChild(currentNode, variable)) { - variableUsageExpressions.add(currentNode); - currentNode = null; - } } // If IF block doesn't include ELSE then analyze variable usage // only inside IF block. - if (currentNode != null - && isChild(currentNode, variable)) { + // OR + // The IF block contained ELSE and currentNode is equal to the first child in + // ELSE block, then also analyze the first child. + if (isChild(currentNode, variable)) { variableUsageExpressions.add(currentNode); } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheckTest.java index bf2f3250dcf..aab4c75250e 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheckTest.java @@ -86,6 +86,7 @@ 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), }; verifyWithInlineConfigParser( getPath("InputVariableDeclarationUsageDistanceGeneral2.java"), expected); diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/variabledeclarationusagedistance/InputVariableDeclarationUsageDistanceGeneral2.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/variabledeclarationusagedistance/InputVariableDeclarationUsageDistanceGeneral2.java index d361ad7216d..4ad0a3e6395 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/variabledeclarationusagedistance/InputVariableDeclarationUsageDistanceGeneral2.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/variabledeclarationusagedistance/InputVariableDeclarationUsageDistanceGeneral2.java @@ -63,4 +63,14 @@ void method() throws Exception { a.equals(""); } } + + void method2() { + int a = 12; // violation + if (true) { + method2(); + p(); + method2(); + a++; + } + } }