Skip to content

[java] A false positive about the rule NonThreadSafeSingleton #4043

@Zustin

Description

@Zustin

Affects PMD Version:6.47.0

Rule:NonThreadSafeSingleton

Please provide the rule name and a link to the rule documentation:
https://pmd.github.io/latest/pmd_rules_java_multithreading.html#nonthreadsafesingleton

Description:
Hi, I found a false positive about the rule NonThreadSafeSingleton, please read the following code example. In line 3 and line 4, PMD report two warnings, but I think we only need one warning in line 4 is sufficient. Hence, I think this is a false positive. Thanks for your consideration.

Code Sample demonstrating the issue:

private static List buz;
public static List bar() {
    if (true) { // the first warning
        if (buz == null) // the second warning
            buz = new ArrayList();
    }
    return buz;
}

Expected outcome:

PMD reports a violation at line 3, but that's wrong. That's a false positive.

Running PMD through: [Maven]

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positivePMD flags a piece of code that is not problematic

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions