GeezerVison is showing github.com — [java] ImmutableField - false negative when field assigned in constructor conditionally · Issue #3850 · pmd/pmd
[java] ImmutableField - false negative when field assigned in constructor conditionally #3850Description
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] Activitychanged the title [-][java] ImmutableField: private field could be final when only assigning in constructor[/-] [+][java] ImmutableField: private field could be final when only assigned in constructor[/+] on Mar 17, 2022adangel commented on Mar 17, 2022 Thanks for the report. I think you are right - in Example 1, the field "str" can be made final and the rule should report this. "str" is a private field whose value never changes once object initialization ends.
Interestingly we have a test case, the manifests the opposite, but I think, the test case is just wrong:
|
<test-code> |
|
<description>Bug 1740480, assignment in single constructor based on constructor argument check</description> |
|
<expected-problems>0</expected-problems> |
|
<code><![CDATA[ |
|
public class MyClass { |
|
private int positive; |
|
|
|
public MyClass(int value) { |
|
if (value > 0) { |
|
positive = value; |
|
} else { |
|
positive = 1; |
|
} |
|
} |
|
} |
|
]]></code> |
|
</test-code> |
Reacted by MetaBF and Clément Fournier changed the title [-][java] ImmutableField: private field could be final when only assigned in constructor[/-] [+][java] ImmutableField - false negative when field assigned in constructor conditionally[/+] on Mar 17, 2022added a commit that references this issue on Mar 17, 2022This issue will close once commit b22990a is merged into the 'main' branch.b22990a ITachiLab commented on Aug 6, 2024 @adangel I have a feeling this change is a regression. The rule works well for the ideal condition proposed by @MetaBF, where the field is initialized both inside if and else blocks. Unfortunately, for the following code the PMD behavior is annoying:
public class ExampleImmutableField {
private int fooBar;
public ExampleImmutableField(int newVal, boolean flag) {
if (flag) {
this.fooBar = newVal;
}
}
}
PMD marks fooBar as eligible for final modifier, although I really want my fooBar field to be 0 by default when the flag is not set because this is the expectation of the algorithm. The above code was valid for PMD 6.12. And sure, I can add else block with this.fooBar = 0 and that will silence PMD for good, but imagine doing this for five, eight, or more fields:
public class ExampleImmutableField {
private final int a;
private final int b;
private final int c;
private final int d;
public ExampleImmutableField(int newVal, boolean flag) {
if (flag) {
this.a = magic(newVal);
this.b = magic(newVal);
this.c = magic(newVal);
this.d = magic(newVal);
} else {
this.a = 0;
this.b = 0;
this.c = 0;
this.d = 0;
}
}
}
Or maybe I'm missing something here.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|