Skip to content

Commit

Permalink
Issue checkstyle#13038: handled inner class method definition in Vari…
Browse files Browse the repository at this point in the history
…ableDeclarationUsageDistanceCheck
  • Loading branch information
jxr98 committed Mar 17, 2024
1 parent 386360b commit 9810e8a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,9 @@ private static Entry<DetailAST, Integer> calculateDistanceBetweenScopes(
getFirstNodeInsideTryCatchFinallyBlocks(blockWithVariableUsage,
variable);
break;
case TokenTypes.METHOD_DEF:
exprWithVariableUsage = blockWithVariableUsage.getNextSibling();
break;
default:
exprWithVariableUsage = blockWithVariableUsage.getFirstChild();
}
Expand Down Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ void mm() {
<T> void xx(List<T> m){}
}
public void method9() {
// until https://github.com/checkstyle/checkstyle/issues/13011
Integer z = 5; // violation 'Distance .* is 3.'
Integer z = 5;
Iterator<Integer> mn = new HashSet<Integer>().iterator();
class BClass extends Parent {
Boolean h = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <T> Iterator<T> method(T[] b) throws Exception{
if(a>0){
System.out.println(b.length);
}
return null;
}
};
System.out.print(b);
System.out.print(c);
}
}

0 comments on commit 9810e8a

Please sign in to comment.