diff --git a/.ci/pitest-suppressions/pitest-coding-1-suppressions.xml b/.ci/pitest-suppressions/pitest-coding-1-suppressions.xml
index e3bb01fbde1..8d6f0e4d4e0 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 01b38a36690..a27210882e4 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
@@ -762,36 +762,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 '{}'
+ 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;
- }
+ // Change previousNode to the if statement body
+ 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..4f5af32550a 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,9 @@ 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),
};
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..cdc2caaa5ec 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,28 @@ 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++;
+ }
}