Skip to content

x/tools/go/analysis/passes/copylock: fails to report re-assignment of sync.WaitGroup variables #71357

Open
@merrickclay

Description

@merrickclay

Proposal Details

Reassignment of variables of type sync.WaitGroup is likely to cause bugs due to the potential to overwrite a WaitGroup with a non-zero counter value with a WaitGroup that has a zero counter. I can't think of any scenarios whereby reassigning a variable of type sync.WaitGroup would be required or useful. The waitgroup analysis pass should be extended to report reassignments of sync.WaitGroup variables.

Example

package main

import (
	"sync"
)

type Object struct {
	wg sync.WaitGroup
}

var wg0 sync.WaitGroup

func main() {
	var wg1 sync.WaitGroup                    // ok
	wg2 := sync.WaitGroup{}                   // ok
	var wg3 sync.WaitGroup = sync.WaitGroup{} // ok

	obj := Object{
		wg: sync.WaitGroup{},  // ok
	}

	wg0 = sync.WaitGroup{} // want: variable of type sync.WaitGroup reassigned
	wg1 = sync.WaitGroup{} // want: variable of type sync.WaitGroup reassigned

	obj.wg = sync.WaitGroup{} // want: variable of type sync.WaitGroup reassigned

	wg0.Wait()
	wg1.Wait()
	wg2.Wait()
	wg3.Wait()
}

Should this proposal be approved, I'd like to contribute the aforementioned changes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.ToolsThis label describes issues relating to any tools in the x/tools repository.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions