Skip to content

x/tools/gopls: "extract method" creates invalid code when extracting free "continue" statement #63394

@adonovan

Description

@adonovan

When extracting statements from the body of a loop that include continue, but not the enclosing loop, gopls seems not to notice the implications of the continue statement, and this generates invalid code. Extracting:

func f(cond bool) {
	for range "abc" {
|		if cond {
|			continue
|		}
|		println(0)
	}
}

becomes:

func f(cond bool) {
	for range "abc" {
		newFunction1(cond)
	}
}

func newFunction1(cond bool) {
	if cond {
		continue // error: continue not in loop
	}
	println(0)
}

This is a pity since the extract logic seems to go to great trouble to deal with return continuations.

Metadata

Metadata

Labels

RefactoringIssues related to refactoring toolsToolsThis label describes issues relating to any tools in the x/tools repository.goplsIssues related to the Go language server, gopls.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions