Skip to content

x/tools/gopls/internal/analysis/modernize: minmax offers invalid fix #73576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
cuishuang opened this issue May 2, 2025 · 1 comment
Open
Labels
gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@cuishuang
Copy link
Contributor

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
}
@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels May 2, 2025
@gopherbot gopherbot added this to the Unreleased milestone May 2, 2025
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/669495 mentions this issue: gopls/internal/analysis/modernize: fix bug in minmax analyzer that incorrectly handles nested if-else-if structures

@adonovan adonovan modified the milestones: Unreleased, gopls/v0.19.0 May 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

3 participants