Skip to content

False Negative: FinalLocalVariableCheck does not report for variables assigned in adjacent catches #19717

@lithops-zty

Description

@lithops-zty

I have read check documentation: https://checkstyle.org/checks/coding/finallocalvariable.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 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">
    <module name="TreeWalker">
        <module name="FinalLocalVariableCheck"/>
    </module>
</module>
/var/tmp $ cat Test.java 
import java.io.FileNotFoundException;
import java.nio.channels.FileLock;

public class Test {
    public void test(boolean x) {
        int u;  // violation, but not reported
        try {
        } catch (IndexOutOfBoundsException e) {
            u = 9;
        } catch (RuntimeException e) {
            u = 10;
        }

        int v;  // violation
        if (x) {
            v = 0;
        } else {
            v = 1;
        }
    }

}
/var/tmp $ RUN_LOCALE="-Duser.language=en -Duser.country=US"
/var/tmp $ java $RUN_LOCALE -jar checkstyle-13.4.0-all.jar -c config.xml Test.java 
Starting audit...
[ERROR] /mnt/d/IntellijProjects/checkstyle-playground/inputs/final-local-variable/Test.java:14:13: Variable 'v' should be declared final. [FinalLocalVariable]
Audit done.
Checkstyle ends with 1 errors.

Describe what you expect in detail.

A violation on u not declared final should be reported, since catch clauses are mutually-exclusive.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions