From ac1ab87d3c33e2d3a2d4febec5b9562c2b3a29d6 Mon Sep 17 00:00:00 2001 From: jxr98 Date: Sat, 9 Mar 2024 22:35:25 -0500 Subject: [PATCH] Issue #13038: handled inner class method definition in VariableDeclarationUsageDistanceCheck --- ...VariableDeclarationUsageDistanceCheck.java | 6 +++- ...ableDeclarationUsageDistanceCheckTest.java | 13 ++++++-- ...iableDeclarationUsageDistanceGeneral4.java | 3 +- ...larationUsageDistanceMethodDefinition.java | 30 +++++++++++++++++++ 4 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/variabledeclarationusagedistance/InputVariableDeclarationUsageDistanceMethodDefinition.java 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 a5d777e2225..2bc72516c30 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 @@ -415,6 +415,9 @@ private static Entry calculateDistanceBetweenScopes( getFirstNodeInsideTryCatchFinallyBlocks(blockWithVariableUsage, variable); break; + case TokenTypes.METHOD_DEF: + exprWithVariableUsage = blockWithVariableUsage.getNextSibling(); + break; default: exprWithVariableUsage = blockWithVariableUsage.getFirstChild(); } @@ -784,7 +787,8 @@ private static boolean isZeroDistanceToken(int type) { || type == TokenTypes.MODIFIERS || type == TokenTypes.RESOURCE || type == TokenTypes.EXTENDS_CLAUSE - || type == TokenTypes.IMPLEMENTS_CLAUSE; + || type == TokenTypes.IMPLEMENTS_CLAUSE + || type == TokenTypes.CLASS_DEF; } } 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 3b4050ad2fe..bb542acc729 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 @@ -352,9 +352,7 @@ public void testGeneralClass3() throws Exception { @Test public void testGeneralClass4() throws Exception { - final String[] expected = { - "26:9: " + getCheckMessage(MSG_KEY, "z", 3, 1), - }; + final String[] expected = {}; verifyWithInlineConfigParser( getPath("InputVariableDeclarationUsageDistanceGeneral4.java"), expected); @@ -408,4 +406,13 @@ public void testVariableDeclarationUsageDistanceCloseToBlock() throws Exception verifyWithInlineConfigParser( getPath("InputVariableDeclarationUsageDistanceCloseToBlock.java"), expected); } + + @Test + public void testVariableDeclarationUsageDistanceMethodDefinition() throws Exception { + final String[] expected = { + "18:9: " + getCheckMessage(MSG_KEY, "c", 2, 1), + }; + verifyWithInlineConfigParser( + getPath("InputVariableDeclarationUsageDistanceMethodDefinition.java"), expected); + } } diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/variabledeclarationusagedistance/InputVariableDeclarationUsageDistanceGeneral4.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/variabledeclarationusagedistance/InputVariableDeclarationUsageDistanceGeneral4.java index 25da6e04710..ee8a4fb4089 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/variabledeclarationusagedistance/InputVariableDeclarationUsageDistanceGeneral4.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/variabledeclarationusagedistance/InputVariableDeclarationUsageDistanceGeneral4.java @@ -22,8 +22,7 @@ void mm() { void xx(List m){} } public void method9() { - // until https://github.com/checkstyle/checkstyle/issues/13011 - Integer z = 5; // violation 'Distance .* is 3.' + Integer z = 5; Iterator mn = new HashSet().iterator(); class BClass extends Parent { Boolean h = false; diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/variabledeclarationusagedistance/InputVariableDeclarationUsageDistanceMethodDefinition.java b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/variabledeclarationusagedistance/InputVariableDeclarationUsageDistanceMethodDefinition.java new file mode 100644 index 00000000000..35ece41e3f9 --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/variabledeclarationusagedistance/InputVariableDeclarationUsageDistanceMethodDefinition.java @@ -0,0 +1,30 @@ +/* +VariableDeclarationUsageDistance +allowedDistance = 1 +ignoreVariablePattern = (default) +validateBetweenScopes = true +ignoreFinal = false + + +*/ + +package com.puppycrawl.tools.checkstyle.checks.coding.variabledeclarationusagedistance; + +import java.util.Iterator; +public class InputVariableDeclarationUsageDistanceMethodDefinition { + public void method(){ + int a=1; + int b=2; + int c=3;// violation 'Distance .* is 2.' + class GeneralLogic { + public Iterator method(T[] b) throws Exception{ + if(a>0){ + System.out.println(b.length); + } + return null; + } + }; + System.out.print(b); + System.out.print(c); + } +}