A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pmd/pmd/issues/3850 below:

[java] ImmutableField - false negative when field assigned in constructor conditionally · Issue #3850 · pmd/pmd · GitHub

Affects PMD Version: 6.43.0

Rule: ImmutableField

https://pmd.github.io/pmd-6.42.0/pmd_rules_java_design.html#immutablefield

Description:

Hi, I found a false-negative about rule ImmutableField, in this case, the private field str is only assigned in the constructor, therefore str could be final.

public class ExampleImmutableField {

    private String str; // false negative here, could be final

    public ExampleImmutableField(String strLocal, boolean flag) { 
        if (flag){
            this.str = strLocal;
        } else {
            this.str = strLocal+"123";
        }
    }

}

I think it's probably due to wrong/limited control-flow analysis since PMD works fine in the following code.

public class ExampleImmutableField {

    private String str; // PMD successfully reports warning here

    public ExampleImmutableField(String strLocal, boolean flag) {
            this.str = strLocal;
    }

} 

Expected outcome:

PMD should report a violation at line 3 in Code Example 1, but doesn't. This is a false-negative.

Running PMD through: [CLI]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4