golang/go

cmd/vet: make printf checking more precise when arguments are changed

Open

#26,555 opened on Jul 23, 2018

View on GitHub
 (3 comments) (0 reactions) (0 assignees)Go (19,008 forks)batch import
AnalysisNeedsInvestigationhelp wanted

Repository metrics

Stars
 (133,883 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

For #26486 https://golang.org/cl/125039 changed the printf checking to only warn about functions that do not modify the arguments before passing them to fmt.Printf (or whatever). The example in that issue, drawn from real code, is:

func dbg(s string, va ...interface{}) {
	if s == "" {
		s = strings.Repeat("%v ", len(va))
	}
	_, fn, fl, _ := runtime.Caller(1)
	fmt.Printf("dbg %s:%d: ", path.Base(fn), fl)
	fmt.Printf(s, va...)
	fmt.Println()
}

This can be called as dbg("", err).

The fix in CL 125039 means that we no longer issue printf warnings for functions like this:

func Prefix(s string, args ...interface{}) {
    s = "error: " + s
    fmt.Printf(s, args)
}

We should figure out a way to continue issuing warnings for Prefix without issuing them for dbg.

Contributor guide