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: flag likely incorrect %T usages on reflect.Type #41590

Open
dsnet opened this issue Sep 23, 2020 · 1 comment
Open

cmd/vet: flag likely incorrect %T usages on reflect.Type #41590

dsnet opened this issue Sep 23, 2020 · 1 comment

Comments

@dsnet
Copy link
Member

@dsnet dsnet commented Sep 23, 2020

In code that uses Go reflection, it is common that the user prints the type. However, it easy for people (out of habit) to use %T with a reflect.Type when they should actually be using %v instead.

For example:

t := reflect.TypeOf(...)
if checkValid(t) {
    panic(fmt.Sprintf("invalid type: %T: t)) // incorrect: should be using %v
}

It is unlikely that the wrong verb is detected during code review.

In the instances similar to this, I don't think I saw a single case where a person actually wanted to print the type of reflect.Type itself (which by the way is *reflect.rtype and not particularly interesting for users).

\cc @dominikh in case this is a check better suited for staticcheck than vet.

@mvdan
Copy link
Member

@mvdan mvdan commented Sep 23, 2020

I think this would be fine for precision and correctness. I'm less sure about frequency; how often does this bug come up in practice? I think I've seen this bug maybe once or twice before, which makes me think it would be significantly less frequent than a lot of the other vet checks, but perhaps my experience is biased.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.