From: Russ Cox Date: Thu, 28 Jun 2018 04:05:46 +0000 (-0400) Subject: cmd/vet: fix ironic misuse of fmt.Sprintf X-Git-Tag: go1.11beta2~260 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=0d52c144a2564fbd5b755cd06759ab7d6870c85b;p=gostls13.git cmd/vet: fix ironic misuse of fmt.Sprintf Move badf helper into top-level function so that prints from buildtag.go are once again themselves printf-format-checked by vet. Also, fix implementation, which was missing a ... in the Sprintf call and produced messages like: /Users/rsc/x_test.go:1: +build comment must appear before package clause and be followed by a blank line%!(EXTRA []interface {}=[]) These were introduced in CL 111415. Change-Id: I000af3a4e01dc99fc79c9146aa68a71dace1460f Reviewed-on: https://go-review.googlesource.com/121300 Run-TryBot: Russ Cox TryBot-Result: Gobot Gobot Reviewed-by: Rob Pike --- diff --git a/src/cmd/vet/buildtag.go b/src/cmd/vet/buildtag.go index d1fedec554..ba3a361b91 100644 --- a/src/cmd/vet/buildtag.go +++ b/src/cmd/vet/buildtag.go @@ -18,18 +18,17 @@ var ( plusBuild = []byte("+build") ) +func badfLine(f *File, line int, format string, args ...interface{}) { + msg := fmt.Sprintf(format, args...) + fmt.Fprintf(os.Stderr, "%s:%d: %s\n", f.name, line, msg) + setExit(1) +} + // checkBuildTag checks that build tags are in the correct location and well-formed. func checkBuildTag(f *File) { if !vet("buildtags") { return } - // badf is like File.Badf, but it uses a line number instead of - // token.Pos. - badf := func(line int, format string, args ...interface{}) { - msg := fmt.Sprintf(format, args) - fmt.Fprintf(os.Stderr, "%s:%d: %s\n", f.name, line, msg) - setExit(1) - } // we must look at the raw lines, as build tags may appear in non-Go // files such as assembly files. @@ -92,11 +91,11 @@ func checkBuildTag(f *File) { fields := bytes.Fields(text) if !bytes.Equal(fields[0], plusBuild) { // Comment is something like +buildasdf not +build. - badf(i+1, "possible malformed +build comment") + badfLine(f, i+1, "possible malformed +build comment") continue } if i >= cutoff { - badf(i+1, "+build comment must appear before package clause and be followed by a blank line") + badfLine(f, i+1, "+build comment must appear before package clause and be followed by a blank line") continue } // Check arguments. @@ -104,13 +103,13 @@ func checkBuildTag(f *File) { for _, arg := range fields[1:] { for _, elem := range strings.Split(string(arg), ",") { if strings.HasPrefix(elem, "!!") { - badf(i+1, "invalid double negative in build constraint: %s", arg) + badfLine(f, i+1, "invalid double negative in build constraint: %s", arg) break Args } elem = strings.TrimPrefix(elem, "!") for _, c := range elem { if !unicode.IsLetter(c) && !unicode.IsDigit(c) && c != '_' && c != '.' { - badf(i+1, "invalid non-alphanumeric build constraint: %s", arg) + badfLine(f, i+1, "invalid non-alphanumeric build constraint: %s", arg) break Args } } @@ -120,7 +119,7 @@ func checkBuildTag(f *File) { } // Comment with +build but not at beginning. if i < cutoff { - badf(i+1, "possible malformed +build comment") + badfLine(f, i+1, "possible malformed +build comment") continue } } diff --git a/src/cmd/vet/testdata/buildtag/buildtag.go b/src/cmd/vet/testdata/buildtag/buildtag.go index 6ee08da638..c2fd6aaaf2 100644 --- a/src/cmd/vet/testdata/buildtag/buildtag.go +++ b/src/cmd/vet/testdata/buildtag/buildtag.go @@ -9,7 +9,7 @@ package testdata -// +build toolate // ERROR "build comment must appear before package clause and be followed by a blank line" +// +build toolate // ERROR "build comment must appear before package clause and be followed by a blank line$" var _ = 3