Skip to content

[java] DoubleCheckedLocking: False negative when the outer null check is written as !(x != null) #6900

Closed

Description

@MarkLee131

Affects PMD Version: 7.26.0

Rule: DoubleCheckedLocking
https://docs.pmd-code.org/latest/pmd_rules_java_multithreading.html#doublecheckedlocking

Description:

DoubleCheckedLocking matches the guard by its syntactic form. Writing the outer null check as the logically equivalent !(x != null) instead of x == null makes the rule stop firing, even though the double-checked-locking anti-pattern (and its unsafe publication) is entirely unchanged. Found by metamorphic testing; the same silence reproduces across many double-checked-locking seeds under equivalent guard rewrites.

Code Sample demonstrating the issue:

public class DclSeed {          // reported: DclSeed.java:4 DoubleCheckedLocking
    Object baz;

    Object bar() {
        if (baz == null) {
            synchronized (this) {
                if (baz == null) {
                    baz = new Object();
                }
            }
        }
        return baz;
    }
}
public class DclVariant {       // NOT reported (only LogicInversion fires)
    Object baz;

    Object bar() {
        if (!(baz != null)) {                 // <- equivalent rewrite of `baz == null`
            synchronized (this) {
                if (baz == null) {
                    baz = new Object();
                }
            }
        }
        return baz;
    }
}
$ pmd check -d DclSeed.java,DclVariant.java \
      -R category/java/multithreading.xml/DoubleCheckedLocking -f text
DclSeed.java:4:  DoubleCheckedLocking: Double checked locking is not thread safe in Java.
# (no violation on DclVariant.java)

Expected outcome:

PMD should report a DoubleCheckedLocking violation at DclVariant.java:4 (the outer if), but doesn't. This is a false-negative.

Running PMD through: CLI

Activity

  1. added
    a:false-negative
    on Jul 22, 2026
  2. changed the title [-][java] DoubleCheckedLocking: not reported when the outer null check is written as !(x != null)[/-] [+][java] DoubleCheckedLocking: False negative when the outer null check is written as !(x != null)[/+] on Jul 22, 2026
  3. added this to the 7.27.0 milestone on Aug 4, 2026
  4. added a commit that references this issue on Aug 4, 2026
    55d2cca
  5. added a commit that references this issue on Oct 7, 2026
    f71a964
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-negative

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions