Skip to content

x/tools/gopls/internal/analysis/modernize: fmtappendf suggests invalid fix #74709

@adonovan

Description

@adonovan
xtools$ cat b.go
package tools

import "fmt"

type S struct{ Bytes []byte }

var _ = struct{ A S }{
	A: S{
		Bytes: []byte(
			fmt.Sprintf("%d", 0),
		),
	},
}
xtools$ go run ./gopls/internal/analysis/modernize/cmd/modernize -fix ./b.go && cat b.go
package tools

import "fmt"

type S struct{ Bytes []byte }

var _ = struct{ A S }{
        A: S{
                Bytes: 
                        fmt.Appendf(nil, "%d", 0),
                ,
        },
}

This code doesn't compile because the fix turned a conversion T(x) into a simple expression x, but a conversion is permitted to have a comma after the x, as in T(x,) even when the expression x alone is not.

Also, the fix fails to delete the spaces between "[]byte(" and "fmt", and between ")" and "," (though there are none here), which leads to strange formatting. However, deleting the spaces would also delete comments. There is no perfect answer here.

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