Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] SingularField false positive with read in while condition #3754

Closed
adangel opened this issue Jan 28, 2022 · 1 comment
Closed

[java] SingularField false positive with read in while condition #3754

adangel opened this issue Jan 28, 2022 · 1 comment
Labels
a:false-positive PMD flags a piece of code that is not problematic in:data-flow Affects the data flow analysis code
Milestone

Comments

@adangel
Copy link
Member

adangel commented Jan 28, 2022

Affects PMD Version: 7.0.0-SNAPSHOT

Rule: SingularField

Description: This is a false positive detected by dogfood (#3657) on PMD 7. I thought it would have been fixed by #3671, but it is still there. I think, this is in the realm of dataflow. See the sample code below, there are various different cases.

Code Sample demonstrating the issue:

public class SingularFieldFP {
    public Object case1() {
        return new Object() {
            private int field; // <---------- false positive
            public boolean foo() {
                while (field++ < 10);
                return false;
            }
        };
    }
    public Object case2() {
        return new Object() {
            private int field; // <---------- false positive
            public boolean foo() {
                while (field < 10) {
                    field++;
                }
                return false;
            }
        };
    }
    public Object case3() {
        return new Object() {
            private int field;
            public boolean foo() {
                if (field < 10) {
                    field++;
                }
                while (field < 10) {
                    field++;
                }
                return false;
            }
        };
    }
    public Object case4() {
        return new Object() {
            private int field;
            public boolean foo() {
                if (field > 10) {
                    return true;
                }
                field++;
                return false;
            }
        };
    }
}

Expected outcome:

PMD reports a violation at lines 4 and 13, but that's wrong. That's a false positive.

The field is read at the beginning of the method "foo" and the current value is unknown. So this field cannot be converted to a local variable.

Running PMD through: Maven

@adangel adangel added in:data-flow Affects the data flow analysis code a:false-positive PMD flags a piece of code that is not problematic labels Jan 28, 2022
@adangel adangel added this to the 7.0.0 milestone Jan 28, 2022
adangel added a commit that referenced this issue Feb 3, 2022
[java] Fix #3754 singularfield fp #3756

* pr-3756:
  Fix #3754 - SingularField false positive with read in while condition
@adangel adangel mentioned this issue Jan 23, 2023
55 tasks
@jsotuyod
Copy link
Member

This is fixed in PMD 7.0.0-rc1. It won't be backported to PMD 6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-positive PMD flags a piece of code that is not problematic in:data-flow Affects the data flow analysis code
Projects
None yet
Development

No branches or pull requests

2 participants