1// Copyright 2010 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// This file contains tests for the printf checker.
6
7package print
8
9import (
10	"fmt"
11	logpkg "log" // renamed to make it harder to see
12	"math"
13	"os"
14	"testing"
15	"unsafe" // just for test case printing unsafe.Pointer
16	// For testing printf-like functions from external package.
17	// "github.com/foobar/externalprintf"
18)
19
20func UnsafePointerPrintfTest() {
21	var up unsafe.Pointer
22	fmt.Printf("%p, %x %X", up, up, up)
23}
24
25// Error methods that do not satisfy the Error interface and should be checked.
26type errorTest1 int
27
28func (errorTest1) Error(...interface{}) string {
29	return "hi"
30}
31
32type errorTest2 int // Analogous to testing's *T type.
33func (errorTest2) Error(...interface{}) {
34}
35
36type errorTest3 int
37
38func (errorTest3) Error() { // No return value.
39}
40
41type errorTest4 int
42
43func (errorTest4) Error() int { // Different return type.
44	return 3
45}
46
47type errorTest5 int
48
49func (errorTest5) error() { // niladic; don't complain if no args (was bug)
50}
51
52// This function never executes, but it serves as a simple test for the program.
53// Test with make test.
54func PrintfTests() {
55	var b bool
56	var i int
57	var r rune
58	var s string
59	var x float64
60	var p *int
61	var imap map[int]int
62	var fslice []float64
63	var c complex64
64	// Some good format/argtypes
65	fmt.Printf("")
66	fmt.Printf("%b %b %b", 3, i, x)
67	fmt.Printf("%c %c %c %c", 3, i, 'x', r)
68	fmt.Printf("%d %d %d", 3, i, imap)
69	fmt.Printf("%e %e %e %e", 3e9, x, fslice, c)
70	fmt.Printf("%E %E %E %E", 3e9, x, fslice, c)
71	fmt.Printf("%f %f %f %f", 3e9, x, fslice, c)
72	fmt.Printf("%F %F %F %F", 3e9, x, fslice, c)
73	fmt.Printf("%g %g %g %g", 3e9, x, fslice, c)
74	fmt.Printf("%G %G %G %G", 3e9, x, fslice, c)
75	fmt.Printf("%b %b %b %b", 3e9, x, fslice, c)
76	fmt.Printf("%o %o", 3, i)
77	fmt.Printf("%p", p)
78	fmt.Printf("%q %q %q %q", 3, i, 'x', r)
79	fmt.Printf("%s %s %s", "hi", s, []byte{65})
80	fmt.Printf("%t %t", true, b)
81	fmt.Printf("%T %T", 3, i)
82	fmt.Printf("%U %U", 3, i)
83	fmt.Printf("%v %v", 3, i)
84	fmt.Printf("%x %x %x %x %x %x %x", 3, i, "hi", s, x, c, fslice)
85	fmt.Printf("%X %X %X %X %X %X %X", 3, i, "hi", s, x, c, fslice)
86	fmt.Printf("%.*s %d %g", 3, "hi", 23, 2.3)
87	fmt.Printf("%s", &stringerv)
88	fmt.Printf("%v", &stringerv)
89	fmt.Printf("%T", &stringerv)
90	fmt.Printf("%s", &embeddedStringerv)
91	fmt.Printf("%v", &embeddedStringerv)
92	fmt.Printf("%T", &embeddedStringerv)
93	fmt.Printf("%v", notstringerv)
94	fmt.Printf("%T", notstringerv)
95	fmt.Printf("%q", stringerarrayv)
96	fmt.Printf("%v", stringerarrayv)
97	fmt.Printf("%s", stringerarrayv)
98	fmt.Printf("%v", notstringerarrayv)
99	fmt.Printf("%T", notstringerarrayv)
100	fmt.Printf("%d", new(fmt.Formatter))
101	fmt.Printf("%*%", 2)               // Ridiculous but allowed.
102	fmt.Printf("%s", interface{}(nil)) // Nothing useful we can say.
103
104	fmt.Printf("%g", 1+2i)
105	fmt.Printf("%#e %#E %#f %#F %#g %#G", 1.2, 1.2, 1.2, 1.2, 1.2, 1.2) // OK since Go 1.9
106	// Some bad format/argTypes
107	fmt.Printf("%b", "hi")                      // ERROR "Printf format %b has arg \x22hi\x22 of wrong type string"
108	fmt.Printf("%t", c)                         // ERROR "Printf format %t has arg c of wrong type complex64"
109	fmt.Printf("%t", 1+2i)                      // ERROR "Printf format %t has arg 1 \+ 2i of wrong type complex128"
110	fmt.Printf("%c", 2.3)                       // ERROR "Printf format %c has arg 2.3 of wrong type float64"
111	fmt.Printf("%d", 2.3)                       // ERROR "Printf format %d has arg 2.3 of wrong type float64"
112	fmt.Printf("%e", "hi")                      // ERROR "Printf format %e has arg \x22hi\x22 of wrong type string"
113	fmt.Printf("%E", true)                      // ERROR "Printf format %E has arg true of wrong type bool"
114	fmt.Printf("%f", "hi")                      // ERROR "Printf format %f has arg \x22hi\x22 of wrong type string"
115	fmt.Printf("%F", 'x')                       // ERROR "Printf format %F has arg 'x' of wrong type rune"
116	fmt.Printf("%g", "hi")                      // ERROR "Printf format %g has arg \x22hi\x22 of wrong type string"
117	fmt.Printf("%g", imap)                      // ERROR "Printf format %g has arg imap of wrong type map\[int\]int"
118	fmt.Printf("%G", i)                         // ERROR "Printf format %G has arg i of wrong type int"
119	fmt.Printf("%o", x)                         // ERROR "Printf format %o has arg x of wrong type float64"
120	fmt.Printf("%p", nil)                       // ERROR "Printf format %p has arg nil of wrong type untyped nil"
121	fmt.Printf("%p", 23)                        // ERROR "Printf format %p has arg 23 of wrong type int"
122	fmt.Printf("%q", x)                         // ERROR "Printf format %q has arg x of wrong type float64"
123	fmt.Printf("%s", b)                         // ERROR "Printf format %s has arg b of wrong type bool"
124	fmt.Printf("%s", byte(65))                  // ERROR "Printf format %s has arg byte\(65\) of wrong type byte"
125	fmt.Printf("%t", 23)                        // ERROR "Printf format %t has arg 23 of wrong type int"
126	fmt.Printf("%U", x)                         // ERROR "Printf format %U has arg x of wrong type float64"
127	fmt.Printf("%x", nil)                       // ERROR "Printf format %x has arg nil of wrong type untyped nil"
128	fmt.Printf("%s", stringerv)                 // ERROR "Printf format %s has arg stringerv of wrong type .*print.ptrStringer"
129	fmt.Printf("%t", stringerv)                 // ERROR "Printf format %t has arg stringerv of wrong type .*print.ptrStringer"
130	fmt.Printf("%s", embeddedStringerv)         // ERROR "Printf format %s has arg embeddedStringerv of wrong type .*print.embeddedStringer"
131	fmt.Printf("%t", embeddedStringerv)         // ERROR "Printf format %t has arg embeddedStringerv of wrong type .*print.embeddedStringer"
132	fmt.Printf("%q", notstringerv)              // ERROR "Printf format %q has arg notstringerv of wrong type .*print.notstringer"
133	fmt.Printf("%t", notstringerv)              // ERROR "Printf format %t has arg notstringerv of wrong type .*print.notstringer"
134	fmt.Printf("%t", stringerarrayv)            // ERROR "Printf format %t has arg stringerarrayv of wrong type .*print.stringerarray"
135	fmt.Printf("%t", notstringerarrayv)         // ERROR "Printf format %t has arg notstringerarrayv of wrong type .*print.notstringerarray"
136	fmt.Printf("%q", notstringerarrayv)         // ERROR "Printf format %q has arg notstringerarrayv of wrong type .*print.notstringerarray"
137	fmt.Printf("%d", BoolFormatter(true))       // ERROR "Printf format %d has arg BoolFormatter\(true\) of wrong type .*print.BoolFormatter"
138	fmt.Printf("%z", FormatterVal(true))        // correct (the type is responsible for formatting)
139	fmt.Printf("%d", FormatterVal(true))        // correct (the type is responsible for formatting)
140	fmt.Printf("%s", nonemptyinterface)         // correct (the type is responsible for formatting)
141	fmt.Printf("%.*s %d %6g", 3, "hi", 23, 'x') // ERROR "Printf format %6g has arg 'x' of wrong type rune"
142	fmt.Println()                               // not an error
143	fmt.Println("%s", "hi")                     // ERROR "Println call has possible Printf formatting directive %s"
144	fmt.Println("%v", "hi")                     // ERROR "Println call has possible Printf formatting directive %v"
145	fmt.Println("%T", "hi")                     // ERROR "Println call has possible Printf formatting directive %T"
146	fmt.Println("0.0%")                         // correct (trailing % couldn't be a formatting directive)
147	fmt.Printf("%s", "hi", 3)                   // ERROR "Printf call needs 1 arg but has 2 args"
148	_ = fmt.Sprintf("%"+("s"), "hi", 3)         // ERROR "Sprintf call needs 1 arg but has 2 args"
149	fmt.Printf("%s%%%d", "hi", 3)               // correct
150	fmt.Printf("%08s", "woo")                   // correct
151	fmt.Printf("% 8s", "woo")                   // correct
152	fmt.Printf("%.*d", 3, 3)                    // correct
153	fmt.Printf("%.*d x", 3, 3, 3, 3)            // ERROR "Printf call needs 2 args but has 4 args"
154	fmt.Printf("%.*d x", "hi", 3)               // ERROR "Printf format %.*d uses non-int \x22hi\x22 as argument of \*"
155	fmt.Printf("%.*d x", i, 3)                  // correct
156	fmt.Printf("%.*d x", s, 3)                  // ERROR "Printf format %.\*d uses non-int s as argument of \*"
157	fmt.Printf("%*% x", 0.22)                   // ERROR "Printf format %\*% uses non-int 0.22 as argument of \*"
158	fmt.Printf("%q %q", multi()...)             // ok
159	fmt.Printf("%#q", `blah`)                   // ok
160	// printf("now is the time", "buddy")          // no error "printf call has arguments but no formatting directives"
161	Printf("now is the time", "buddy") // ERROR "Printf call has arguments but no formatting directives"
162	Printf("hi")                       // ok
163	const format = "%s %s\n"
164	Printf(format, "hi", "there")
165	Printf(format, "hi")              // ERROR "Printf format %s reads arg #2, but call has 1 arg$"
166	Printf("%s %d %.3v %q", "str", 4) // ERROR "Printf format %.3v reads arg #3, but call has 2 args"
167	f := new(ptrStringer)
168	f.Warn(0, "%s", "hello", 3)           // ERROR "Warn call has possible Printf formatting directive %s"
169	f.Warnf(0, "%s", "hello", 3)          // ERROR "Warnf call needs 1 arg but has 2 args"
170	f.Warnf(0, "%r", "hello")             // ERROR "Warnf format %r has unknown verb r"
171	f.Warnf(0, "%#s", "hello")            // ERROR "Warnf format %#s has unrecognized flag #"
172	f.Warn2(0, "%s", "hello", 3)          // ERROR "Warn2 call has possible Printf formatting directive %s"
173	f.Warnf2(0, "%s", "hello", 3)         // ERROR "Warnf2 call needs 1 arg but has 2 args"
174	f.Warnf2(0, "%r", "hello")            // ERROR "Warnf2 format %r has unknown verb r"
175	f.Warnf2(0, "%#s", "hello")           // ERROR "Warnf2 format %#s has unrecognized flag #"
176	f.Wrap(0, "%s", "hello", 3)           // ERROR "Wrap call has possible Printf formatting directive %s"
177	f.Wrapf(0, "%s", "hello", 3)          // ERROR "Wrapf call needs 1 arg but has 2 args"
178	f.Wrapf(0, "%r", "hello")             // ERROR "Wrapf format %r has unknown verb r"
179	f.Wrapf(0, "%#s", "hello")            // ERROR "Wrapf format %#s has unrecognized flag #"
180	f.Wrap2(0, "%s", "hello", 3)          // ERROR "Wrap2 call has possible Printf formatting directive %s"
181	f.Wrapf2(0, "%s", "hello", 3)         // ERROR "Wrapf2 call needs 1 arg but has 2 args"
182	f.Wrapf2(0, "%r", "hello")            // ERROR "Wrapf2 format %r has unknown verb r"
183	f.Wrapf2(0, "%#s", "hello")           // ERROR "Wrapf2 format %#s has unrecognized flag #"
184	fmt.Printf("%#s", FormatterVal(true)) // correct (the type is responsible for formatting)
185	Printf("d%", 2)                       // ERROR "Printf format % is missing verb at end of string"
186	Printf("%d", percentDV)
187	Printf("%d", &percentDV)
188	Printf("%d", notPercentDV)  // ERROR "Printf format %d has arg notPercentDV of wrong type .*print.notPercentDStruct"
189	Printf("%d", &notPercentDV) // ERROR "Printf format %d has arg &notPercentDV of wrong type \*.*print.notPercentDStruct"
190	Printf("%p", &notPercentDV) // Works regardless: we print it as a pointer.
191	Printf("%q", &percentDV)    // ERROR "Printf format %q has arg &percentDV of wrong type \*.*print.percentDStruct"
192	Printf("%s", percentSV)
193	Printf("%s", &percentSV)
194	// Good argument reorderings.
195	Printf("%[1]d", 3)
196	Printf("%[1]*d", 3, 1)
197	Printf("%[2]*[1]d", 1, 3)
198	Printf("%[2]*.[1]*[3]d", 2, 3, 4)
199	fmt.Fprintf(os.Stderr, "%[2]*.[1]*[3]d", 2, 3, 4) // Use Fprintf to make sure we count arguments correctly.
200	// Bad argument reorderings.
201	Printf("%[xd", 3)                      // ERROR "Printf format %\[xd is missing closing \]"
202	Printf("%[x]d x", 3)                   // ERROR "Printf format has invalid argument index \[x\]"
203	Printf("%[3]*s x", "hi", 2)            // ERROR "Printf format has invalid argument index \[3\]"
204	_ = fmt.Sprintf("%[3]d x", 2)          // ERROR "Sprintf format has invalid argument index \[3\]"
205	Printf("%[2]*.[1]*[3]d x", 2, "hi", 4) // ERROR "Printf format %\[2]\*\.\[1\]\*\[3\]d uses non-int \x22hi\x22 as argument of \*"
206	Printf("%[0]s x", "arg1")              // ERROR "Printf format has invalid argument index \[0\]"
207	Printf("%[0]d x", 1)                   // ERROR "Printf format has invalid argument index \[0\]"
208	// Something that satisfies the error interface.
209	var e error
210	fmt.Println(e.Error()) // ok
211	// Something that looks like an error interface but isn't, such as the (*T).Error method
212	// in the testing package.
213	var et1 *testing.T
214	et1.Error()        // ok
215	et1.Error("hi")    // ok
216	et1.Error("%d", 3) // ERROR "Error call has possible Printf formatting directive %d"
217	var et3 errorTest3
218	et3.Error() // ok, not an error method.
219	var et4 errorTest4
220	et4.Error() // ok, not an error method.
221	var et5 errorTest5
222	et5.error() // ok, not an error method.
223	// Interfaces can be used with any verb.
224	var iface interface {
225		ToTheMadness() bool // Method ToTheMadness usually returns false
226	}
227	fmt.Printf("%f", iface) // ok: fmt treats interfaces as transparent and iface may well have a float concrete type
228	// Can't print a function.
229	Printf("%d", someFunction) // ERROR "Printf format %d arg someFunction is a func value, not called"
230	Printf("%v", someFunction) // ERROR "Printf format %v arg someFunction is a func value, not called"
231	Println(someFunction)      // ERROR "Println arg someFunction is a func value, not called"
232	Printf("%p", someFunction) // ok: maybe someone wants to see the pointer
233	Printf("%T", someFunction) // ok: maybe someone wants to see the type
234	// Bug: used to recur forever.
235	Printf("%p %x", recursiveStructV, recursiveStructV.next)
236	Printf("%p %x", recursiveStruct1V, recursiveStruct1V.next) // ERROR "Printf format %x has arg recursiveStruct1V\.next of wrong type \*.*print\.RecursiveStruct2"
237	Printf("%p %x", recursiveSliceV, recursiveSliceV)
238	Printf("%p %x", recursiveMapV, recursiveMapV)
239	// Special handling for Log.
240	math.Log(3) // OK
241	var t *testing.T
242	t.Log("%d", 3) // ERROR "Log call has possible Printf formatting directive %d"
243	t.Logf("%d", 3)
244	t.Logf("%d", "hi") // ERROR "Logf format %d has arg \x22hi\x22 of wrong type string"
245
246	Errorf(1, "%d", 3)    // OK
247	Errorf(1, "%d", "hi") // ERROR "Errorf format %d has arg \x22hi\x22 of wrong type string"
248
249	// Multiple string arguments before variadic args
250	errorf("WARNING", "foobar")            // OK
251	errorf("INFO", "s=%s, n=%d", "foo", 1) // OK
252	errorf("ERROR", "%d")                  // ERROR "errorf format %d reads arg #1, but call has 0 args"
253
254	// Printf from external package
255	// externalprintf.Printf("%d", 42) // OK
256	// externalprintf.Printf("foobar") // OK
257	// level := 123
258	// externalprintf.Logf(level, "%d", 42)                        // OK
259	// externalprintf.Errorf(level, level, "foo %q bar", "foobar") // OK
260	// externalprintf.Logf(level, "%d")                            // no error "Logf format %d reads arg #1, but call has 0 args"
261	// var formatStr = "%s %s"
262	// externalprintf.Sprintf(formatStr, "a", "b")     // OK
263	// externalprintf.Logf(level, formatStr, "a", "b") // OK
264
265	// user-defined Println-like functions
266	ss := &someStruct{}
267	ss.Log(someFunction, "foo")          // OK
268	ss.Error(someFunction, someFunction) // OK
269	ss.Println()                         // OK
270	ss.Println(1.234, "foo")             // OK
271	ss.Println(1, someFunction)          // no error "Println arg someFunction is a func value, not called"
272	ss.log(someFunction)                 // OK
273	ss.log(someFunction, "bar", 1.33)    // OK
274	ss.log(someFunction, someFunction)   // no error "log arg someFunction is a func value, not called"
275
276	// indexed arguments
277	Printf("%d %[3]d %d %[2]d x", 1, 2, 3, 4)             // OK
278	Printf("%d %[0]d %d %[2]d x", 1, 2, 3, 4)             // ERROR "Printf format has invalid argument index \[0\]"
279	Printf("%d %[3]d %d %[-2]d x", 1, 2, 3, 4)            // ERROR "Printf format has invalid argument index \[-2\]"
280	Printf("%d %[3]d %d %[2234234234234]d x", 1, 2, 3, 4) // ERROR "Printf format has invalid argument index \[2234234234234\]"
281	Printf("%d %[3]d %-10d %[2]d x", 1, 2, 3)             // ERROR "Printf format %-10d reads arg #4, but call has 3 args"
282	Printf("%[1][3]d x", 1, 2)                            // ERROR "Printf format %\[1\]\[ has unknown verb \["
283	Printf("%[1]d x", 1, 2)                               // OK
284	Printf("%d %[3]d %d %[2]d x", 1, 2, 3, 4, 5)          // OK
285
286	// wrote Println but meant Fprintln
287	Printf("%p\n", os.Stdout)   // OK
288	Println(os.Stdout, "hello") // ERROR "Println does not take io.Writer but has first arg os.Stdout"
289
290	Printf(someString(), "hello") // OK
291
292	// Printf wrappers in package log should be detected automatically
293	logpkg.Fatal("%d", 1)    // ERROR "Fatal call has possible Printf formatting directive %d"
294	logpkg.Fatalf("%d", "x") // ERROR "Fatalf format %d has arg \x22x\x22 of wrong type string"
295	logpkg.Fatalln("%d", 1)  // ERROR "Fatalln call has possible Printf formatting directive %d"
296	logpkg.Panic("%d", 1)    // ERROR "Panic call has possible Printf formatting directive %d"
297	logpkg.Panicf("%d", "x") // ERROR "Panicf format %d has arg \x22x\x22 of wrong type string"
298	logpkg.Panicln("%d", 1)  // ERROR "Panicln call has possible Printf formatting directive %d"
299	logpkg.Print("%d", 1)    // ERROR "Print call has possible Printf formatting directive %d"
300	logpkg.Printf("%d", "x") // ERROR "Printf format %d has arg \x22x\x22 of wrong type string"
301	logpkg.Println("%d", 1)  // ERROR "Println call has possible Printf formatting directive %d"
302
303	// Methods too.
304	var l *logpkg.Logger
305	l.Fatal("%d", 1)    // ERROR "Fatal call has possible Printf formatting directive %d"
306	l.Fatalf("%d", "x") // ERROR "Fatalf format %d has arg \x22x\x22 of wrong type string"
307	l.Fatalln("%d", 1)  // ERROR "Fatalln call has possible Printf formatting directive %d"
308	l.Panic("%d", 1)    // ERROR "Panic call has possible Printf formatting directive %d"
309	l.Panicf("%d", "x") // ERROR "Panicf format %d has arg \x22x\x22 of wrong type string"
310	l.Panicln("%d", 1)  // ERROR "Panicln call has possible Printf formatting directive %d"
311	l.Print("%d", 1)    // ERROR "Print call has possible Printf formatting directive %d"
312	l.Printf("%d", "x") // ERROR "Printf format %d has arg \x22x\x22 of wrong type string"
313	l.Println("%d", 1)  // ERROR "Println call has possible Printf formatting directive %d"
314
315	// Issue 26486
316	dbg("", 1) // no error "call has arguments but no formatting directive"
317}
318
319func someString() string { return "X" }
320
321type someStruct struct{}
322
323// Log is non-variadic user-define Println-like function.
324// Calls to this func must be skipped when checking
325// for Println-like arguments.
326func (ss *someStruct) Log(f func(), s string) {}
327
328// Error is variadic user-define Println-like function.
329// Calls to this func mustn't be checked for Println-like arguments,
330// since variadic arguments type isn't interface{}.
331func (ss *someStruct) Error(args ...func()) {}
332
333// Println is variadic user-defined Println-like function.
334// Calls to this func must be checked for Println-like arguments.
335func (ss *someStruct) Println(args ...interface{}) {}
336
337// log is variadic user-defined Println-like function.
338// Calls to this func must be checked for Println-like arguments.
339func (ss *someStruct) log(f func(), args ...interface{}) {}
340
341// A function we use as a function value; it has no other purpose.
342func someFunction() {}
343
344// Printf is used by the test so we must declare it.
345func Printf(format string, args ...interface{}) {
346	fmt.Printf(format, args...)
347}
348
349// Println is used by the test so we must declare it.
350func Println(args ...interface{}) {
351	fmt.Println(args...)
352}
353
354// printf is used by the test so we must declare it.
355func printf(format string, args ...interface{}) {
356	fmt.Printf(format, args...)
357}
358
359// Errorf is used by the test for a case in which the first parameter
360// is not a format string.
361func Errorf(i int, format string, args ...interface{}) {
362	_ = fmt.Errorf(format, args...)
363}
364
365// errorf is used by the test for a case in which the function accepts multiple
366// string parameters before variadic arguments
367func errorf(level, format string, args ...interface{}) {
368	_ = fmt.Errorf(format, args...)
369}
370
371// multi is used by the test.
372func multi() []interface{} {
373	panic("don't call - testing only")
374}
375
376type stringer int
377
378func (stringer) String() string { return "string" }
379
380type ptrStringer float64
381
382var stringerv ptrStringer
383
384func (*ptrStringer) String() string {
385	return "string"
386}
387
388func (p *ptrStringer) Warn2(x int, args ...interface{}) string {
389	return p.Warn(x, args...)
390}
391
392func (p *ptrStringer) Warnf2(x int, format string, args ...interface{}) string {
393	return p.Warnf(x, format, args...)
394}
395
396func (*ptrStringer) Warn(x int, args ...interface{}) string {
397	return "warn"
398}
399
400func (*ptrStringer) Warnf(x int, format string, args ...interface{}) string {
401	return "warnf"
402}
403
404func (p *ptrStringer) Wrap2(x int, args ...interface{}) string {
405	return p.Wrap(x, args...)
406}
407
408func (p *ptrStringer) Wrapf2(x int, format string, args ...interface{}) string {
409	return p.Wrapf(x, format, args...)
410}
411
412func (*ptrStringer) Wrap(x int, args ...interface{}) string {
413	return fmt.Sprint(args...)
414}
415
416func (*ptrStringer) Wrapf(x int, format string, args ...interface{}) string {
417	return fmt.Sprintf(format, args...)
418}
419
420func (*ptrStringer) BadWrap(x int, args ...interface{}) string {
421	return fmt.Sprint(args) // ERROR "missing ... in args forwarded to print-like function"
422}
423
424func (*ptrStringer) BadWrapf(x int, format string, args ...interface{}) string {
425	return fmt.Sprintf(format, args) // ERROR "missing ... in args forwarded to printf-like function"
426}
427
428func (*ptrStringer) WrapfFalsePositive(x int, arg1 string, arg2 ...interface{}) string {
429	return fmt.Sprintf("%s %v", arg1, arg2)
430}
431
432type embeddedStringer struct {
433	foo string
434	ptrStringer
435	bar int
436}
437
438var embeddedStringerv embeddedStringer
439
440type notstringer struct {
441	f float64
442}
443
444var notstringerv notstringer
445
446type stringerarray [4]float64
447
448func (stringerarray) String() string {
449	return "string"
450}
451
452var stringerarrayv stringerarray
453
454type notstringerarray [4]float64
455
456var notstringerarrayv notstringerarray
457
458var nonemptyinterface = interface {
459	f()
460}(nil)
461
462// A data type we can print with "%d".
463type percentDStruct struct {
464	a int
465	b []byte
466	c *float64
467}
468
469var percentDV percentDStruct
470
471// A data type we cannot print correctly with "%d".
472type notPercentDStruct struct {
473	a int
474	b []byte
475	c bool
476}
477
478var notPercentDV notPercentDStruct
479
480// A data type we can print with "%s".
481type percentSStruct struct {
482	a string
483	b []byte
484	C stringerarray
485}
486
487var percentSV percentSStruct
488
489type recursiveStringer int
490
491func (s recursiveStringer) String() string {
492	_ = fmt.Sprintf("%d", s)
493	_ = fmt.Sprintf("%#v", s)
494	_ = fmt.Sprintf("%v", s)  // ERROR "Sprintf format %v with arg s causes recursive .*String method call"
495	_ = fmt.Sprintf("%v", &s) // ERROR "Sprintf format %v with arg &s causes recursive .*String method call"
496	_ = fmt.Sprintf("%T", s)  // ok; does not recursively call String
497	return fmt.Sprintln(s)    // ERROR "Sprintln arg s causes recursive call to .*String method"
498}
499
500type recursivePtrStringer int
501
502func (p *recursivePtrStringer) String() string {
503	_ = fmt.Sprintf("%v", *p)
504	_ = fmt.Sprint(&p)     // ok; prints address
505	return fmt.Sprintln(p) // ERROR "Sprintln arg p causes recursive call to .*String method"
506}
507
508type BoolFormatter bool
509
510func (*BoolFormatter) Format(fmt.State, rune) {
511}
512
513// Formatter with value receiver
514type FormatterVal bool
515
516func (FormatterVal) Format(fmt.State, rune) {
517}
518
519type RecursiveSlice []RecursiveSlice
520
521var recursiveSliceV = &RecursiveSlice{}
522
523type RecursiveMap map[int]RecursiveMap
524
525var recursiveMapV = make(RecursiveMap)
526
527type RecursiveStruct struct {
528	next *RecursiveStruct
529}
530
531var recursiveStructV = &RecursiveStruct{}
532
533type RecursiveStruct1 struct {
534	next *RecursiveStruct2
535}
536
537type RecursiveStruct2 struct {
538	next *RecursiveStruct1
539}
540
541var recursiveStruct1V = &RecursiveStruct1{}
542
543type unexportedInterface struct {
544	f interface{}
545}
546
547// Issue 17798: unexported ptrStringer cannot be formatted.
548type unexportedStringer struct {
549	t ptrStringer
550}
551type unexportedStringerOtherFields struct {
552	s string
553	t ptrStringer
554	S string
555}
556
557// Issue 17798: unexported error cannot be formatted.
558type unexportedError struct {
559	e error
560}
561type unexportedErrorOtherFields struct {
562	s string
563	e error
564	S string
565}
566
567type errorer struct{}
568
569func (e errorer) Error() string { return "errorer" }
570
571type unexportedCustomError struct {
572	e errorer
573}
574
575type errorInterface interface {
576	error
577	ExtraMethod()
578}
579
580type unexportedErrorInterface struct {
581	e errorInterface
582}
583
584func UnexportedStringerOrError() {
585	fmt.Printf("%s", unexportedInterface{"foo"}) // ok; prints {foo}
586	fmt.Printf("%s", unexportedInterface{3})     // ok; we can't see the problem
587
588	us := unexportedStringer{}
589	fmt.Printf("%s", us)  // ERROR "Printf format %s has arg us of wrong type .*print.unexportedStringer"
590	fmt.Printf("%s", &us) // ERROR "Printf format %s has arg &us of wrong type [*].*print.unexportedStringer"
591
592	usf := unexportedStringerOtherFields{
593		s: "foo",
594		S: "bar",
595	}
596	fmt.Printf("%s", usf)  // ERROR "Printf format %s has arg usf of wrong type .*print.unexportedStringerOtherFields"
597	fmt.Printf("%s", &usf) // ERROR "Printf format %s has arg &usf of wrong type [*].*print.unexportedStringerOtherFields"
598
599	ue := unexportedError{
600		e: &errorer{},
601	}
602	fmt.Printf("%s", ue)  // ERROR "Printf format %s has arg ue of wrong type .*print.unexportedError"
603	fmt.Printf("%s", &ue) // ERROR "Printf format %s has arg &ue of wrong type [*].*print.unexportedError"
604
605	uef := unexportedErrorOtherFields{
606		s: "foo",
607		e: &errorer{},
608		S: "bar",
609	}
610	fmt.Printf("%s", uef)  // ERROR "Printf format %s has arg uef of wrong type .*print.unexportedErrorOtherFields"
611	fmt.Printf("%s", &uef) // ERROR "Printf format %s has arg &uef of wrong type [*].*print.unexportedErrorOtherFields"
612
613	uce := unexportedCustomError{
614		e: errorer{},
615	}
616	fmt.Printf("%s", uce) // ERROR "Printf format %s has arg uce of wrong type .*print.unexportedCustomError"
617
618	uei := unexportedErrorInterface{}
619	fmt.Printf("%s", uei)       // ERROR "Printf format %s has arg uei of wrong type .*print.unexportedErrorInterface"
620	fmt.Println("foo\n", "bar") // not an error
621
622	fmt.Println("foo\n")  // ERROR "Println arg list ends with redundant newline"
623	fmt.Println("foo\\n") // not an error
624	fmt.Println(`foo\n`)  // not an error
625
626	intSlice := []int{3, 4}
627	fmt.Printf("%s", intSlice) // ERROR "Printf format %s has arg intSlice of wrong type \[\]int"
628	nonStringerArray := [1]unexportedStringer{{}}
629	fmt.Printf("%s", nonStringerArray)  // ERROR "Printf format %s has arg nonStringerArray of wrong type \[1\].*print.unexportedStringer"
630	fmt.Printf("%s", []stringer{3, 4})  // not an error
631	fmt.Printf("%s", [2]stringer{3, 4}) // not an error
632}
633
634// TODO: Disable complaint about '0' for Go 1.10. To be fixed properly in 1.11.
635// See issues 23598 and 23605.
636func DisableErrorForFlag0() {
637	fmt.Printf("%0t", true)
638}
639
640// Issue 26486.
641func dbg(format string, args ...interface{}) {
642	if format == "" {
643		format = "%v"
644	}
645	fmt.Printf(format, args...)
646}
647
648func PointersToCompoundTypes() {
649	stringSlice := []string{"a", "b"}
650	fmt.Printf("%s", &stringSlice) // not an error
651
652	intSlice := []int{3, 4}
653	fmt.Printf("%s", &intSlice) // ERROR "Printf format %s has arg &intSlice of wrong type \*\[\]int"
654
655	stringArray := [2]string{"a", "b"}
656	fmt.Printf("%s", &stringArray) // not an error
657
658	intArray := [2]int{3, 4}
659	fmt.Printf("%s", &intArray) // ERROR "Printf format %s has arg &intArray of wrong type \*\[2\]int"
660
661	stringStruct := struct{ F string }{"foo"}
662	fmt.Printf("%s", &stringStruct) // not an error
663
664	intStruct := struct{ F int }{3}
665	fmt.Printf("%s", &intStruct) // ERROR "Printf format %s has arg &intStruct of wrong type \*struct{F int}"
666
667	stringMap := map[string]string{"foo": "bar"}
668	fmt.Printf("%s", &stringMap) // not an error
669
670	intMap := map[int]int{3: 4}
671	fmt.Printf("%s", &intMap) // ERROR "Printf format %s has arg &intMap of wrong type \*map\[int\]int"
672
673	type T2 struct {
674		X string
675	}
676	type T1 struct {
677		X *T2
678	}
679	fmt.Printf("%s\n", T1{&T2{"x"}}) // ERROR "Printf format %s has arg T1{&T2{.x.}} of wrong type .*print\.T1"
680}
681