GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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 #3850

New issue
Copy link
New issue
Copy link
Closed
#3855
Closed
[java] ImmutableField - false negative when field assigned in constructor conditionally#3850
#3855
Copy link
Labels
a:false-negativePMD doesn't flag a problematic piece of code
Milestone
6.44.0

Description

@MetaBF
MetaBF
opened on Mar 17, 2022
Issue body actions

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.

  • Code Example 1
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.

  • Code Example 2
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]

Activity

  1. MetaBF
    added
    a:false-negativePMD doesn't flag a problematic piece of code
    on Mar 17, 2022
  2. MetaBF
    changed 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, 2022
  3. adangel commented on Mar 17, 2022

    @adangel
    adangel
    on Mar 17, 2022
    Member
    More actions

    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:

    pmd/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/ImmutableField.xml

    Lines 309 to 325 in 59b36b6

    <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
  4. adangel
    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, 2022
  5. adangel
    added a commit that references this issue on Mar 17, 2022

    [java] Fix ImmutableField with conditionally assignment in ctors

    This issue will close once commit b22990a is merged into the 'main' branch.
    b22990a
  6. adangel
    added this to the 6.44.0 milestone on Mar 17, 2022
  7. adangel
    closed this as completedin #3855on Mar 27, 2022
  8. adangel
    closed this as completedin b22990aon Mar 27, 2022
  9. ITachiLab commented on Aug 6, 2024

    @ITachiLab
    ITachiLab
    on Aug 6, 2024
    More actions

    @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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

      Milestone

      • 6.44.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.