diff --git a/.ci/pitest-suppressions/pitest-coding-1-suppressions.xml b/.ci/pitest-suppressions/pitest-coding-1-suppressions.xml index 6d6c4cabebb..e30acedb2a7 100644 --- a/.ci/pitest-suppressions/pitest-coding-1-suppressions.xml +++ b/.ci/pitest-suppressions/pitest-coding-1-suppressions.xml @@ -126,51 +126,6 @@ switch (examineNode.getType()) { - - VariableDeclarationUsageDistanceCheck.java - com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck - getFirstNodeInsideIfBlock - org.pitest.mutationtest.engine.gregor.mutators.experimental.NakedReceiverMutator - replaced call to com/puppycrawl/tools/checkstyle/api/DetailAST::getLastChild with receiver - currentNode = currentNode.getLastChild(); - - - - VariableDeclarationUsageDistanceCheck.java - com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck - getFirstNodeInsideIfBlock - org.pitest.mutationtest.engine.gregor.mutators.NonVoidMethodCallMutator - removed call to com/puppycrawl/tools/checkstyle/checks/coding/VariableDeclarationUsageDistanceCheck::isChild - 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 - getFirstNodeInsideIfBlock - org.pitest.mutationtest.engine.gregor.mutators.NonVoidMethodCallMutator - removed call to com/puppycrawl/tools/checkstyle/api/DetailAST::getType - 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 34fb64fe233..2e2d0c86c9e 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 @@ -761,36 +761,25 @@ private static DetailAST getFirstNodeInsideIfBlock( DetailAST firstNodeInsideBlock = null; if (!isVariableInOperatorExpr(block, variable)) { - DetailAST currentNode = block.getLastChild(); - final List 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 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(); + // Is if statement without '{}' and has a following else branch, + // then change previousNode to the if statement body. + 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; - } + previousNode = previousNode.getPreviousSibling(); + } + + final List 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); } 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..8fefffac79e 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,10 @@ 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), }; 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..f149df72cd7 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,36 @@ 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(); + } }