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: No error for unkeyed use of composite literals where type is omitted in slice element #23539
Comments
Can you show us a standalone example that hasn't been changed? Just to clarify exactly what is happening. Thanks. |
The link above is still valid: https://github.com/grpc/grpc-go/blob/5ba054bf3709c0a51d1ed76f68d274a8c1eef459/examples/route_guide/client/client.go#L103-L110 notes := []*pb.RouteNote{
{&pb.Point{Latitude: 0, Longitude: 1}, "First message"},
{&pb.Point{Latitude: 0, Longitude: 2}, "Second message"},
{&pb.Point{Latitude: 0, Longitude: 3}, "Third message"},
{&pb.Point{Latitude: 0, Longitude: 1}, "Fourth message"},
{&pb.Point{Latitude: 0, Longitude: 2}, "Fifth message"},
{&pb.Point{Latitude: 0, Longitude: 3}, "Sixth message"},
} Note the slice elements (type |
The fix to properly use field names can be seen here: https://github.com/grpc/grpc-go/pull/1829/files And adding the type to the slice elements allows vet to detect the error, i.e.:
|
What is the problem here? It looks all the following literal forms are legal.
|
@dotaheor this is not about valid and invalid programs - this is about vet, a static analysis tool. What it's warning about here is when one initialises struct values of types in other packages, and sets their fields without using keys. This is very error-prone, as struct types tend to get new fields added, and one has no control over that if it's in another package. The problem here is the pointer - I found a fix, CL incoming. |
Slight correction - vet does support struct pointer literals, but it was getting confused in this case where they are elements with omitted types. |
Change https://golang.org/cl/89715 mentions this issue: |
The current version of go vet does not flag these as noted in golang/go#23539. A fix is in the works, and will likely be included in Go 1.11. Make the updated vet tool happy, so that the vendored copy of pprof in the Go tree can be updated and make the vet trybot happy. Fixes #297.
The current version of go vet does not flag these as noted in golang/go#23539. A fix is in the works, and will likely be included in Go 1.11. Make the updated vet tool happy, so that the vendored copy of pprof in the Go tree can be updated and make the vet trybot happy. Fixes google#297.
What version of Go are you using (
go version
)?go version devel +165e7523fb linux/amd64; same with all earlier versions tested (1.8, 1.9 at least)
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?(can provide if needed)
What did you do?
See illegal use of composite literals here:
https://github.com/grpc/grpc-go/blob/5ba054bf3709c0a51d1ed76f68d274a8c1eef459/examples/route_guide/client/client.go#L103-L110
What did you expect to see?
An error
What did you see instead?
No error
Changing the lines to specify
&pb.RouteNote{}
results in an error as expected:EDIT: grpc-Go has since been updated to stop using unkeyed fields here with grpc/grpc-go#1829.
The text was updated successfully, but these errors were encountered: