Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VariableDeclarationUsageDistanceCheck doesn't handle method definition properly #13038

Open
jxr98 opened this issue Apr 30, 2023 · 2 comments · May be fixed by #16026
Open

VariableDeclarationUsageDistanceCheck doesn't handle method definition properly #13038

jxr98 opened this issue Apr 30, 2023 · 2 comments · May be fixed by #16026
Labels

Comments

@jxr98
Copy link
Contributor

jxr98 commented Apr 30, 2023

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.

/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">
    <property name="charset" value="UTF-8"/>

    <!-- do not change severity to 'error', as that will hide errors caused by exceptions -->
    <property name="severity" value="warning"/>

    <!-- haltOnException is required for exception fixes and reporting of all exceptions -->
    <property name="haltOnException" value="false"/>

    <!-- BeforeExecutionFileFilters is required for sources of java9 -->
    <module name="BeforeExecutionExclusionFileFilter">
        <property name="fileNamePattern" value="module\-info\.java$" />
    </module>

    <module name="TreeWalker">
         <!-- Example of checkstyle Check usage -->
         <!-- PLEASE CHANGE IT TO CHECK YOU ARE TESTING !!!! -->
              
        <module name="VariableDeclarationUsageDistance">
          <property name="id" value="Custom"/>
          <property name="validateBetweenScopes" value="true"/>
          <property name="allowedDistance" value="1"/>
        </module> 
    </module>
    <!-- Example of filter -->
    <!--
    <module name="SeverityMatchFilter">
        <property name="severity" value="warning"/>
        <property name="acceptOnMatch" value="false"/>
    </module>
    -->

    <!-- as we run on regression even on non-compiled files we need to skip exceptions on them -->
    <module name="SuppressionSingleFilter">
      <property name="message" value="Exception occurred while parsing"/>
      <property name="checks" value="Checker"/>
    </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). [Custom]
Audit done.

For Windows users, please use type instead of cat and run

set RUN_LOCALE="-Duser.language=en -Duser.country=US"
java %RUN_LOCALE% -jar checkstyle-10.9.3-all.jar -c config.xml Test1.java

in place of the last 2 commands above.


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.

pymoura added a commit to pymoura/checkstyle that referenced this issue May 15, 2023
pymoura added a commit to pymoura/checkstyle that referenced this issue May 15, 2023
pymoura added a commit to pymoura/checkstyle that referenced this issue May 15, 2023
JAZwielich pushed a commit to pymoura/checkstyle that referenced this issue May 15, 2023
…en method in VariableDeclarationUsageDistaceCheck
pymoura added a commit to pymoura/checkstyle that referenced this issue May 15, 2023
…en method in VariableDeclarationUsageDistaceCheck
pymoura added a commit to pymoura/checkstyle that referenced this issue May 20, 2023
pymoura added a commit to pymoura/checkstyle that referenced this issue May 20, 2023
@checkstyle checkstyle deleted a comment from pymoura Nov 11, 2023
@checkstyle checkstyle deleted a comment from jxr98 Nov 11, 2023
@checkstyle checkstyle deleted a comment from jxr98 Nov 11, 2023
@checkstyle checkstyle deleted a comment from pymoura Nov 11, 2023
@checkstyle checkstyle deleted a comment from pymoura Nov 11, 2023
@romani
Copy link
Member

romani commented Nov 11, 2023

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

@jxr98
Copy link
Contributor Author

jxr98 commented Feb 29, 2024

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

jxr98 added a commit to jxr98/checkstyle that referenced this issue Mar 10, 2024
jxr98 added a commit to jxr98/checkstyle that referenced this issue Mar 10, 2024
jxr98 added a commit to jxr98/checkstyle that referenced this issue Mar 17, 2024
jxr98 added a commit to jxr98/checkstyle that referenced this issue Apr 1, 2024
jxr98 added a commit to jxr98/checkstyle that referenced this issue Apr 1, 2024
jxr98 added a commit to jxr98/checkstyle that referenced this issue Apr 2, 2024
MohanadKh03 pushed a commit to MohanadKh03/checkstyle that referenced this issue Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants