Skip to content

Commit 2563079

Browse files
committed
go/analysis/passes/printf: update directive diagnostic message
Updates the Diagnostic message for a Printf directive being found in a non-Printf function. New message suggests using Printf. Example buggy code: fmt.Println("%s", "hi") Old diagnostic message: fmt.Println call has possible formatting directive %s New diagnostic message: fmt.Println call contains %s, possibly intended as Printf formatting directive Fixes golang/go#47872 Change-Id: Ia2edb7c017b3f4417809e49c0acb0d677c8ec016 Reviewed-on: https://go-review.googlesource.com/c/tools/+/491075 Reviewed-by: Zvonimir Pavlinovic <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Tim King <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent 3f6c3b3 commit 2563079

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

go/analysis/passes/printf/printf.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ func checkPrint(pass *analysis.Pass, call *ast.CallExpr, fn *types.Func) {
10561056
if strings.Contains(s, "%") {
10571057
m := printFormatRE.FindStringSubmatch(s)
10581058
if m != nil {
1059-
pass.ReportRangef(call, "%s call has possible formatting directive %s", fn.FullName(), m[0])
1059+
pass.ReportRangef(call, "%s call contains %s, possibly intended as Printf formatting directive", fn.FullName(), m[0])
10601060
}
10611061
}
10621062
}

go/analysis/passes/printf/testdata/src/a/a.go

+24-24
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,10 @@ func PrintfTests() {
150150
fmt.Printf("%s", nonemptyinterface) // correct (the type is responsible for formatting)
151151
fmt.Printf("%.*s %d %6g", 3, "hi", 23, 'x') // want "fmt.Printf format %6g has arg 'x' of wrong type rune"
152152
fmt.Println() // not an error
153-
fmt.Println("%s", "hi") // want "fmt.Println call has possible formatting directive %s"
154-
fmt.Println("%v", "hi") // want "fmt.Println call has possible formatting directive %v"
155-
fmt.Println("%T", "hi") // want "fmt.Println call has possible formatting directive %T"
156-
fmt.Println("%s"+" there", "hi") // want "fmt.Println call has possible formatting directive %s"
153+
fmt.Println("%s", "hi") // want "fmt.Println call contains %s, possibly intended as Printf formatting directive"
154+
fmt.Println("%v", "hi") // want "fmt.Println call contains %v, possibly intended as Printf formatting directive"
155+
fmt.Println("%T", "hi") // want "fmt.Println call contains %T, possibly intended as Printf formatting directive"
156+
fmt.Println("%s"+" there", "hi") // want "fmt.Println call contains %s, possibly intended as Printf formatting directive"
157157
fmt.Println("0.0%") // correct (trailing % couldn't be a formatting directive)
158158
fmt.Printf("%s", "hi", 3) // want "fmt.Printf call needs 1 arg but has 2 args"
159159
_ = fmt.Sprintf("%"+("s"), "hi", 3) // want "fmt.Sprintf call needs 1 arg but has 2 args"
@@ -177,19 +177,19 @@ func PrintfTests() {
177177
Printf(format, "hi") // want "a.Printf format %s reads arg #2, but call has 1 arg$"
178178
Printf("%s %d %.3v %q", "str", 4) // want "a.Printf format %.3v reads arg #3, but call has 2 args"
179179
f := new(ptrStringer)
180-
f.Warn(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Warn call has possible formatting directive %s`
180+
f.Warn(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Warn call contains %s, possibly intended as Printf formatting directive`
181181
f.Warnf(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Warnf call needs 1 arg but has 2 args`
182182
f.Warnf(0, "%r", "hello") // want `\(\*a.ptrStringer\).Warnf format %r has unknown verb r`
183183
f.Warnf(0, "%#s", "hello") // want `\(\*a.ptrStringer\).Warnf format %#s has unrecognized flag #`
184-
f.Warn2(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Warn2 call has possible formatting directive %s`
184+
f.Warn2(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Warn2 call contains %s, possibly intended as Printf formatting directive`
185185
f.Warnf2(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Warnf2 call needs 1 arg but has 2 args`
186186
f.Warnf2(0, "%r", "hello") // want `\(\*a.ptrStringer\).Warnf2 format %r has unknown verb r`
187187
f.Warnf2(0, "%#s", "hello") // want `\(\*a.ptrStringer\).Warnf2 format %#s has unrecognized flag #`
188-
f.Wrap(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Wrap call has possible formatting directive %s`
188+
f.Wrap(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Wrap call contains %s, possibly intended as Printf formatting directive`
189189
f.Wrapf(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Wrapf call needs 1 arg but has 2 args`
190190
f.Wrapf(0, "%r", "hello") // want `\(\*a.ptrStringer\).Wrapf format %r has unknown verb r`
191191
f.Wrapf(0, "%#s", "hello") // want `\(\*a.ptrStringer\).Wrapf format %#s has unrecognized flag #`
192-
f.Wrap2(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Wrap2 call has possible formatting directive %s`
192+
f.Wrap2(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Wrap2 call contains %s, possibly intended as Printf formatting directive`
193193
f.Wrapf2(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Wrapf2 call needs 1 arg but has 2 args`
194194
f.Wrapf2(0, "%r", "hello") // want `\(\*a.ptrStringer\).Wrapf2 format %r has unknown verb r`
195195
f.Wrapf2(0, "%#s", "hello") // want `\(\*a.ptrStringer\).Wrapf2 format %#s has unrecognized flag #`
@@ -226,7 +226,7 @@ func PrintfTests() {
226226
var et1 *testing.T
227227
et1.Error() // ok
228228
et1.Error("hi") // ok
229-
et1.Error("%d", 3) // want `\(\*testing.common\).Error call has possible formatting directive %d`
229+
et1.Error("%d", 3) // want `\(\*testing.common\).Error call contains %d, possibly intended as Printf formatting directive`
230230
et1.Errorf("%s", 1) // want `\(\*testing.common\).Errorf format %s has arg 1 of wrong type int`
231231
var et3 errorTest3
232232
et3.Error() // ok, not an error method.
@@ -253,7 +253,7 @@ func PrintfTests() {
253253
// Special handling for Log.
254254
math.Log(3) // OK
255255
var t *testing.T
256-
t.Log("%d", 3) // want `\(\*testing.common\).Log call has possible formatting directive %d`
256+
t.Log("%d", 3) // want `\(\*testing.common\).Log call contains %d, possibly intended as Printf formatting directive`
257257
t.Logf("%d", 3)
258258
t.Logf("%d", "hi") // want `\(\*testing.common\).Logf format %d has arg "hi" of wrong type string`
259259

@@ -307,27 +307,27 @@ func PrintfTests() {
307307
Printf(someString(), "hello") // OK
308308

309309
// Printf wrappers in package log should be detected automatically
310-
logpkg.Fatal("%d", 1) // want "log.Fatal call has possible formatting directive %d"
310+
logpkg.Fatal("%d", 1) // want "log.Fatal call contains %d, possibly intended as Printf formatting directive"
311311
logpkg.Fatalf("%d", "x") // want `log.Fatalf format %d has arg "x" of wrong type string`
312-
logpkg.Fatalln("%d", 1) // want "log.Fatalln call has possible formatting directive %d"
313-
logpkg.Panic("%d", 1) // want "log.Panic call has possible formatting directive %d"
312+
logpkg.Fatalln("%d", 1) // want "log.Fatalln call contains %d, possibly intended as Printf formatting directive"
313+
logpkg.Panic("%d", 1) // want "log.Panic call contains %d, possibly intended as Printf formatting directive"
314314
logpkg.Panicf("%d", "x") // want `log.Panicf format %d has arg "x" of wrong type string`
315-
logpkg.Panicln("%d", 1) // want "log.Panicln call has possible formatting directive %d"
316-
logpkg.Print("%d", 1) // want "log.Print call has possible formatting directive %d"
315+
logpkg.Panicln("%d", 1) // want "log.Panicln call contains %d, possibly intended as Printf formatting directive"
316+
logpkg.Print("%d", 1) // want "log.Print call contains %d, possibly intended as Printf formatting directive"
317317
logpkg.Printf("%d", "x") // want `log.Printf format %d has arg "x" of wrong type string`
318-
logpkg.Println("%d", 1) // want "log.Println call has possible formatting directive %d"
318+
logpkg.Println("%d", 1) // want "log.Println call contains %d, possibly intended as Printf formatting directive"
319319

320320
// Methods too.
321321
var l *logpkg.Logger
322-
l.Fatal("%d", 1) // want `\(\*log.Logger\).Fatal call has possible formatting directive %d`
322+
l.Fatal("%d", 1) // want `\(\*log.Logger\).Fatal call contains %d, possibly intended as Printf formatting directive`
323323
l.Fatalf("%d", "x") // want `\(\*log.Logger\).Fatalf format %d has arg "x" of wrong type string`
324-
l.Fatalln("%d", 1) // want `\(\*log.Logger\).Fatalln call has possible formatting directive %d`
325-
l.Panic("%d", 1) // want `\(\*log.Logger\).Panic call has possible formatting directive %d`
324+
l.Fatalln("%d", 1) // want `\(\*log.Logger\).Fatalln call contains %d, possibly intended as Printf formatting directive`
325+
l.Panic("%d", 1) // want `\(\*log.Logger\).Panic call contains %d, possibly intended as Printf formatting directive`
326326
l.Panicf("%d", "x") // want `\(\*log.Logger\).Panicf format %d has arg "x" of wrong type string`
327-
l.Panicln("%d", 1) // want `\(\*log.Logger\).Panicln call has possible formatting directive %d`
328-
l.Print("%d", 1) // want `\(\*log.Logger\).Print call has possible formatting directive %d`
327+
l.Panicln("%d", 1) // want `\(\*log.Logger\).Panicln call contains %d, possibly intended as Printf formatting directive`
328+
l.Print("%d", 1) // want `\(\*log.Logger\).Print call contains %d, possibly intended as Printf formatting directive`
329329
l.Printf("%d", "x") // want `\(\*log.Logger\).Printf format %d has arg "x" of wrong type string`
330-
l.Println("%d", 1) // want `\(\*log.Logger\).Println call has possible formatting directive %d`
330+
l.Println("%d", 1) // want `\(\*log.Logger\).Println call contains %d, possibly intended as Printf formatting directive`
331331

332332
// Issue 26486
333333
dbg("", 1) // no error "call has arguments but no formatting directive"
@@ -361,7 +361,7 @@ func PrintfTests() {
361361
eis.Errorf(0, "%w", err) // OK
362362
ess.Errorf("ERROR", "%w", err) // OK
363363
fmt.Appendf(nil, "%d", "123") // want `wrong type`
364-
fmt.Append(nil, "%d", 123) // want `possible formatting directive`
364+
fmt.Append(nil, "%d", 123) // want `possibly intended as Printf formatting directive`
365365

366366
}
367367

@@ -839,7 +839,7 @@ func PointersToCompoundTypes() {
839839
// Printf wrappers from external package
840840
func externalPackage() {
841841
b.Wrapf("%s", 1) // want "Wrapf format %s has arg 1 of wrong type int"
842-
b.Wrap("%s", 1) // want "Wrap call has possible formatting directive %s"
842+
b.Wrap("%s", 1) // want "Wrap call contains %s, possibly intended as Printf formatting directive"
843843
b.NoWrap("%s", 1)
844844
b.Wrapf2("%s", 1) // want "Wrapf2 format %s has arg 1 of wrong type int"
845845
}

0 commit comments

Comments
 (0)