From 6ad76718cfdd59977b0008c1e774150a7e39fbd8 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Thu, 7 Jul 2016 17:40:37 -0700 Subject: [PATCH] cmd/vet: don't treat trailing % as possible formatting directive Eliminates the following false positive: cmd/go/go_test.go:1916: possible formatting directive in Error call The line in question: tg.t.Error("some coverage results are 0.0%") Updates #11041 Change-Id: I3b7611fa3e0245714a19bd5388f21e39944f5296 Reviewed-on: https://go-review.googlesource.com/27128 Run-TryBot: Josh Bleecher Snyder TryBot-Result: Gobot Gobot Reviewed-by: Rob Pike --- src/cmd/vet/print.go | 5 ++++- src/cmd/vet/testdata/print.go | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/cmd/vet/print.go b/src/cmd/vet/print.go index f4b985cfbd..e46897115a 100644 --- a/src/cmd/vet/print.go +++ b/src/cmd/vet/print.go @@ -626,7 +626,10 @@ func (f *File) checkPrint(call *ast.CallExpr, name string) { } arg := args[0] if lit, ok := arg.(*ast.BasicLit); ok && lit.Kind == token.STRING { - if strings.Contains(lit.Value, "%") { + // Ignore trailing % character in lit.Value. + // The % in "abc 0.0%" couldn't be a formatting directive. + s := strings.TrimSuffix(lit.Value, `%"`) + if strings.Contains(s, "%") { f.Badf(call.Pos(), "possible formatting directive in %s call", name) } } diff --git a/src/cmd/vet/testdata/print.go b/src/cmd/vet/testdata/print.go index 6805b0ec60..bda6b77aab 100644 --- a/src/cmd/vet/testdata/print.go +++ b/src/cmd/vet/testdata/print.go @@ -133,6 +133,7 @@ func PrintfTests() { fmt.Printf("%.*s %d %g", 3, "hi", 23, 'x') // ERROR "arg 'x' for printf verb %g of wrong type" fmt.Println() // not an error fmt.Println("%s", "hi") // ERROR "possible formatting directive in Println call" + fmt.Println("0.0%") // correct (trailing % couldn't be a formatting directive) fmt.Printf("%s", "hi", 3) // ERROR "wrong number of args for format in Printf call" _ = fmt.Sprintf("%"+("s"), "hi", 3) // ERROR "wrong number of args for format in Sprintf call" fmt.Printf("%s%%%d", "hi", 3) // correct