Skip to content
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

x/tools/gopls: range over func without variable shows 'simplify range expression' warning #65799

Open
k3forx opened this issue Feb 20, 2024 · 1 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.
Milestone

Comments

@k3forx
Copy link

k3forx commented Feb 20, 2024

gopls version

golang.org/x/tools/gopls v0.14.2 golang.org/x/tools/gopls@v0.14.2 h1:sIw6vjZiuQ9S7s0auUUkHlWgsCkKZFWDHmrge8LYsnc= github.com/BurntSushi/toml@v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak= github.com/google/go-cmp@v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/sergi/go-diff@v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= golang.org/x/exp/typeparams@v0.0.0-20221212164502-fae10dda9338 h1:2O2DON6y3XMJiQRAS1UWU+54aec2uopH3x7MAiqGW6Y= golang.org/x/mod@v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= golang.org/x/sync@v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ= golang.org/x/sys@v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= golang.org/x/telemetry@v0.0.0-20231114163143-69313e640400 h1:brbkEFfGwNGAEkykUOcryE/JiHUMMJouzE0fWWmz/QU= golang.org/x/text@v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= golang.org/x/tools@v0.14.1-0.20231114185516-c9d3e7de13fd h1:Oku7E+OCrXHyst1dG1z10etCTxewCHXNFLRlyMPbh3w= golang.org/x/vuln@v1.0.1 h1:KUas02EjQK5LTuIx1OylBQdKKZ9jeugs+HiqO5HormU= honnef.co/go/tools@v0.4.5 h1:YGD4H+SuIOOqsyoLOpZDWcieM28W47/zRO7f+9V3nvo= mvdan.cc/gofumpt@v0.4.0 h1:JVf4NN1mIpHogBj7ABpgOyZc65/UUOkKQFkoURsz4MM= mvdan.cc/xurls/v2@v2.4.0 h1:tzxjVAj+wSBmDcF6zBB7/myTy3gX9xvi8Tyr28AuQgc= go: go1.22.0

go env

❯ go env
GO111MODULE='on'
GOARCH='amd64'
GOBIN=''
GOCACHE='/Users/kanatamiyahana/Library/Caches/go-build'
GOENV='/Users/kanatamiyahana/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT='rangefunc'
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/kanatamiyahana/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/kanatamiyahana/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/kanatamiyahana/.asdf/installs/golang/1.22.0/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/kanatamiyahana/.asdf/installs/golang/1.22.0/go/pkg/tool/darwin_amd64'
GOVCS=''
GOVERSION='go1.22.0'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/kanatamiyahana/repos/snkrdunk.com/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/fn/jml4l1qs6q32rndh73fcqptc0000gn/T/go-build3540065755=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

I wrote the following program.

package main

type Seq[V any] func(yield func(V) bool)

func main() {
	var seq Seq[int] = func(yield func(int) bool) {
		for i := range 5 {
			yield(i)
		}
	}

	for _ = range seq {
		println("Hello, World!")
	}
}

What did you see happen?

I got the warning from gopls.

❯ gopls check ./main.go
/Users/kanatamiyahana/repos/scrap/go1.22/main.go:12:6-7: simplify range expression

If I re-write the code like below, then I get the another error ./main.go:12:12: cannot range over seq (variable of type Seq[int])

package main

type Seq[V any] func(yield func(V) bool)

func main() {
	var seq Seq[int] = func(yield func(int) bool) {
		for i := range 5 {
			yield(i)
		}
	}

	for range seq { // cannot range over seq (variable of type Seq[int])
		println("Hello, World!")
	}
}

What did you expect to see?

I expect to get no warning.

Editor and settings

Here is my VSCode settings. I only pasted Go related settings.

  "[go]": {
    "editor.insertSpaces": false,
    "editor.defaultFormatter": "golang.go"
  },
  "go.goroot": "/Users/kanatamiyahana/.asdf/installs/golang/1.22.0/go",
  "go.gopath": "/Users/kanatamiyahana/go",
  "gopls": {
    "buildFlags": ["-tags=parallel,serial,integration"]
  },

Logs

No response

@k3forx k3forx added gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository. labels Feb 20, 2024
@gopherbot gopherbot added this to the Unreleased milestone Feb 20, 2024
@hyangah
Copy link
Contributor

hyangah commented Feb 20, 2024

I think this is an interesting consequences between the simplifyrange analyzer and the inconsistency discussed in #65236
Depending on how issue 65236 is resolved, it looks like we also need to adjust the analyzer and also gofmt -s.

@hyangah hyangah modified the milestones: Unreleased, gopls/v0.16.0 Feb 20, 2024
alexandreLamarre added a commit to alexandreLamarre/ro that referenced this issue May 1, 2024
Signed-off-by: Alexandre Lamarre <alex7285@gmail.com>
alexandreLamarre added a commit to alexandreLamarre/ro that referenced this issue May 2, 2024
Signed-off-by: Alexandre Lamarre <alex7285@gmail.com>
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