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: structtag false positives about json:"-,omitempty"
for unexported fields
#51298
Comments
I think the root cause is in the tools. github.com/golang/tools/go/analysis/passes/structtag for _, enc := range [...]string{"json", "xml"} {
switch reflect.StructTag(tag).Get(enc) {
// Ignore warning if the field not exported and the tag is marked as
// ignored.
case "", "-":
default:
pass.Reportf(field.Pos(), "struct field %s has %s tag but is not exported", field.Name(), enc)
return
}
} |
Change https://go.dev/cl/387114 mentions this issue: |
My understanding is that the vet check is WAI.
is naming the unexported field of X This is my reading of https://pkg.go.dev/encoding/json:
and
Someone authoritative on "encoding/json" want to weight in? If this is WAI, we might want to add additional checks so this case is easier to understand. Open to suggestions about how. (Complain about using "-" on "a" and "b" as they are unexported?) I think new checks would need a proposal. |
cc owner @rsc https://dev.golang.org/owners |
Second this. "c" is interpreted as a json field with name "-" and an extra tag "omitempty"
In addition, using |
I agree with @timothy-king, https://pkg.go.dev/encoding/json@go1.17.8 // Field appears in JSON as key "-".
Field int `json:"-,"`` Is it ok to close this issue? |
Case split in my mind:
Line between "better" and "new" is fuzzy. But I would put warn on "-" on any exported field as "new" while having a special case for when a duplicate json tag is named "-" would be "better". Such as Open to all three choices though. |
Looks like the consensus is working as intended |
go vet -structtag
skips private fields with JSON tag-
, but doesn't skip those with tag-,omitempty
. This is inconsistent.What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes, also with
go1.18beta2
.What did you do?
What did you see?
What did you expect to see?
No error.
If
a
andb
are not reported, neither shouldc
ord
.Or maybe the error should be about the presence of
omitempty
.The text was updated successfully, but these errors were encountered: