Skip to content

x/tools/gopls/internal/analysis/modernize: minmax offers invalid fix when pattern appears beneath "else" #73576

Closed
@cuishuang

Description

@cuishuang

Go version

go 1.24.2

Output of go env in your module/workspace:

AR='ar'
CC='clang'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='0'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='clang++'
GCCGO='gccgo'
GO111MODULE='auto'
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/Users/xxx/Library/Caches/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/Users/xxx/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -arch arm64 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/9t/839s3jmj73bcgyp5x_xh3gw00000gn/T/go-build2973783802=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='/Users/xxxx/20250428/tools/gopls/go.mod'
GOMODCACHE='/Users/xxx/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/xxx/go'
GOPRIVATE=''
GOPROXY='https://goproxy.cn,direct'
GOROOT='/usr/local/go'
GOSUMDB='off'
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/xxx/Library/Application Support/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.24.2'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

package main

import "fmt"


func (f *Form) SetFocus(index int) *Form {
	if index < 0 {
		f.focusedElement = 0
	} else if index >= len(f.items)+len(f.buttons) {
		f.focusedElement = len(f.items) + len(f.buttons)
	} else {
		f.focusedElement = index
	}
	if f.changed != nil {
		f.changed(f.focusedElement)
	}
	return f
}

type Form struct {
	items          []string
	buttons        []string
	focusedElement int
	changed        func(index int)
}

func main() {
	// Create a new Form instance
	form := &Form{
		items:   []string{"Item 1", "Item 2", "Item 3"},
		buttons: []string{"Submit", "Cancel"},
		changed: func(index int) {
			fmt.Printf("Focused element changed to index: %d\n", index)
		},
	}

	// Set focus to different elements
	form.SetFocus(0)  // Focus on first item
	form.SetFocus(3)  // Focus on first button
	form.SetFocus(5)  // Attempt to focus beyond the last element
	form.SetFocus(-1) // Focus on the first element
}

The Output:

Focused element changed to index: 0
Focused element changed to index: 3
Focused element changed to index: 5
Focused element changed to index: 0

What did you see happen?

After executing modernize -fix -test ./..., it will become.

package main

import "fmt"


func (f *Form) SetFocus(index int) *Form {
	if index < 0 {
		f.focusedElement = 0
	} else f.focusedElement = min(index, len(f.items) + len(f.buttons))
	if f.changed != nil {
		f.changed(f.focusedElement)
	}
	return f
}

type Form struct {
	items          []string
	buttons        []string
	focusedElement int
	changed        func(index int)
}

func main() {
	// Create a new Form instance
	form := &Form{
		items:   []string{"Item 1", "Item 2", "Item 3"},
		buttons: []string{"Submit", "Cancel"},
		changed: func(index int) {
			fmt.Printf("Focused element changed to index: %d\n", index)
		},
	}

	// Set focus to different elements
	form.SetFocus(0)  // Focus on first item
	form.SetFocus(3)  // Focus on first button
	form.SetFocus(5)  // Attempt to focus beyond the last element
	form.SetFocus(-1) // Focus on the first element
}

Output:

# command-line-arguments
./main.go:10:9: syntax error: else must be followed by if or statement block

What did you expect to see?

package main

import "fmt"


func (f *Form) SetFocus(index int) *Form {
	if index < 0 {
		f.focusedElement = 0
	} else {
		f.focusedElement = min(index, len(f.items)+len(f.buttons))
	}
	if f.changed != nil {
		f.changed(f.focusedElement)
	}
	return f
}

type Form struct {
	items          []string
	buttons        []string
	focusedElement int
	changed        func(index int)
}

func main() {
	// Create a new Form instance
	form := &Form{
		items:   []string{"Item 1", "Item 2", "Item 3"},
		buttons: []string{"Submit", "Cancel"},
		changed: func(index int) {
			fmt.Printf("Focused element changed to index: %d\n", index)
		},
	}

	// Set focus to different elements
	form.SetFocus(0)  // Focus on first item
	form.SetFocus(3)  // Focus on first button
	form.SetFocus(5)  // Attempt to focus beyond the last element
	form.SetFocus(-1) // Focus on the first element
}

Metadata

Metadata

Assignees

Labels

ToolsThis 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