Commit 0d52c144 authored by Russ Cox's avatar Russ Cox Committed by Ian Lance Taylor

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: default avatarRob Pike <r@golang.org>
parent 578b9617
...@@ -18,18 +18,17 @@ var ( ...@@ -18,18 +18,17 @@ var (
plusBuild = []byte("+build") 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. // checkBuildTag checks that build tags are in the correct location and well-formed.
func checkBuildTag(f *File) { func checkBuildTag(f *File) {
if !vet("buildtags") { if !vet("buildtags") {
return 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 // we must look at the raw lines, as build tags may appear in non-Go
// files such as assembly files. // files such as assembly files.
...@@ -92,11 +91,11 @@ func checkBuildTag(f *File) { ...@@ -92,11 +91,11 @@ func checkBuildTag(f *File) {
fields := bytes.Fields(text) fields := bytes.Fields(text)
if !bytes.Equal(fields[0], plusBuild) { if !bytes.Equal(fields[0], plusBuild) {
// Comment is something like +buildasdf not +build. // Comment is something like +buildasdf not +build.
badf(i+1, "possible malformed +build comment") badfLine(f, i+1, "possible malformed +build comment")
continue continue
} }
if i >= cutoff { 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 continue
} }
// Check arguments. // Check arguments.
...@@ -104,13 +103,13 @@ func checkBuildTag(f *File) { ...@@ -104,13 +103,13 @@ func checkBuildTag(f *File) {
for _, arg := range fields[1:] { for _, arg := range fields[1:] {
for _, elem := range strings.Split(string(arg), ",") { for _, elem := range strings.Split(string(arg), ",") {
if strings.HasPrefix(elem, "!!") { 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 break Args
} }
elem = strings.TrimPrefix(elem, "!") elem = strings.TrimPrefix(elem, "!")
for _, c := range elem { for _, c := range elem {
if !unicode.IsLetter(c) && !unicode.IsDigit(c) && c != '_' && c != '.' { 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 break Args
} }
} }
...@@ -120,7 +119,7 @@ func checkBuildTag(f *File) { ...@@ -120,7 +119,7 @@ func checkBuildTag(f *File) {
} }
// Comment with +build but not at beginning. // Comment with +build but not at beginning.
if i < cutoff { if i < cutoff {
badf(i+1, "possible malformed +build comment") badfLine(f, i+1, "possible malformed +build comment")
continue continue
} }
} }
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
package testdata 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 var _ = 3
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment