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

UnusedLocalVariable does not support local classes #14092

Closed
Vyom-Yadav opened this issue Dec 2, 2023 · 2 comments
Closed

UnusedLocalVariable does not support local classes #14092

Vyom-Yadav opened this issue Dec 2, 2023 · 2 comments

Comments

@Vyom-Yadav
Copy link
Member

Vyom-Yadav commented Dec 2, 2023

I have read check documentation: https://checkstyle.sourceforge.io/checks/coding/unusedlocalvariable.html#UnusedLocalVariable
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
<?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" />
    <property name="severity" value="warning" />
    <property name="haltOnException" value="false" />
    <module name="TreeWalker">
        <module name="UnusedLocalVariable">
        </module>
    </module>
</module>
/var/tmp $ cat Test.java
public class Test {
    int a = 12;

    void foo() {
        int a = 12; // this is unused, no violation
        int ab = 12; // this is unused, violation thrown

        class asd {
            Test a = new Test() {
                void asd() {
                    System.out.println(a);
                }
            };
        }
    }
}
/var/tmp $ RUN_LOCALE="-Duser.language=en -Duser.country=US"
/var/tmp $ java $RUN_LOCALE -jar checkstyle-10.12.5-all.jar -c config.xml Test.java
Starting audit...
[WARN] /var/tmp/Test.java:6:9: Unused local variable 'ab'. [UnusedLocalVariable]
Audit done.

Describe what you expect in detail.

I expect a violation to be thrown for both the local variables. The check currently has no logic for scoping local classes, which is the root cause of this false negative. There might be cases where lack of this support might give a false positive.

@Vyom-Yadav
Copy link
Member Author

Scoping local classes is difficult for the current check logic as local classes can have n nesting and their independent ecosystem. Local classes can extend other local classes, and all problems that occur with top-level classes can occur for local too. We currently don't do any scoping for local classes. Ideally, local classes should be tracked within the method boundary similar to top-level classes.

@romani romani removed the bug label Dec 2, 2023
@Lmh-java
Copy link
Contributor

Hi, is anyone working on this right now? If not, consider I am on it.

@checkstyle checkstyle deleted a comment from Kevin222004 Mar 17, 2024
@checkstyle checkstyle deleted a comment from aayushRedHat Mar 17, 2024
@checkstyle checkstyle deleted a comment from relentless-pursuit Mar 17, 2024
@checkstyle checkstyle deleted a comment from aayushRedHat Mar 17, 2024
@checkstyle checkstyle deleted a comment from Vyom-Yadav Mar 17, 2024
@checkstyle checkstyle deleted a comment from harshit4311 Mar 17, 2024
Lmh-java added a commit to Lmh-java/checkstyle that referenced this issue Mar 18, 2024
Lmh-java added a commit to Lmh-java/checkstyle that referenced this issue Mar 19, 2024
Lmh-java added a commit to Lmh-java/checkstyle that referenced this issue Mar 19, 2024
Lmh-java added a commit to Lmh-java/checkstyle that referenced this issue Mar 19, 2024
Lmh-java added a commit to Lmh-java/checkstyle that referenced this issue Apr 6, 2024
Lmh-java added a commit to Lmh-java/checkstyle that referenced this issue Apr 7, 2024
Lmh-java added a commit to Lmh-java/checkstyle that referenced this issue Apr 8, 2024
Lmh-java added a commit to Lmh-java/checkstyle that referenced this issue Apr 14, 2024
Lmh-java added a commit to Lmh-java/checkstyle that referenced this issue Apr 16, 2024
@rnveach rnveach closed this as completed Apr 16, 2024
@github-actions github-actions bot added this to the 10.15.1 milestone Apr 16, 2024
mahfouz72 added a commit to mahfouz72/checkstyle that referenced this issue Apr 16, 2024
@nrmancuso nrmancuso added the bug label Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants