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.
Error Prone version
Error Prone 2.49.0 (javac plugin) — javac 21.0.8, source/target 21.
Check name
UnnecessaryAsync
Description
UnnecessaryAsyncflags 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 formAtomicInteger a = new AtomicInteger(0). Splitting it intoAtomicInteger 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
Expected behavior
UnnecessaryAsyncshould report the same finding on BEFORE and AFTER, since the variable remains initialized-once and non-escaping in both forms.Actual behavior
Variables which are initialized and do not escape the current scope ...).The matcher only recognizes the combined declarator-with-initializer form and misses the equivalent split-assignment form.