diff options
| author | Russ Cox <rsc@golang.org> | 2018-06-28 00:05:46 -0400 |
|---|---|---|
| committer | Ian Lance Taylor <iant@golang.org> | 2018-06-28 22:17:10 +0000 |
| commit | 0d52c144a2564fbd5b755cd06759ab7d6870c85b (patch) | |
| tree | a9fbe91946327e0feef2bdc7ba560a87218d98d7 /src | |
| parent | 578b961727e65787160af389272517098b37b4ef (diff) | |
| download | go-0d52c144a2564fbd5b755cd06759ab7d6870c85b.tar.xz | |
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 <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
Diffstat (limited to 'src')
| -rw-r--r-- | src/cmd/vet/buildtag.go | 23 | ||||
| -rw-r--r-- | src/cmd/vet/testdata/buildtag/buildtag.go | 2 |
2 files changed, 12 insertions, 13 deletions
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 |
