Skip to content

go/analysis/passes/modernize: make waitgroupgo more conservative - #709

Open
ElenaSeg wants to merge 1 commit into
golang:masterfrom
ElenaSeg:modernize-waitgroupgo-early-return
Open

ElenaSeg wants to merge 1 commit into
golang:masterfrom
ElenaSeg:modernize-waitgroupgo-early-return

Conversation

@ElenaSeg

@ElenaSeg ElenaSeg commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

The waitgroupgo fix removes a trailing wg.Done() call and lets
WaitGroup.Go call Done when the function returns. If the function
literal can return before the trailing Done, the rewrite changes
behavior: Done now also runs on the early path. Code that already
calls Done before the early return then panics with a negative
WaitGroup counter. For example:

wg.Add(1)
go func() {
	if skip {
		wg.Done()
		return
	}
	wg.Done()
}()

was rewritten to

wg.Go(func() {
	if skip {
		wg.Done()
		return
	}
})

Do not report the trailing wg.Done() form when the function literal
contains a return statement outside nested function literals.

Also, to be safe, do not report either form when the function
literal uses the WaitGroup other than in the Done call of the
pattern, for example to call Done again or to pass it to other code.
Such code is often wrong, and the fix is no longer offered for it.

Fixes golang/go#82876

@gopherbot

Copy link
Copy Markdown
Contributor

This PR (HEAD: 4567b47) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/tools/+/861181.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Madeline Kalil:

Patch Set 1: Commit-Queue+1

(2 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/861181.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com:

Patch Set 1:

Dry run: CV is trying the patch.

Bot data: {"action":"start","triggered_at":"2026-11-11T21:37:25Z","revision":"176831f41271cbb7b30d136ad97c979acaf869de"}


Please don’t reply on this GitHub thread. Visit golang.org/cl/861181.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Madeline Kalil:

Patch Set 1: -Commit-Queue

(Performed by <GERRIT_ACCOUNT_60063> on behalf of <GERRIT_ACCOUNT_3771561>)


Please don’t reply on this GitHub thread. Visit golang.org/cl/861181.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com:

Patch Set 1:

This CL has failed the run. Reason:

Tryjob golang/try/x_tools-gotip-linux-arm64_debian13 has failed with summary (view all results):


To reproduce, try gomote repro 8665394285514955361.

Additional links for debugging:


Please don’t reply on this GitHub thread. Visit golang.org/cl/861181.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com:

Patch Set 1: LUCI-TryBot-Result-1


Please don’t reply on this GitHub thread. Visit golang.org/cl/861181.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Madeline Kalil:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/861181.
After addressing review feedback, remember to publish your drafts!

@ElenaSeg
ElenaSeg force-pushed the modernize-waitgroupgo-early-return branch from 4567b47 to 2ce3a38 Compare November 14, 2026 22:20
@gopherbot

Copy link
Copy Markdown
Contributor

This PR (HEAD: 2ce3a38) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/tools/+/861181.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Елена Егорова:

Patch Set 1:

(2 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/861181.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Madeline Kalil:

Patch Set 2: Code-Review+2


Please don’t reply on this GitHub thread. Visit golang.org/cl/861181.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Madeline Kalil:

Patch Set 2: Commit-Queue+1

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/861181.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com:

Patch Set 2:

Dry run: CV is trying the patch.

Bot data: {"action":"start","triggered_at":"2026-11-16T17:18:04Z","revision":"96880bae58bd9345c659b6632a8d53954a89bcd2"}


Please don’t reply on this GitHub thread. Visit golang.org/cl/861181.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Madeline Kalil:

Patch Set 2: -Commit-Queue

(Performed by <GERRIT_ACCOUNT_60063> on behalf of <GERRIT_ACCOUNT_3771561>)


Please don’t reply on this GitHub thread. Visit golang.org/cl/861181.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com:

Patch Set 2:

This CL has passed the run


Please don’t reply on this GitHub thread. Visit golang.org/cl/861181.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com:

Patch Set 2: LUCI-TryBot-Result+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/861181.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Alan Donovan:

Patch Set 2: Code-Review+2

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/861181.
After addressing review feedback, remember to publish your drafts!

@ElenaSeg
ElenaSeg force-pushed the modernize-waitgroupgo-early-return branch from 2ce3a38 to 642649c Compare November 18, 2026 21:46
@ElenaSeg ElenaSeg changed the title go/analysis/passes/modernize: skip waitgroupgo fix after a return go/analysis/passes/modernize: make waitgroupgo more conservative Nov 18, 2026
@gopherbot

Copy link
Copy Markdown
Contributor

This PR (HEAD: 642649c) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/tools/+/861181.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Елена Егорова:

Patch Set 2:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/861181.
After addressing review feedback, remember to publish your drafts!

The waitgroupgo fix removes a trailing wg.Done() call and lets
WaitGroup.Go call Done when the function returns. If the function
literal can return before the trailing Done, the rewrite changes
behavior: Done now also runs on the early path. Code that already
calls Done before the early return then panics with a negative
WaitGroup counter. For example:

	wg.Add(1)
	go func() {
		if skip {
			wg.Done()
			return
		}
		wg.Done()
	}()

was rewritten to

	wg.Go(func() {
		if skip {
			wg.Done()
			return
		}
	})

Do not report the trailing wg.Done() form when the function literal
contains a return statement outside nested function literals.

Also, to be safe, do not report either form when the function
literal uses the WaitGroup other than in the Done call of the
pattern, for example to call Done again or to pass it to other code.
Such code is often wrong, and the fix is no longer offered for it.

Fixes golang/go#82876
@ElenaSeg
ElenaSeg force-pushed the modernize-waitgroupgo-early-return branch from 642649c to 5a893a7 Compare November 18, 2026 22:23
@gopherbot

Copy link
Copy Markdown
Contributor

Message from Alan Donovan:

Patch Set 3:

(3 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/861181.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

This PR (HEAD: 5a893a7) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/tools/+/861181.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Alan Donovan:

Patch Set 4: Code-Review+2


Please don’t reply on this GitHub thread. Visit golang.org/cl/861181.
After addressing review feedback, remember to publish your drafts!

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.

x/tools/go/analysis/passes/modernize: waitgroupgo: trailing Done with an early return changes behavior

2 participants