Skip to content

[java] LocalVariableCouldBeFinal: False negative when multiple variables are declared at once #6602

@UncleOwen

Description

@UncleOwen

Affects PMD Version: 7.23.0

Rule: LocalVariableCouldBeFinal

Description: When multiple variables are declared in the same line, and some of them could be final, LocalVariableCouldBeFinal is not raised.

Code Sample demonstrating the issue:

class Foo {
    void foo(boolean cond1) {
        int i=0, j=1;

        if (cond1) {
            i = 3;
        }

        IO.println(i + " " + j);
    }
}

Expected outcome:

PMD should report a violation at line 3, but doesn't. This is a false-negative. If I split up the two declarations, there is a violation for j.

PMD does report a OneDeclarationPerLine violation for the above code. I'm aware of PMDs policy to only report one violation for one issue. But PMD also actively encourages users to pick and choose which rules to enable. I find it very surprising that the above code doesn't raise a violation if LocalVariableCouldBeFinal is enabled (and OneDeclarationPerLine is not).

Running PMD through: CLI

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-negativePMD doesn't flag a problematic piece of code

    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