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

cmd/vet: warn about more malformed JSON struct tags #37210

Open
pfwgbe opened this issue Feb 13, 2020 · 4 comments
Open

cmd/vet: warn about more malformed JSON struct tags #37210

pfwgbe opened this issue Feb 13, 2020 · 4 comments
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@pfwgbe
Copy link

pfwgbe commented Feb 13, 2020

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

$ go version
go version go1.13.4 darwin/amd64

Does this issue reproduce with the latest release?

Yes, the problem appears with 1.13.8 as well.

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/XYZ/Library/Caches/go-build"
GOENV="/Users/XYZ/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GONOPROXY=""
GONOSUMDB="example.com/*"
GOOS="darwin"
GOPATH="/Users/XYZ/go"
GOPRIVATE=""
GOPROXY="http://proxy.example.com"
GOROOT="/Users/XYZ/go/go1.13.4"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/XYZ/go/go1.13.4/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
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/fm/6cbjfz6s33qbxn77r5lt89cc0000gp/T/go-build495195143=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I ran go vet on the following code:

package main

import (
	"fmt"
	"encoding/json"
)

type S struct {
	Foo int `json:"fnord.foo"`
}

func main() {
	data := []byte(`
		{
			"fnord": {
				"foo": 1
			}
		}
	`)

	var s S
	err := json.Unmarshal(data, &s)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%#v\n", s)
}

What did you expect to see?

I expected go vet to warn me about the undecodable parts of the struct, with something like "encoding/json does not support dot-separated paths"

What did you see instead?

go vet did not print any output and had a zero exit value, indicating that there are no problems with the code.

@josharian josharian changed the title cmd/vet should warn about malformed struct tags for JSON decoding cmd/vet: warn about more malformed JSON struct tags Feb 13, 2020
@josharian
Copy link
Contributor

Seems reasonable. Should fit nicely into the existing vet struct tags check. Should be pretty straightforward to implement for anyone wanting to try their hand on vet.

cc @mvdan @robpike

@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Feb 15, 2020
@dmitshur dmitshur added this to the Backlog milestone Feb 15, 2020
@gopherbot
Copy link

Change https://golang.org/cl/236522 mentions this issue: analysis/passes/stucttag: error on . field accessor in JSON tag

@dmitshur dmitshur added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels May 19, 2022
@seankhliao
Copy link
Member

I don't think this is something that can be flagged,
{"fnord.foo": 1} is a valid json object that is supported by the current syntax https://go.dev/play/p/faEAfGx8uzE

cc @mvdan

@mvdan
Copy link
Member

mvdan commented Jun 13, 2022

I tend to agree with @seankhliao, and we likely can't change the existing behavior either. Also note that vet has a frequency criteria for its checks, and I have never encountered this kind of mistake myself.

cc @dsnet in case he disagrees here

@adonovan adonovan added the Analysis Issues related to static analysis (vet, x/tools/go/analysis) label Apr 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

7 participants