Skip to content

[java] ImmutableField not reporting fields that are only initialized in the declaration #4008

@HaelC

Description

@HaelC

Affects PMD Version:
6.44, 6.45, 6.46

Rule:
ImmutableField

Please provide the rule name and a link to the rule documentation:
https://pmd.github.io/pmd-6.46.0/pmd_rules_java_design.html#immutablefield

Description:

Fields only initialized in the declaration can be made final. PMD does not report such problem since 6.44.0.

Code Sample demonstrating the issue:

public class Foo {
    private String variable = "hello";

    public String getVariable() {
        return variable;
    }
}

Expected outcome:

PMD should report a violation at line 2, but doesn't. This is a false-negative.

Running PMD through: CLI, Maven

Some of my personal idea: many issues related to ImmutableField (#3874 #3981 #3998 #4004) have occurred since PMD 6.44.0. And the solution is to keep adding annotations to defaultSuppressionAnnotations. Personally I don't think it a good idea...

In 6.44.0 PMD introduced some changes in the ImmutableFieldRule in #3855 , can we investigate it a little bit more?

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