Skip to content

x/tools/go/analysis/passes/fieldalignment: suggested fix producing garbled output #43233

Open
@shreyaskarnik

Description

@shreyaskarnik

What version of Go are you using (go version)?

$ go version
go version go1.15.5 darwin/amd64

Does this issue reproduce with the latest release?

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/user/Library/Caches/go-build"
GOENV="/Users/user/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/user/work/go/pkg/mod"
GOOS="darwin"
GOPATH="/Users/user/work/go"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.15.5/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.15.5/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/tmp/test-gopls/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/vh/853pkypj3tnd78rgvfhgc9kr0000gn/T/go-build258818279=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

enabled the following analysis after upgrading to gopls 0.6.0
"analyses": {
"fieldalignment": true
}

What did you expect to see?

Following sample code to demonstrate the issue:

package main

import "fmt"

type Settings struct {
	AllowedEvents []*string // allowed events
	BlockedEvents []*string // blocked events
	APIVersion    string    `mapstructure:"api_version"`
	BaseURL       string    `mapstructure:"base_url"`
	AccessToken   string    `mapstructure:"access_token"`
}
func main() {
	s := &Settings{
		APIVersion:    "",
		BaseURL:       "",
		AccessToken:   "",
		AllowedEvents: []*string{},
		BlockedEvents: []*string{},
	}
	fmt.Println(s)
}

applied fieldalignment codelens in vscode

What did you see instead?

type Settings struct {
	APIVersion    string    `mapstructure:"api_version"`
	BaseURL       string    `mapstructure:"base_url"`
	AccessToken   string    `mapstructure:"access_token"`
	AllowedEvents []*string
	BlockedEvents [ // allowed events
	]*string      // blocked events
}

initially I thought that I made a mistake with the comments so I tried the following struct

type Settings struct {
	// test1
	AllowedEvents []*string
	// test2
	BlockedEvents []*string
	APIVersion    string `mapstructure:"api_version"`
	BaseURL       string `mapstructure:"base_url"`
	AccessToken   string `mapstructure:"access_token"`
}

after applying fieldanalysis codelens to above struct it became garbled too

type Settings struct {
	APIVersion    string `mapstructure:"api_version"`
	BaseURL       string `mapstructure:"base_url"`
	AccessToken   string `mapstructure:"access_token"`
	AllowedEvents [ // test1
	]*string
	BlockedEvents [ // test2
	]*string
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    AnalysisIssues related to static analysis (vet, x/tools/go/analysis)NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.RefactoringIssues related to refactoring toolsToolsThis label describes issues relating to any tools in the x/tools repository.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions