-
Notifications
You must be signed in to change notification settings - Fork 19.1k
cmd/vet: report printf calls with non-const format and no args #60529
Copy link
Copy link
Closed
Labels
AnalysisIssues related to static analysis (vet, x/tools/go/analysis)Issues related to static analysis (vet, x/tools/go/analysis)FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.The path to resolution is known, but the work has not been done.ProposalProposal-Acceptedgopls/analysisIssues related to running analysis in goplsIssues related to running analysis in goplshelp wantedrelease-blocker
Milestone
Metadata
Metadata
Assignees
Labels
AnalysisIssues related to static analysis (vet, x/tools/go/analysis)Issues related to static analysis (vet, x/tools/go/analysis)FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.The path to resolution is known, but the work has not been done.ProposalProposal-Acceptedgopls/analysisIssues related to running analysis in goplsIssues related to running analysis in goplshelp wantedrelease-blocker
Type
Fields
Give feedbackNo fields configured for issues without a type.
A common mistake not caught by the existing printf vet checker is
Printf(v), where v is a variable, when the user intendedPrintf("%s", v). If the value of v contains an unexpected percent sign, the program has a bug.I feel like I see this mistake in Google Go readability reviews at least once a week, and the Google corpus shows up hundreds of violations with what looks like close to 100% precision:
https://source.corp.google.com/search?q=%5B.%5DPrintf%5C(%5Ba-z%5D*%5C)%20lang:go&sq=
(Apologies, link to Google internal service.)
Printf and similar functions are usually called with a literal format string, followed by zero or more arguments. Occasionally the format string argument is a variable, either because preceding logic chooses between two alternative formats, or because the call appears in a wrapper function that takes both the format and its arguments as parameters, but in both those cases the format is followed by other arguments. It's hard to imagine any good reason one would call printf with a variable format string and no arguments.
We should make the printf checker report this error.
@timothy-king @findleyr