Skip to content

UnnecessaryAsync misses the split declaration/assignment form #5829

@Chordrain

Description

@Chordrain

Error Prone version

Error Prone 2.49.0 (javac plugin) — javac 21.0.8, source/target 21.

Check name

UnnecessaryAsync

Description

UnnecessaryAsync flags atomic variables that are initialized and do not escape the current scope (so the atomicity is unnecessary). The matcher recognizes the combined declarator-with-initializer form AtomicInteger a = new AtomicInteger(0). Splitting it into AtomicInteger a; a = new AtomicInteger(0); is a semantics-preserving rewrite — the variable is still initialized exactly once before use and still does not escape — yet the rule stops firing.

Reproducer

// BEFORE — 1 finding
import java.util.concurrent.atomic.AtomicInteger;
class C {
  int f() {
    AtomicInteger a = new AtomicInteger(0);
    a.incrementAndGet();
    return a.get();
  }
}
// AFTER — equivalent rewrite, 0 findings
import java.util.concurrent.atomic.AtomicInteger;
class C {
  int f() {
    AtomicInteger a;
    a = new AtomicInteger(0);
    a.incrementAndGet();
    return a.get();
  }
}

Expected behavior

UnnecessaryAsync should report the same finding on BEFORE and AFTER, since the variable remains initialized-once and non-escaping in both forms.

Actual behavior

  • BEFORE: 1 finding (Variables which are initialized and do not escape the current scope ...).
  • AFTER: 0 findings.

The matcher only recognizes the combined declarator-with-initializer form and misses the equivalent split-assignment form.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions