Skip to content

cmd/fix,x/tools/go/analysis/passes/modernize: slicescontains produces invalid result #77616

@atzoum

Description

@atzoum

Go version

go version go1.26.0 darwin/arm64

Output of go env in your module/workspace:

AR='ar'
CC='clang'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='clang++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/Users/aristzoumas/Library/Caches/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/Users/aristzoumas/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/kj/1cxl4y917c7d5zr5d3_159n00000gn/T/go-build3966991578=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='/tmp/demo-gofix/go.mod'
GOMODCACHE='/Users/aristzoumas/go/pkg/mod'
GONOPROXY='github.com/rudderlabs'
GONOSUMDB='github.com/rudderlabs'
GOOS='darwin'
GOPATH='/Users/aristzoumas/go'
GOPRIVATE='github.com/rudderlabs'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/aristzoumas/.gobrew/current/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/aristzoumas/Library/Application Support/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/aristzoumas/.gobrew/current/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.26.0'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

run go fix ./... against main.go

package main

const (
	FULL_SYNC        = "full_sync"
	INCREMENTAL_SYNC = "incremental_sync"
)

func main() {
	type StreamConfig struct {
		SupportedSyncModes []string
	}

	streamConfig := StreamConfig{
		SupportedSyncModes: []string{FULL_SYNC, INCREMENTAL_SYNC},
	}
	syncMode := FULL_SYNC
	for _, syncOption := range streamConfig.SupportedSyncModes {
		if syncOption == INCREMENTAL_SYNC {
			syncMode = INCREMENTAL_SYNC
			break
		}
	}
	var finalSyncMode string = syncMode
	println("Selected Sync Mode:", finalSyncMode)
}

What did you see happen?

produces wrong code that doesn't compile

package main

import "slices"

const (
	FULL_SYNC        = "full_sync"
	INCREMENTAL_SYNC = "incremental_sync"
)

func main() {
	type StreamConfig struct {
		SupportedSyncModes []string
	}

	streamConfig := StreamConfig{
		SupportedSyncModes: []string{FULL_SYNC, INCREMENTAL_SYNC},
	}
	syncMode := slices.Contains(streamConfig.SupportedSyncModes, INCREMENTAL_SYNC)
	var finalSyncMode string = syncMode
	println("Selected Sync Mode:", finalSyncMode)
}

What did you expect to see?

better results, e.g.

package main

import "slices"

const (
	FULL_SYNC        = "full_sync"
	INCREMENTAL_SYNC = "incremental_sync"
)

func main() {
	type StreamConfig struct {
		SupportedSyncModes []string
	}

	streamConfig := StreamConfig{
		SupportedSyncModes: []string{FULL_SYNC, INCREMENTAL_SYNC},
	}
	syncMode := FULL_SYNC
	if slices.Contains(streamConfig.SupportedSyncModes, INCREMENTAL_SYNC) {
		syncMode = INCREMENTAL_SYNC
	}
	var finalSyncMode string = syncMode
	println("Selected Sync Mode:", finalSyncMode)
}

Metadata

Metadata

Assignees

Labels

BugReportIssues describing a possible bug in the Go implementation.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions