Skip to content

x/tools/go/analysis/passes/modernize: rangeint transformation is unsound when index value is used after loop #77161

@eikemeier

Description

@eikemeier

golang/go#71952 mentions “later uses”, golang/go#76880 “implicitly used”, but there are at least two ways to use a value via aliasing:

func closure() int {
	var i int
	f := func() int { return i }

	for i = 0; i < 10; i++ {
	}

	return f()
}

func address() int {
	var i int
	p := &i

	for i = 0; i < 10; i++ {
	}

	return *p
}

Both function return different values when the loop is transformed into a range loop (Go Playground).

modernizer checks for defer (go.dev/cl/701715), but not general uncalled closures or addresses.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.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

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions