Skip to content

Recognize split declaration/assignment in UnnecessaryStringBuilder - #6069

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

Recognize split declaration/assignment in UnnecessaryStringBuilder#6069
tanvir-ux wants to merge 1 commit into
google:masterfrom
tanvir-ux:fix-5828-unnecessary-stringbuilder-split-assignment

Conversation

@tanvir-ux

Copy link
Copy Markdown

Description

Fixes #5828.

UnnecessaryStringBuilder already flags locals like:

StringBuilder sb = new StringBuilder("x");

but misses the equivalent split form:

StringBuilder sb;
sb = new StringBuilder("x");

This change treats a sole initializing assignment the same as a declarator
initializer, and ignores that assignment when checking whether the local
still needs to stay a StringBuilder.

Testing

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

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

`StringBuilder x; x = new StringBuilder(...);` is observationally the same
as `StringBuilder x = new StringBuilder(...);`, but the check only handled
the combined declarator form. Teach it to treat a sole initializing
assignment the same way, and ignore that assignment when deciding whether
the local is still required to be a StringBuilder.

Fixes google#5828
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.

UnnecessaryStringBuilder misses the split declaration/assignment form

1 participant