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: supress analysis/simplifycompositelit on a generated code #67733

Closed
ohir opened this issue May 30, 2024 · 5 comments
Closed

x/tools/gopls: supress analysis/simplifycompositelit on a generated code #67733

ohir opened this issue May 30, 2024 · 5 comments
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

@ohir
Copy link

ohir commented May 30, 2024

gopls version

Build info
----------
golang.org/x/tools/gopls v0.15.3
    golang.org/x/tools/[email protected] h1:zbdOidFrPTc8Bx0YrN5QKgJ0zCjyGi0L27sKQ/bDG5o=
    github.com/BurntSushi/[email protected] h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
    github.com/google/[email protected] h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
    golang.org/x/exp/[email protected] h1:2O2DON6y3XMJiQRAS1UWU+54aec2uopH3x7MAiqGW6Y=
    golang.org/x/[email protected] h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8=
    golang.org/x/[email protected] h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
    golang.org/x/[email protected] h1:vcVnuftN4J4UKLRcgetjzfU9FjjgXUUYUc3JhFplgV4=
    golang.org/x/[email protected] h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
    golang.org/x/[email protected] h1:uH9jJYgeLCvblH0S+03kFO0qUDxRkbLRLFiKVVDl7ak=
    golang.org/x/[email protected] h1:KUas02EjQK5LTuIx1OylBQdKKZ9jeugs+HiqO5HormU=
    honnef.co/go/[email protected] h1:oFEHCKeID7to/3autwsWfnuv69j3NsfcXbvJKuIcep8=
    mvdan.cc/[email protected] h1:G3QvahNDmpD+Aek/bNOLrFR2XC6ZAdo62dZu65gmwGo=
    mvdan.cc/xurls/[email protected] h1:lyBNOm8Wo71UknhUs4QTFUNNMyxy2JEIaKKo0RWOh+8=
go: go1.22.1

go env

# empty items skipped
GOARCH='amd64'
GOBIN='/Path/To/bin'
GOCACHE='/Volumes/TEMP/gocache'
GOENV='/Path/To/go/env'
GOHOSTARCH='amd64'
GOHOSTOS='darwin'
GOMODCACHE='/Path/To/src/pkg/mod'
GONOPROXY='example.com'
GONOSUMDB='example.com'
GOOS='darwin'
GOPATH='/Path/To/src'
GOPRIVATE='example.com'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Path/To/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR='/Path/To/TEMP/go'
GOTOOLCHAIN='auto'
GOTOOLDIR='/Path/To/go/pkg/tool/darwin_amd64'
GOVCS='public:git'
GOVERSION='go1.22.3'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Path/To/project/module/go.mod'
GOWORK='/Path/To/project/go.work'
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=/Path/To/TEMP/go/go-build1959820345=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

I have code that has been generated with fmt.Printf("%#v",somestruct) in files that are being properly
marked as such with // Code generated with somegen DO NOT EDIT. header in the first line.
Gopls errornously suggests to simplify Printf produced literals polluting the "Problems"
pane in the IDE (while properly refusing to format file on save).

Repr:

// Code generated with somegen DO NOT EDIT.
package main

import "fmt"

type ProofOB struct{
	A [][]string
}
func main(){
	// x := ProofOB{A: [][]string{{"some value"}}}
	x := ProofOB{A: [][]string{[]string{"some value"}}}
	fmt.Printf("%#v\n",x)
}
// Result: main.ProofOB{A:[][]string{[]string{"some value"}}}

What did you see happen?

hundreds of:

[{
	"resource": "[...cut...]/generated_test.go",
	"owner": "_generated_diagnostic_collection_name_#4",
	"code": {
		"value": "default",
		"target": {
			"$mid": 1,
			"path": "/golang.org/x/tools/gopls/internal/analysis/simplifycompositelit",
			"scheme": "https",
			"authority": "pkg.go.dev"
		}
	},
	"severity": 4,
	"message": "redundant type from array, slice, or map composite literal",
	"source": "simplifycompositelit",
	"startLineNumber": 2997,
	"startColumn": 104,
	"endLineNumber": 2997,
	"endColumn": 212,
	"tags": [
		1
	]
}]

What did you expect to see?

I expected Gopls to know from the "Code generated" header that code here CAN NOT be simplified.

Editor and settings

"gopls": {  
        "ui.diagnostic.analyses": {
            "simplifycompositelit": true,
        },
        "formatting.gofumpt": true,
    },

Note: simplifycompositelit is by default true, but I had to have set it to false as a workaround now.

Logs

No response

@ohir ohir 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 May 30, 2024
@gopherbot gopherbot added this to the Unreleased milestone May 30, 2024
@findleyr
Copy link
Contributor

Agreed, we should disable all "simplifiers" on generated code.

@findleyr findleyr modified the milestones: Unreleased, gopls/v0.17.0 May 31, 2024
@idnandre
Copy link

Can i take this one?

@timothy-king
Copy link
Contributor

@idnandre Sure. We won't assign this to you, but if you chip in and send CLs, we will review them. (Take a look at https://go.dev/doc/contribute if you haven't before.)

You probably want to wait for gopls to require a Go Version >= 1.21 e.g. for #65917 to close. After 1.21 is available the function ast.IsGenerated will be available.

(You technically don't have to wait, but everything becomes about 4x harder. It requires create a library with built tags that makes sense both with and without go 1.21, and interprets "IsGenerated" in the correct direction whenever you use it when ast.IsGenerated is not available. Or duplicates the needed code. And write tests both before and after 1.21.)

@idnandre
Copy link

Hi @timothy-king
Okay, I will work on it.

Thank you.

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/598835 mentions this issue: gopls: add skipped gopls analysis simplifycompositelit if its on generated code

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

5 participants