Skip to content

VariableDeclarationUsageDistanceCheck doesn't handle method definition properly #13038

Closed

Description

@jxr98

I have read check documentation: https://checkstyle.org/config_coding.html#VariableDeclarationUsageDistance
I have downloaded the latest checkstyle from: https://checkstyle.org/cmdline.html#Download_and_Run
I have executed the cli and showed it below.

https://checkstyle.org/checks/coding/variabledeclarationusagedistance.html#VariableDeclarationUsageDistance

/var/tmp $ javac Test1.java

/var/tmp $ cat config.xml
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
          "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
          "https://checkstyle.org/dtds/configuration_1_3.dtd">

<module name = "Checker">
    <module name="TreeWalker">
        <module name="VariableDeclarationUsageDistance">
          <property name="validateBetweenScopes" value="true"/>
          <property name="allowedDistance" value="1"/>
        </module> 
    </module>
</module>

/var/tmp $ cat Test1.java

import java.util.Iterator;

public class Test1 {
    public void method(){
        int a=1;
        class GeneralLogic {
            public <T> Iterator<T> method(T[] b) throws Exception{
                if(a>0){
                    System.out.println(b.length);
                }
                return null;
            }
        }
    }
}


/var/tmp $ RUN_LOCALE="-Duser.language=en -Duser.country=US"
/var/tmp $ java $RUN_LOCALE -jar checkstyle-10.9.3-all.jar -c config.xml Test1.java

Starting audit...
[WARN] /var/tmp/Test.java:5:9: Distance between variable 'a' declaration and its first usage is 3,
 but allowed 1.  Consider making that variable final if you still need to store its value in 
advance (before method calls that might have side effects on the original value).
 [VariableDeclarationUsageDistance]
Audit done.

The distance should be 1. This three were caused by generic type which is <T>, parameter type in the method signature and throws. I think the correct distance one should be caused by the expression in the if condition.

Activity

  1. added 3 commits that reference this issue on May 15, 2023
  2. added a commit that references this issue on May 15, 2023
  3. added 3 commits that reference this issue on May 15, 2023
  4. deleted a comment from pymoura on Nov 11, 2023
  5. deleted a comment from jxr98 on Nov 11, 2023
  6. deleted a comment from jxr98 on Nov 11, 2023
  7. deleted a comment from pymoura on Nov 11, 2023
  8. deleted a comment from pymoura on Nov 11, 2023
  9. romani commented on Nov 11, 2023

    @romani
    Member

    almost done update is at #13065 , everyone in web is welcome to reuse and finish

  10. jxr98 commented on Feb 29, 2024

    @jxr98
    ContributorAuthor

    I'm on it. Will try to solve it within this week. Thanks. 😃

  11. 19 remaining items

  12. added 2 commits that reference this issue on Aug 7, 2025
  13. added 3 commits that reference this issue on Sep 3, 2025
  14. moved this from In Progress to Todo in Polishing Google Style implementationon Sep 28, 2025
  15. romani commented on Oct 17, 2025

    @romani
    Member

    There is some incomplete update at #17526 that can be reuses.
    Everyone is welcome to send PR

  16. vivek-0509 commented on Jan 14, 2026

    @vivek-0509
    Member

    I am on it

  17. added a commit that references this issue on Jan 14, 2026
  18. added a commit that references this issue on Jan 25, 2026
  19. added this to the 13.1.0 milestone on Jan 25, 2026
  20. romani commented on Jan 25, 2026

    @romani
    Member

    fix is merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions