A RetroSearch Logo

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

Search Query:

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

[java] SingularField false positive with read in while condition · Issue #3754 · pmd/pmd · GitHub

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


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