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

VariableDeclarationUsageDistance throws NPE on empty switch statements #11973

Closed
Vyom-Yadav opened this issue Jul 23, 2022 · 4 comments · Fixed by #12052
Closed

VariableDeclarationUsageDistance throws NPE on empty switch statements #11973

Vyom-Yadav opened this issue Jul 23, 2022 · 4 comments · Fixed by #12052

Comments

@Vyom-Yadav
Copy link
Member

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, as cli describes the problem better than 1,000 words

/var/tmp $ javac Test.java
/var/tmp $ cat config.xml
<!DOCTYPE module PUBLIC
        "-//Puppy Crawl//DTD Check Configuration 1.2//EN"
        "http://www.puppycrawl.com/dtds/configuration_1_2.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 Test.java
public class Test {
    void method() {
        int i = -1;
        switch (i) {
        }
    }
}
/var/tmp $ RUN_LOCALE="-Duser.language=en -Duser.country=US"
/var/tmp $ java $RUN_LOCALE -jar checkstyle-10.3.1-all.jar -c config.xml Test.java
Starting audit...
com.puppycrawl.tools.checkstyle.api.CheckstyleException: Exception was thrown while processing Test.java
        at com.puppycrawl.tools.checkstyle.Checker.processFiles(Checker.java:307)
        at com.puppycrawl.tools.checkstyle.Checker.process(Checker.java:224)
        at com.puppycrawl.tools.checkstyle.Main.runCheckstyle(Main.java:407)
        at com.puppycrawl.tools.checkstyle.Main.runCli(Main.java:330)
        at com.puppycrawl.tools.checkstyle.Main.execute(Main.java:189)
        at com.puppycrawl.tools.checkstyle.Main.main(Main.java:126)
Caused by: java.lang.NullPointerException
        at com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck.getFirstNodeInsideSwitchBlock(VariableDeclarationUsageDistanceCheck.java:836)
        at com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck.calculateDistanceBetweenScopes(VariableDeclarationUsageDistanceCheck.java:651)
        at com.puppycrawl.tools.checkstyle.checks.coding.VariableDeclarationUsageDistanceCheck.visitToken(VariableDeclarationUsageDistanceCheck.java:410)
        at com.puppycrawl.tools.checkstyle.TreeWalker.notifyVisit(TreeWalker.java:335)
        at com.puppycrawl.tools.checkstyle.TreeWalker.processIter(TreeWalker.java:406)
        at com.puppycrawl.tools.checkstyle.TreeWalker.walk(TreeWalker.java:273)
        at com.puppycrawl.tools.checkstyle.TreeWalker.processFiltered(TreeWalker.java:154)
        at com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck.process(AbstractFileSetCheck.java:92)
        at com.puppycrawl.tools.checkstyle.Checker.processFile(Checker.java:333)
        at com.puppycrawl.tools.checkstyle.Checker.processFiles(Checker.java:294)
        ... 5 more
Checkstyle ends with 1 errors.
@stoyanK7
Copy link
Contributor

On it. Documentation on property validateBetweenScopes is weird to me. I suppose we expect no violation here, is that right?

Also, do we expect violation in the following code when using your configuration, i suppose no as well?

public class Test {
    void method() {
        int i = -1;



        switch (i) {
        }
    }
}

@Vyom-Yadav
Copy link
Member Author

Vyom-Yadav commented Jul 24, 2022

No, we do not expect violation in both cases, empty lines do not count in declaration distance. In both cases, the distance between declaration and usage is 0.

@nrmancuso
Copy link
Member

Closed via #12052

@Vyom-Yadav
Copy link
Member Author

Awesome work @stoyanK7

rnveach pushed a commit to rnveach/checkstyle that referenced this issue Sep 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants