Skip to content

Recognize split declaration/assignment in UnnecessaryAsync - #6070

Open
tanvir-ux wants to merge 1 commit into
google:masterfrom
tanvir-ux:fix-5829-unnecessary-async-split-assignment
Open

Recognize split declaration/assignment in UnnecessaryAsync#6070
tanvir-ux wants to merge 1 commit into
google:masterfrom
tanvir-ux:fix-5829-unnecessary-async-split-assignment

Conversation

@tanvir-ux

Copy link
Copy Markdown

Description

Fixes #5829.

UnnecessaryAsync already flags locals like:

AtomicInteger a = new AtomicInteger(0);

but misses the equivalent split form:

AtomicInteger a;
a = new AtomicInteger(0);

This change finds a sole initializing assignment when the declarator has
no initializer, treats that assignment as non-escaping (same as the
combined form), and removes the assignment statement when applying the
suggested rewrite to a plain local.

Testing

mvn -pl core -am -Dtest=UnnecessaryAsyncTest -DfailIfNoTests=false -Dsurefire.failIfNoSpecifiedTests=false test

All 13 UnnecessaryAsyncTest cases passed (including new split-form coverage).

`AtomicInteger a; a = new AtomicInteger(0);` is equivalent to
`AtomicInteger a = new AtomicInteger(0);`, but the check required an
initializer on the VariableTree. Find a sole initializing assignment,
treat it like a declarator initializer for escape analysis, and delete
that assignment statement when rewriting to a plain local.

Fixes google#5829
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UnnecessaryAsync misses the split declaration/assignment form

1 participant