]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/vet: fix ironic misuse of fmt.Sprintf
authorRuss Cox <rsc@golang.org>
Thu, 28 Jun 2018 04:05:46 +0000 (00:05 -0400)
committerIan Lance Taylor <iant@golang.org>
Thu, 28 Jun 2018 22:17:10 +0000 (22:17 +0000)
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>
src/cmd/vet/buildtag.go
src/cmd/vet/testdata/buildtag/buildtag.go

index d1fedec554918c19628e6185030e41bcdb32b6a3..ba3a361b9113bb01d547d8614da0586afb865b1f 100644 (file)
@@ -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
                }
        }
index 6ee08da638da8db1a4bc895e8bcb9978e84e51f1..c2fd6aaaf2fa01deb087090a78756eabf2e365bb 100644 (file)
@@ -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