Branch: refs/heads/main
  Home:   https://github.com/pmd/pmd
  Commit: ff15eb506bc01cf804e3b7c3e1fa1ebc098189d0
      https://github.com/pmd/pmd/commit/ff15eb506bc01cf804e3b7c3e1fa1ebc098189d0
  Author: fudian <[email protected]>
  Date:   2026-08-10 (Mon, 10 Aug 2026)

  Changed paths:
    M docs/pages/release_notes.md
    M 
pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/multithreading/NonThreadSafeSingletonRule.java
    M 
pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/multithreading/xml/NonThreadSafeSingleton.xml

  Log Message:
  -----------
  [java] Fix #6747: NonThreadSafeSingleton flags ternary lazy init (#6938)

* [java] Fix #6747: NonThreadSafeSingleton flags ternary lazy init

What:
NonThreadSafeSingletonRule now also detects the check-then-act lazy init
race when it is written as a ternary assignment instead of an if statement:
    field = field == null ? new T() : field;

Why:
The rule previously only matched the `if (field == null) field = new T();`
shape, because visit(ASTMethodDeclaration) scanned descendants of
ASTIfStatement. A ternary assignment is an ASTAssignmentExpression whose
right operand is an ASTConditionalExpression, so it lives outside any
ASTIfStatement and the race went unreported (false negative). The two
forms have identical concurrency semantics.

How:
After the existing if-statement scan, iterate the method's assignment
expressions and flag those whose left-hand side is a tracked field and
whose right-hand side is a conditional expression that null-checks the
same field. Synchronized blocks are skipped, mirroring the existing
if-statement handling. Adds two XML test cases (positive: 2 violations
on the ternary form; negative: 0 violations when the ternary guard has
no null check).

Signed-off-by: 付典 <[email protected]>

* [java] NonThreadSafeSingleton: extract shared field-write helper (#6747)

Address review feedback on #6938:

- Extract a private fieldWriteName(ASTAssignmentExpression) helper that
  encapsulates the "non-synchronized write to a tracked singleton field"
  check. The if-statement and ternary check-then-act detection now share
  this helper, so both cases apply identical field-write criteria and
  cannot drift apart. The if case still accepts any tracked field in the
  condition (its existing conservative behavior is left untouched); the
  ternary case keeps requiring the condition field to equal the
  assignment target, since that is what makes `f = f == null ? ... : f`
  a check-then-act race.
- Move the #6747 release notes entry from java-errorprone to a new
  java-multithreading category (NonThreadSafeSingleton is a
  multithreading rule).

Signed-off-by: 付典 <[email protected]>

* [java] NonThreadSafeSingleton: also extract shared null-check helper (#6747)

Further to extracting fieldWriteName, also extract isNullCheckOnField
for the condition check. The if-statement and ternary cases had two
near-identical condition-check blocks that differed only in
fields.contains(...) (if case, any tracked field) vs
fieldName.equals(...) (ternary case, same field read and written) --
exactly the kind of subtle difference that is hard to spot. Both now
call isNullCheckOnField(condition, targetField), where targetField is
null for the if case and the assignment target for the ternary case.

Behavior is unchanged; the if case still accepts any tracked field in
the condition (its long-standing conservative behavior), the ternary
case still requires the condition field to equal the assignment target.

Signed-off-by: 付典 <[email protected]>

---------

Signed-off-by: 付典 <[email protected]>
Co-authored-by: Sören Glimm <[email protected]>



To unsubscribe from these emails, change your notification settings at 
https://github.com/pmd/pmd/settings/notifications


_______________________________________________
Pmd-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pmd-commits

Reply via email to