Skip to content

[java] LocalVariableCouldBeFinal shouldn't report unused variables #4511

@adangel

Description

@adangel

Affects PMD Version: 6.x

Rule: LocalVariableCouldBeFinal

Description:

A local variable, that is just defined but not used, is a different problem: UnusedLocalVariable. Make the variable final doesn't fix anything.

Found via #3123. It's already fixed with PMD 7.0.0-rc1.
Was fixed via #3113.

Code Sample demonstrating the issue:

public class Foo {
    public void test1() {
        int a = 0; // not final, but also not used
    }
}

Expected outcome:

PMD reports a violation at line 3, but that's wrong. That's a false positive.

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions