Skip to content

cmd/vet: report printf calls with non-const format and no args #60529

Closed
@adonovan

Description

@adonovan

A common mistake not caught by the existing printf vet checker is Printf(v), where v is a variable, when the user intended Printf("%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

Metadata

Metadata

Assignees

Labels

AnalysisIssues related to static analysis (vet, x/tools/go/analysis)NeedsFixThe path to resolution is known, but the work has not been done.ProposalProposal-Acceptedgopls/analysisIssues related to running analysis in goplshelp wantedrelease-blocker

Type

No type

Projects

Status

Accepted

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions