Skip to content

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

Description

@dsnet

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    AnalysisIssues related to static analysis (vet, x/tools/go/analysis)FeatureRequestIssues asking for a new feature that does not need a proposal.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions