Skip to content

x/tools/gopls: modernize slices.Contains rewrite affects program behaviour #76210

@mdelah

Description

@mdelah

Go version

go version go1.25.0 linux/amd64

Output of go env in your module/workspace:

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/miles/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/miles/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build648219924=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/home/miles/repos/example/go.mod'
GOMODCACHE='/home/miles/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/miles/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/miles/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.25.0.linux-amd64'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/miles/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/miles/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.25.0.linux-amd64/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.25.0'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

Given example source file example.go:

package main

func IsEmptyOrContains(x []string, y string) bool {
        res := len(x) == 0
        for _, z := range x {
                if y == z {
                        res = true
                        break
                }
        }
        return res
}

(The function returns true if x has length zero, or if it contains the given value y)

Running modernize suggests use of slices.Contains:

$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest example.go 
go: golang.org/x/tools/gopls@v0.20.0 requires go >= 1.24.2; switching to go1.24.10
/home/miles/repos/example/example.go:5:2: Loop can be simplified using slices.Contains
exit status 3

Apply suggested fix:

$ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix example.go 
go: golang.org/x/tools/gopls@v0.20.0 requires go >= 1.24.2; switching to go1.24.10

What did you see happen?

The rewritten code no longer returns true in the case that x has zero length:

package main

import "slices"

func IsEmptyOrContains(x []string, y string) bool {
        res := slices.Contains(x, y)
        return res
}

What did you expect to see?

No fix applied, or alternatively something along the lines of

package main

import "slices"

func IsEmptyOrContains(x []string, y string) bool {
        res := len(x) == 0
        if slices.Contains(x, y) {
            res = true
        }
        return res
}

Metadata

Metadata

Assignees

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