Skip to content

False positive for UnusedLocalVariable #14506

@guillerodriguez

Description

@guillerodriguez

I have read check documentation: https://checkstyle.sourceforge.io/checks/coding/unusedlocalvariable.html
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 UnusedVarTest.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="UnusedLocalVariable"/>
    </module>
</module>

/var/tmp $ cat UnusedVarTest.java

public class UnusedVarTest
{
    public void testParameters()
    {
        String[] tests = { "one", "two" };

        int i = 0;
        for (String test : tests)
        {
            switch (i++)
            {
                default:
                case 0:
                    System.out.println("one: " + test);
                    break;
                case 1:
                    System.out.println("two: " + test);
                    break;
            }
        }
    }
}

/var/tmp $ RUN_LOCALE="-Duser.language=en -Duser.country=US"
/var/tmp $ java $RUN_LOCALE -jar checkstyle-10.13.0-all.jar -c config.xml UnusedVarTest.java
Starting audit...
[ERROR] /home/grodriguez/work/hc/UnusedVarTest.java:8:9: Unused local variable 'i'. [UnusedLocalVariable]
Audit done.
Checkstyle ends with 1 errors.

Describe what you expect in detail.

The UnusedLocalVariable check should pass.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions