Skip to content

cmd/vet: structtag field repeats json warning with valid override #40102

Description

@tylfin

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

$ go version
go version go1.14.1 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/[XX]/Library/Caches/go-build"
GOENV="/Users/[XX]/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/[XX]/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.14.1/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.14.1/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
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/w9/kv18_grs18x65fyzv1p5mvk00000gn/T/go-build893878327=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

When embedding two structs with duplicate JSON tags, e.g.:

type A struct {
   name string `json:"name"`
   ... // additional fields
}

type B struct {
   name string `json:"name"`
   ... // additional fields
}

type C struct {
    A
    B // raises go vet structtag warning
}

A structtag warning is raised by go vet. I think a reasonable workaround to the warning is to override the duplicate-tag on the top level, per extra rules here: https://golang.org/pkg/encoding/json/#Marshal, e.g.

type C struct {
   A
   B
   name string `json:"name"`
}

But go vet still raises the warning.

Note I did add the flag -structtag=false as a workaround but there are cases where I would like to be alerted, but only when no override has been set

What did you expect to see?

go vet treats top-level structtag overrides as a valid solution and no longer raises a warning.

What did you see instead?

go vet ignores the top-level override, resulting in structtag: struct field X repeats json tag "[X]" also at XXXX.go:XXX (govet)

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.ToolsThis label describes issues relating to any tools in the x/tools repository.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions