Skip to content

x/tools/gopls/internal/analysis/modernize: slicescontains analyzer suggests invalid refactoring when function parameter type doesn't match slice element type #74938

@cuishuang

Description

@cuishuang

Go version

go1.24.3

Output of go env in your module/workspace:

AR='ar'
CC='cc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='c++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN='/Users/mac/go/bin'
GOCACHE='/Users/mac/Library/Caches/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/Users/mac/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/tq/m955dwkd1519phkp2hwpv9_80000gn/T/go-build2400307511=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='/dev/null'
GOMODCACHE='/Users/mac/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/mac/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/mac/Library/Application Support/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.24.3'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

package main

import "fmt"

type Expression interface{}

type Column struct {
	UniqueID int64
}

func (col *Column) EqualColumn(expr Expression) bool {
	other, ok := expr.(*Column)
	if !ok {
		return false
	}
	return col.UniqueID == other.UniqueID
}

func (col *Column) InColumnArray(cols []*Column) bool {
	for _, c := range cols {
		if col.EqualColumn(c) { // c is *Column, but EqualColumn expects Expression
			return true
		}
	}
	return false
}

func main() {

	cols := []*Column{
		{UniqueID: 1},
		{UniqueID: 2},
		{UniqueID: 3},
	}
	colToCheck := &Column{UniqueID: 2}
	fmt.Println("Is column in array?", colToCheck.InColumnArray(cols)) // true

	colToCheck2 := &Column{UniqueID: 5}
	fmt.Println("Is column in array?", colToCheck2.InColumnArray(cols)) // false
}

The Output:

Is column in array? true
Is column in array? false

What did you see happen?

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

package main

import "slices"

import "fmt"

type Expression interface{}

type Column struct {
	UniqueID int64
}

func (col *Column) EqualColumn(expr Expression) bool {
	other, ok := expr.(*Column)
	if !ok {
		return false
	}
	return col.UniqueID == other.UniqueID
}

func (col *Column) InColumnArray(cols []*Column) bool {
	return slices.ContainsFunc(cols, col.EqualColumn)
}

func main() {

	cols := []*Column{
		{UniqueID: 1},
		{UniqueID: 2},
		{UniqueID: 3},
	}
	colToCheck := &Column{UniqueID: 2}
	fmt.Println("Is column in array?", colToCheck.InColumnArray(cols)) // true

	colToCheck2 := &Column{UniqueID: 5}
	fmt.Println("Is column in array?", colToCheck2.InColumnArray(cols)) // false
}

Output:

# command-line-arguments
./main2.go:22:28: in call to slices.ContainsFunc, S (type []*Column) does not satisfy ~[]E

What did you expect to see?

Do not take action on this situation.

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions