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.
5 // This file contains tests for the printf checker.
11 logpkg "log" // renamed to make it harder to see
15 "unsafe" // just for test case printing unsafe.Pointer
17 // For testing printf-like functions from external package.
18 // "github.com/foobar/externalprintf"
22 func UnsafePointerPrintfTest() {
24 fmt.Printf("%p, %x %X", up, up, up)
27 // Error methods that do not satisfy the Error interface and should be checked.
30 func (errorTest1) Error(...interface{}) string {
34 type errorTest2 int // Analogous to testing's *T type.
35 func (errorTest2) Error(...interface{}) {
40 func (errorTest3) Error() { // No return value.
45 func (errorTest4) Error() int { // Different return type.
51 func (errorTest5) error() { // niladic; don't complain if no args (was bug)
54 // This function never executes, but it serves as a simple test for the program.
55 // Test with make test.
67 // Some good format/argtypes
69 fmt.Printf("%b %b %b", 3, i, x)
70 fmt.Printf("%c %c %c %c", 3, i, 'x', r)
71 fmt.Printf("%d %d %d", 3, i, imap)
72 fmt.Printf("%e %e %e %e", 3e9, x, fslice, c)
73 fmt.Printf("%E %E %E %E", 3e9, x, fslice, c)
74 fmt.Printf("%f %f %f %f", 3e9, x, fslice, c)
75 fmt.Printf("%F %F %F %F", 3e9, x, fslice, c)
76 fmt.Printf("%g %g %g %g", 3e9, x, fslice, c)
77 fmt.Printf("%G %G %G %G", 3e9, x, fslice, c)
78 fmt.Printf("%b %b %b %b", 3e9, x, fslice, c)
79 fmt.Printf("%o %o", 3, i)
80 fmt.Printf("%O %O", 3, i)
82 fmt.Printf("%q %q %q %q", 3, i, 'x', r)
83 fmt.Printf("%s %s %s", "hi", s, []byte{65})
84 fmt.Printf("%t %t", true, b)
85 fmt.Printf("%T %T", 3, i)
86 fmt.Printf("%U %U", 3, i)
87 fmt.Printf("%v %v", 3, i)
88 fmt.Printf("%x %x %x %x %x %x %x", 3, i, "hi", s, x, c, fslice)
89 fmt.Printf("%X %X %X %X %X %X %X", 3, i, "hi", s, x, c, fslice)
90 fmt.Printf("%.*s %d %g", 3, "hi", 23, 2.3)
91 fmt.Printf("%s", &stringerv)
92 fmt.Printf("%v", &stringerv)
93 fmt.Printf("%T", &stringerv)
94 fmt.Printf("%s", &embeddedStringerv)
95 fmt.Printf("%v", &embeddedStringerv)
96 fmt.Printf("%T", &embeddedStringerv)
97 fmt.Printf("%v", notstringerv)
98 fmt.Printf("%T", notstringerv)
99 fmt.Printf("%q", stringerarrayv)
100 fmt.Printf("%v", stringerarrayv)
101 fmt.Printf("%s", stringerarrayv)
102 fmt.Printf("%v", notstringerarrayv)
103 fmt.Printf("%T", notstringerarrayv)
104 fmt.Printf("%d", new(fmt.Formatter))
105 fmt.Printf("%*%", 2) // Ridiculous but allowed.
106 fmt.Printf("%s", interface{}(nil)) // Nothing useful we can say.
107 fmt.Printf("%a", interface{}(new(BoolFormatter))) // Could be a fmt.Formatter.
109 fmt.Printf("%g", 1+2i)
110 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
111 // Some bad format/argTypes
112 fmt.Printf("%b", "hi") // want "Printf format %b has arg \x22hi\x22 of wrong type string"
113 fmt.Printf("%t", c) // want "Printf format %t has arg c of wrong type complex64"
114 fmt.Printf("%t", 1+2i) // want `Printf format %t has arg 1 \+ 2i of wrong type complex128`
115 fmt.Printf("%c", 2.3) // want "Printf format %c has arg 2.3 of wrong type float64"
116 fmt.Printf("%d", 2.3) // want "Printf format %d has arg 2.3 of wrong type float64"
117 fmt.Printf("%e", "hi") // want `Printf format %e has arg "hi" of wrong type string`
118 fmt.Printf("%E", true) // want "Printf format %E has arg true of wrong type bool"
119 fmt.Printf("%f", "hi") // want "Printf format %f has arg \x22hi\x22 of wrong type string"
120 fmt.Printf("%F", 'x') // want "Printf format %F has arg 'x' of wrong type rune"
121 fmt.Printf("%g", "hi") // want `Printf format %g has arg "hi" of wrong type string`
122 fmt.Printf("%g", imap) // want `Printf format %g has arg imap of wrong type map\[int\]int`
123 fmt.Printf("%G", i) // want "Printf format %G has arg i of wrong type int"
124 fmt.Printf("%o", x) // want "Printf format %o has arg x of wrong type float64"
125 fmt.Printf("%O", x) // want "Printf format %O has arg x of wrong type float64"
126 fmt.Printf("%p", nil) // want "Printf format %p has arg nil of wrong type untyped nil"
127 fmt.Printf("%p", 23) // want "Printf format %p has arg 23 of wrong type int"
128 fmt.Printf("%q", x) // want "Printf format %q has arg x of wrong type float64"
129 fmt.Printf("%s", b) // want "Printf format %s has arg b of wrong type bool"
130 fmt.Printf("%s", byte(65)) // want `Printf format %s has arg byte\(65\) of wrong type byte`
131 fmt.Printf("%t", 23) // want "Printf format %t has arg 23 of wrong type int"
132 fmt.Printf("%U", x) // want "Printf format %U has arg x of wrong type float64"
133 fmt.Printf("%x", nil) // want "Printf format %x has arg nil of wrong type untyped nil"
134 fmt.Printf("%s", stringerv) // want "Printf format %s has arg stringerv of wrong type a.ptrStringer"
135 fmt.Printf("%t", stringerv) // want "Printf format %t has arg stringerv of wrong type a.ptrStringer"
136 fmt.Printf("%s", embeddedStringerv) // want "Printf format %s has arg embeddedStringerv of wrong type a.embeddedStringer"
137 fmt.Printf("%t", embeddedStringerv) // want "Printf format %t has arg embeddedStringerv of wrong type a.embeddedStringer"
138 fmt.Printf("%q", notstringerv) // want "Printf format %q has arg notstringerv of wrong type a.notstringer"
139 fmt.Printf("%t", notstringerv) // want "Printf format %t has arg notstringerv of wrong type a.notstringer"
140 fmt.Printf("%t", stringerarrayv) // want "Printf format %t has arg stringerarrayv of wrong type a.stringerarray"
141 fmt.Printf("%t", notstringerarrayv) // want "Printf format %t has arg notstringerarrayv of wrong type a.notstringerarray"
142 fmt.Printf("%q", notstringerarrayv) // want "Printf format %q has arg notstringerarrayv of wrong type a.notstringerarray"
143 fmt.Printf("%d", BoolFormatter(true)) // want `Printf format %d has arg BoolFormatter\(true\) of wrong type a.BoolFormatter`
144 fmt.Printf("%z", FormatterVal(true)) // correct (the type is responsible for formatting)
145 fmt.Printf("%d", FormatterVal(true)) // correct (the type is responsible for formatting)
146 fmt.Printf("%s", nonemptyinterface) // correct (the type is responsible for formatting)
147 fmt.Printf("%.*s %d %6g", 3, "hi", 23, 'x') // want "Printf format %6g has arg 'x' of wrong type rune"
148 fmt.Println() // not an error
149 fmt.Println("%s", "hi") // want "Println call has possible formatting directive %s"
150 fmt.Println("%v", "hi") // want "Println call has possible formatting directive %v"
151 fmt.Println("%T", "hi") // want "Println call has possible formatting directive %T"
152 fmt.Println("0.0%") // correct (trailing % couldn't be a formatting directive)
153 fmt.Printf("%s", "hi", 3) // want "Printf call needs 1 arg but has 2 args"
154 _ = fmt.Sprintf("%"+("s"), "hi", 3) // want "Sprintf call needs 1 arg but has 2 args"
155 fmt.Printf("%s%%%d", "hi", 3) // correct
156 fmt.Printf("%08s", "woo") // correct
157 fmt.Printf("% 8s", "woo") // correct
158 fmt.Printf("%.*d", 3, 3) // correct
159 fmt.Printf("%.*d x", 3, 3, 3, 3) // want "Printf call needs 2 args but has 4 args"
160 fmt.Printf("%.*d x", "hi", 3) // want `Printf format %.*d uses non-int "hi" as argument of \*`
161 fmt.Printf("%.*d x", i, 3) // correct
162 fmt.Printf("%.*d x", s, 3) // want `Printf format %.\*d uses non-int s as argument of \*`
163 fmt.Printf("%*% x", 0.22) // want `Printf format %\*% uses non-int 0.22 as argument of \*`
164 fmt.Printf("%q %q", multi()...) // ok
165 fmt.Printf("%#q", `blah`) // ok
166 fmt.Printf("%#b", 3) // ok
167 // printf("now is the time", "buddy") // no error "printf call has arguments but no formatting directives"
168 Printf("now is the time", "buddy") // want "Printf call has arguments but no formatting directives"
170 const format = "%s %s\n"
171 Printf(format, "hi", "there")
172 Printf(format, "hi") // want "Printf format %s reads arg #2, but call has 1 arg$"
173 Printf("%s %d %.3v %q", "str", 4) // want "Printf format %.3v reads arg #3, but call has 2 args"
174 f := new(ptrStringer)
175 f.Warn(0, "%s", "hello", 3) // want "Warn call has possible formatting directive %s"
176 f.Warnf(0, "%s", "hello", 3) // want "Warnf call needs 1 arg but has 2 args"
177 f.Warnf(0, "%r", "hello") // want "Warnf format %r has unknown verb r"
178 f.Warnf(0, "%#s", "hello") // want "Warnf format %#s has unrecognized flag #"
179 f.Warn2(0, "%s", "hello", 3) // want "Warn2 call has possible formatting directive %s"
180 f.Warnf2(0, "%s", "hello", 3) // want "Warnf2 call needs 1 arg but has 2 args"
181 f.Warnf2(0, "%r", "hello") // want "Warnf2 format %r has unknown verb r"
182 f.Warnf2(0, "%#s", "hello") // want "Warnf2 format %#s has unrecognized flag #"
183 f.Wrap(0, "%s", "hello", 3) // want "Wrap call has possible formatting directive %s"
184 f.Wrapf(0, "%s", "hello", 3) // want "Wrapf call needs 1 arg but has 2 args"
185 f.Wrapf(0, "%r", "hello") // want "Wrapf format %r has unknown verb r"
186 f.Wrapf(0, "%#s", "hello") // want "Wrapf format %#s has unrecognized flag #"
187 f.Wrap2(0, "%s", "hello", 3) // want "Wrap2 call has possible formatting directive %s"
188 f.Wrapf2(0, "%s", "hello", 3) // want "Wrapf2 call needs 1 arg but has 2 args"
189 f.Wrapf2(0, "%r", "hello") // want "Wrapf2 format %r has unknown verb r"
190 f.Wrapf2(0, "%#s", "hello") // want "Wrapf2 format %#s has unrecognized flag #"
191 fmt.Printf("%#s", FormatterVal(true)) // correct (the type is responsible for formatting)
192 Printf("d%", 2) // want "Printf format % is missing verb at end of string"
193 Printf("%d", percentDV)
194 Printf("%d", &percentDV)
195 Printf("%d", notPercentDV) // want "Printf format %d has arg notPercentDV of wrong type a.notPercentDStruct"
196 Printf("%d", ¬PercentDV) // want `Printf format %d has arg ¬PercentDV of wrong type \*a.notPercentDStruct`
197 Printf("%p", ¬PercentDV) // Works regardless: we print it as a pointer.
198 Printf("%q", &percentDV) // want `Printf format %q has arg &percentDV of wrong type \*a.percentDStruct`
199 Printf("%s", percentSV)
200 Printf("%s", &percentSV)
201 // Good argument reorderings.
203 Printf("%[1]*d", 3, 1)
204 Printf("%[2]*[1]d", 1, 3)
205 Printf("%[2]*.[1]*[3]d", 2, 3, 4)
206 fmt.Fprintf(os.Stderr, "%[2]*.[1]*[3]d", 2, 3, 4) // Use Fprintf to make sure we count arguments correctly.
207 // Bad argument reorderings.
208 Printf("%[xd", 3) // want `Printf format %\[xd is missing closing \]`
209 Printf("%[x]d x", 3) // want `Printf format has invalid argument index \[x\]`
210 Printf("%[3]*s x", "hi", 2) // want `Printf format has invalid argument index \[3\]`
211 _ = fmt.Sprintf("%[3]d x", 2) // want `Sprintf format has invalid argument index \[3\]`
212 Printf("%[2]*.[1]*[3]d x", 2, "hi", 4) // want `Printf format %\[2]\*\.\[1\]\*\[3\]d uses non-int \x22hi\x22 as argument of \*`
213 Printf("%[0]s x", "arg1") // want `Printf format has invalid argument index \[0\]`
214 Printf("%[0]d x", 1) // want `Printf format has invalid argument index \[0\]`
215 // Something that satisfies the error interface.
217 fmt.Println(e.Error()) // ok
218 // Something that looks like an error interface but isn't, such as the (*T).Error method
219 // in the testing package.
222 et1.Error("hi") // ok
223 et1.Error("%d", 3) // want "Error call has possible formatting directive %d"
224 et1.Errorf("%s", 1) // want "Errorf format %s has arg 1 of wrong type int"
226 et3.Error() // ok, not an error method.
228 et4.Error() // ok, not an error method.
230 et5.error() // ok, not an error method.
231 // Interfaces can be used with any verb.
232 var iface interface {
233 ToTheMadness() bool // Method ToTheMadness usually returns false
235 fmt.Printf("%f", iface) // ok: fmt treats interfaces as transparent and iface may well have a float concrete type
236 // Can't print a function.
237 Printf("%d", someFunction) // want "Printf format %d arg someFunction is a func value, not called"
238 Printf("%v", someFunction) // want "Printf format %v arg someFunction is a func value, not called"
239 Println(someFunction) // want "Println arg someFunction is a func value, not called"
240 Printf("%p", someFunction) // ok: maybe someone wants to see the pointer
241 Printf("%T", someFunction) // ok: maybe someone wants to see the type
242 // Bug: used to recur forever.
243 Printf("%p %x", recursiveStructV, recursiveStructV.next)
244 Printf("%p %x", recursiveStruct1V, recursiveStruct1V.next) // want `Printf format %x has arg recursiveStruct1V\.next of wrong type \*a\.RecursiveStruct2`
245 Printf("%p %x", recursiveSliceV, recursiveSliceV)
246 Printf("%p %x", recursiveMapV, recursiveMapV)
247 // Special handling for Log.
250 t.Log("%d", 3) // want "Log call has possible formatting directive %d"
252 t.Logf("%d", "hi") // want `Logf format %d has arg "hi" of wrong type string`
254 Errorf(1, "%d", 3) // OK
255 Errorf(1, "%d", "hi") // want `Errorf format %d has arg "hi" of wrong type string`
257 // Multiple string arguments before variadic args
258 errorf("WARNING", "foobar") // OK
259 errorf("INFO", "s=%s, n=%d", "foo", 1) // OK
260 errorf("ERROR", "%d") // want "errorf format %d reads arg #1, but call has 0 args"
263 tb.Errorf("%s", 1) // want "Errorf format %s has arg 1 of wrong type int"
265 // Printf from external package
266 // externalprintf.Printf("%d", 42) // OK
267 // externalprintf.Printf("foobar") // OK
269 // externalprintf.Logf(level, "%d", 42) // OK
270 // externalprintf.Errorf(level, level, "foo %q bar", "foobar") // OK
271 // externalprintf.Logf(level, "%d") // no error "Logf format %d reads arg #1, but call has 0 args"
272 // var formatStr = "%s %s"
273 // externalprintf.Sprintf(formatStr, "a", "b") // OK
274 // externalprintf.Logf(level, formatStr, "a", "b") // OK
276 // user-defined Println-like functions
278 ss.Log(someFunction, "foo") // OK
279 ss.Error(someFunction, someFunction) // OK
281 ss.Println(1.234, "foo") // OK
282 ss.Println(1, someFunction) // no error "Println arg someFunction is a func value, not called"
283 ss.log(someFunction) // OK
284 ss.log(someFunction, "bar", 1.33) // OK
285 ss.log(someFunction, someFunction) // no error "log arg someFunction is a func value, not called"
288 Printf("%d %[3]d %d %[2]d x", 1, 2, 3, 4) // OK
289 Printf("%d %[0]d %d %[2]d x", 1, 2, 3, 4) // want `Printf format has invalid argument index \[0\]`
290 Printf("%d %[3]d %d %[-2]d x", 1, 2, 3, 4) // want `Printf format has invalid argument index \[-2\]`
291 Printf("%d %[3]d %d %[2234234234234]d x", 1, 2, 3, 4) // want `Printf format has invalid argument index \[2234234234234\]`
292 Printf("%d %[3]d %-10d %[2]d x", 1, 2, 3) // want "Printf format %-10d reads arg #4, but call has 3 args"
293 Printf("%[1][3]d x", 1, 2) // want `Printf format %\[1\]\[ has unknown verb \[`
294 Printf("%[1]d x", 1, 2) // OK
295 Printf("%d %[3]d %d %[2]d x", 1, 2, 3, 4, 5) // OK
297 // wrote Println but meant Fprintln
298 Printf("%p\n", os.Stdout) // OK
299 Println(os.Stdout, "hello") // want "Println does not take io.Writer but has first arg os.Stdout"
301 Printf(someString(), "hello") // OK
303 // Printf wrappers in package log should be detected automatically
304 logpkg.Fatal("%d", 1) // want "Fatal call has possible formatting directive %d"
305 logpkg.Fatalf("%d", "x") // want `Fatalf format %d has arg "x" of wrong type string`
306 logpkg.Fatalln("%d", 1) // want "Fatalln call has possible formatting directive %d"
307 logpkg.Panic("%d", 1) // want "Panic call has possible formatting directive %d"
308 logpkg.Panicf("%d", "x") // want `Panicf format %d has arg "x" of wrong type string`
309 logpkg.Panicln("%d", 1) // want "Panicln call has possible formatting directive %d"
310 logpkg.Print("%d", 1) // want "Print call has possible formatting directive %d"
311 logpkg.Printf("%d", "x") // want `Printf format %d has arg "x" of wrong type string`
312 logpkg.Println("%d", 1) // want "Println call has possible formatting directive %d"
316 l.Fatal("%d", 1) // want "Fatal call has possible formatting directive %d"
317 l.Fatalf("%d", "x") // want `Fatalf format %d has arg "x" of wrong type string`
318 l.Fatalln("%d", 1) // want "Fatalln call has possible formatting directive %d"
319 l.Panic("%d", 1) // want "Panic call has possible formatting directive %d"
320 l.Panicf("%d", "x") // want `Panicf format %d has arg "x" of wrong type string`
321 l.Panicln("%d", 1) // want "Panicln call has possible formatting directive %d"
322 l.Print("%d", 1) // want "Print call has possible formatting directive %d"
323 l.Printf("%d", "x") // want `Printf format %d has arg "x" of wrong type string`
324 l.Println("%d", 1) // want "Println call has possible formatting directive %d"
327 dbg("", 1) // no error "call has arguments but no formatting directive"
330 _ = fmt.Errorf("%w", err)
331 _ = fmt.Errorf("%#w", err)
332 _ = fmt.Errorf("%[2]w %[1]s", "x", err)
333 _ = fmt.Errorf("%[2]w %[1]s", e, "x") // want `Errorf format %\[2\]w has arg "x" of wrong type string`
334 _ = fmt.Errorf("%w", "x") // want `Errorf format %w has arg "x" of wrong type string`
335 _ = fmt.Errorf("%w %w", err, err) // want `Errorf call has more than one error-wrapping directive %w`
336 fmt.Printf("%w", err) // want `Printf call has error-wrapping directive %w`
340 func someString() string { return "X" }
342 type someStruct struct{}
344 // Log is non-variadic user-define Println-like function.
345 // Calls to this func must be skipped when checking
346 // for Println-like arguments.
347 func (ss *someStruct) Log(f func(), s string) {}
349 // Error is variadic user-define Println-like function.
350 // Calls to this func mustn't be checked for Println-like arguments,
351 // since variadic arguments type isn't interface{}.
352 func (ss *someStruct) Error(args ...func()) {}
354 // Println is variadic user-defined Println-like function.
355 // Calls to this func must be checked for Println-like arguments.
356 func (ss *someStruct) Println(args ...interface{}) {}
358 // log is variadic user-defined Println-like function.
359 // Calls to this func must be checked for Println-like arguments.
360 func (ss *someStruct) log(f func(), args ...interface{}) {}
362 // A function we use as a function value; it has no other purpose.
363 func someFunction() {}
365 // Printf is used by the test so we must declare it.
366 func Printf(format string, args ...interface{}) { // want Printf:"printfWrapper"
367 fmt.Printf(format, args...)
370 // Println is used by the test so we must declare it.
371 func Println(args ...interface{}) { // want Println:"printWrapper"
375 // printf is used by the test so we must declare it.
376 func printf(format string, args ...interface{}) { // want printf:"printfWrapper"
377 fmt.Printf(format, args...)
380 // Errorf is used by the test for a case in which the first parameter
381 // is not a format string.
382 func Errorf(i int, format string, args ...interface{}) { // want Errorf:"errorfWrapper"
383 _ = fmt.Errorf(format, args...)
386 // errorf is used by the test for a case in which the function accepts multiple
387 // string parameters before variadic arguments
388 func errorf(level, format string, args ...interface{}) { // want errorf:"errorfWrapper"
389 _ = fmt.Errorf(format, args...)
392 // multi is used by the test.
393 func multi() []interface{} {
394 panic("don't call - testing only")
399 func (stringer) String() string { return "string" }
401 type ptrStringer float64
403 var stringerv ptrStringer
405 func (*ptrStringer) String() string {
409 func (p *ptrStringer) Warn2(x int, args ...interface{}) string { // want Warn2:"printWrapper"
410 return p.Warn(x, args...)
413 func (p *ptrStringer) Warnf2(x int, format string, args ...interface{}) string { // want Warnf2:"printfWrapper"
414 return p.Warnf(x, format, args...)
417 // During testing -printf.funcs flag matches Warn.
418 func (*ptrStringer) Warn(x int, args ...interface{}) string {
422 // During testing -printf.funcs flag matches Warnf.
423 func (*ptrStringer) Warnf(x int, format string, args ...interface{}) string {
427 func (p *ptrStringer) Wrap2(x int, args ...interface{}) string { // want Wrap2:"printWrapper"
428 return p.Wrap(x, args...)
431 func (p *ptrStringer) Wrapf2(x int, format string, args ...interface{}) string { // want Wrapf2:"printfWrapper"
432 return p.Wrapf(x, format, args...)
435 func (*ptrStringer) Wrap(x int, args ...interface{}) string { // want Wrap:"printWrapper"
436 return fmt.Sprint(args...)
439 func (*ptrStringer) Wrapf(x int, format string, args ...interface{}) string { // want Wrapf:"printfWrapper"
440 return fmt.Sprintf(format, args...)
443 func (*ptrStringer) BadWrap(x int, args ...interface{}) string {
444 return fmt.Sprint(args) // want "missing ... in args forwarded to print-like function"
447 func (*ptrStringer) BadWrapf(x int, format string, args ...interface{}) string {
448 return fmt.Sprintf(format, args) // want "missing ... in args forwarded to printf-like function"
451 func (*ptrStringer) WrapfFalsePositive(x int, arg1 string, arg2 ...interface{}) string {
452 return fmt.Sprintf("%s %v", arg1, arg2)
455 type embeddedStringer struct {
461 var embeddedStringerv embeddedStringer
463 type notstringer struct {
467 var notstringerv notstringer
469 type stringerarray [4]float64
471 func (stringerarray) String() string {
475 var stringerarrayv stringerarray
477 type notstringerarray [4]float64
479 var notstringerarrayv notstringerarray
481 var nonemptyinterface = interface {
485 // A data type we can print with "%d".
486 type percentDStruct struct {
492 var percentDV percentDStruct
494 // A data type we cannot print correctly with "%d".
495 type notPercentDStruct struct {
501 var notPercentDV notPercentDStruct
503 // A data type we can print with "%s".
504 type percentSStruct struct {
510 var percentSV percentSStruct
512 type recursiveStringer int
514 func (s recursiveStringer) String() string {
515 _ = fmt.Sprintf("%d", s)
516 _ = fmt.Sprintf("%#v", s)
517 _ = fmt.Sprintf("%v", s) // want "Sprintf format %v with arg s causes recursive String method call"
518 _ = fmt.Sprintf("%v", &s) // want "Sprintf format %v with arg &s causes recursive String method call"
519 _ = fmt.Sprintf("%T", s) // ok; does not recursively call String
520 return fmt.Sprintln(s) // want "Sprintln arg s causes recursive call to String method"
523 type recursivePtrStringer int
525 func (p *recursivePtrStringer) String() string {
526 _ = fmt.Sprintf("%v", *p)
527 _ = fmt.Sprint(&p) // ok; prints address
528 return fmt.Sprintln(p) // want "Sprintln arg p causes recursive call to String method"
531 type recursiveError int
533 func (s recursiveError) Error() string {
534 _ = fmt.Sprintf("%d", s)
535 _ = fmt.Sprintf("%#v", s)
536 _ = fmt.Sprintf("%v", s) // want "Sprintf format %v with arg s causes recursive Error method call"
537 _ = fmt.Sprintf("%v", &s) // want "Sprintf format %v with arg &s causes recursive Error method call"
538 _ = fmt.Sprintf("%T", s) // ok; does not recursively call Error
539 return fmt.Sprintln(s) // want "Sprintln arg s causes recursive call to Error method"
542 type recursivePtrError int
544 func (p *recursivePtrError) Error() string {
545 _ = fmt.Sprintf("%v", *p)
546 _ = fmt.Sprint(&p) // ok; prints address
547 return fmt.Sprintln(p) // want "Sprintln arg p causes recursive call to Error method"
550 type recursiveStringerAndError int
552 func (s recursiveStringerAndError) String() string {
553 _ = fmt.Sprintf("%d", s)
554 _ = fmt.Sprintf("%#v", s)
555 _ = fmt.Sprintf("%v", s) // want "Sprintf format %v with arg s causes recursive String method call"
556 _ = fmt.Sprintf("%v", &s) // want "Sprintf format %v with arg &s causes recursive String method call"
557 _ = fmt.Sprintf("%T", s) // ok; does not recursively call String
558 return fmt.Sprintln(s) // want "Sprintln arg s causes recursive call to String method"
561 func (s recursiveStringerAndError) Error() string {
562 _ = fmt.Sprintf("%d", s)
563 _ = fmt.Sprintf("%#v", s)
564 _ = fmt.Sprintf("%v", s) // want "Sprintf format %v with arg s causes recursive Error method call"
565 _ = fmt.Sprintf("%v", &s) // want "Sprintf format %v with arg &s causes recursive Error method call"
566 _ = fmt.Sprintf("%T", s) // ok; does not recursively call Error
567 return fmt.Sprintln(s) // want "Sprintln arg s causes recursive call to Error method"
570 type recursivePtrStringerAndError int
572 func (p *recursivePtrStringerAndError) String() string {
573 _ = fmt.Sprintf("%v", *p)
574 _ = fmt.Sprint(&p) // ok; prints address
575 return fmt.Sprintln(p) // want "Sprintln arg p causes recursive call to String method"
578 func (p *recursivePtrStringerAndError) Error() string {
579 _ = fmt.Sprintf("%v", *p)
580 _ = fmt.Sprint(&p) // ok; prints address
581 return fmt.Sprintln(p) // want "Sprintln arg p causes recursive call to Error method"
584 // implements a String() method but with non-matching return types
585 type nonStringerWrongReturn int
587 func (s nonStringerWrongReturn) String() (string, error) {
588 return "", fmt.Errorf("%v", s)
591 // implements a String() method but with non-matching arguments
592 type nonStringerWrongArgs int
594 func (s nonStringerWrongArgs) String(i int) string {
595 return fmt.Sprintf("%d%v", i, s)
603 func (cons *cons) String() string {
607 _ = fmt.Sprint(cons.cdr) // don't want "recursive call" diagnostic
608 return fmt.Sprintf("(%d . %v)", cons.car, cons.cdr) // don't want "recursive call" diagnostic
611 type BoolFormatter bool
613 func (*BoolFormatter) Format(fmt.State, rune) {
616 // Formatter with value receiver
617 type FormatterVal bool
619 func (FormatterVal) Format(fmt.State, rune) {
622 type RecursiveSlice []RecursiveSlice
624 var recursiveSliceV = &RecursiveSlice{}
626 type RecursiveMap map[int]RecursiveMap
628 var recursiveMapV = make(RecursiveMap)
630 type RecursiveStruct struct {
631 next *RecursiveStruct
634 var recursiveStructV = &RecursiveStruct{}
636 type RecursiveStruct1 struct {
637 next *RecursiveStruct2
640 type RecursiveStruct2 struct {
641 next *RecursiveStruct1
644 var recursiveStruct1V = &RecursiveStruct1{}
646 type unexportedInterface struct {
650 // Issue 17798: unexported ptrStringer cannot be formatted.
651 type unexportedStringer struct {
654 type unexportedStringerOtherFields struct {
660 // Issue 17798: unexported error cannot be formatted.
661 type unexportedError struct {
664 type unexportedErrorOtherFields struct {
670 type errorer struct{}
672 func (e errorer) Error() string { return "errorer" }
674 type unexportedCustomError struct {
678 type errorInterface interface {
683 type unexportedErrorInterface struct {
687 func UnexportedStringerOrError() {
688 fmt.Printf("%s", unexportedInterface{"foo"}) // ok; prints {foo}
689 fmt.Printf("%s", unexportedInterface{3}) // ok; we can't see the problem
691 us := unexportedStringer{}
692 fmt.Printf("%s", us) // want "Printf format %s has arg us of wrong type a.unexportedStringer"
693 fmt.Printf("%s", &us) // want "Printf format %s has arg &us of wrong type [*]a.unexportedStringer"
695 usf := unexportedStringerOtherFields{
699 fmt.Printf("%s", usf) // want "Printf format %s has arg usf of wrong type a.unexportedStringerOtherFields"
700 fmt.Printf("%s", &usf) // want "Printf format %s has arg &usf of wrong type [*]a.unexportedStringerOtherFields"
702 ue := unexportedError{
705 fmt.Printf("%s", ue) // want "Printf format %s has arg ue of wrong type a.unexportedError"
706 fmt.Printf("%s", &ue) // want "Printf format %s has arg &ue of wrong type [*]a.unexportedError"
708 uef := unexportedErrorOtherFields{
713 fmt.Printf("%s", uef) // want "Printf format %s has arg uef of wrong type a.unexportedErrorOtherFields"
714 fmt.Printf("%s", &uef) // want "Printf format %s has arg &uef of wrong type [*]a.unexportedErrorOtherFields"
716 uce := unexportedCustomError{
719 fmt.Printf("%s", uce) // want "Printf format %s has arg uce of wrong type a.unexportedCustomError"
721 uei := unexportedErrorInterface{}
722 fmt.Printf("%s", uei) // want "Printf format %s has arg uei of wrong type a.unexportedErrorInterface"
723 fmt.Println("foo\n", "bar") // not an error
725 fmt.Println("foo\n") // want "Println arg list ends with redundant newline"
726 fmt.Println("foo\\n") // not an error
727 fmt.Println(`foo\n`) // not an error
729 intSlice := []int{3, 4}
730 fmt.Printf("%s", intSlice) // want `Printf format %s has arg intSlice of wrong type \[\]int`
731 nonStringerArray := [1]unexportedStringer{{}}
732 fmt.Printf("%s", nonStringerArray) // want `Printf format %s has arg nonStringerArray of wrong type \[1\]a.unexportedStringer`
733 fmt.Printf("%s", []stringer{3, 4}) // not an error
734 fmt.Printf("%s", [2]stringer{3, 4}) // not an error
737 // TODO: Disable complaint about '0' for Go 1.10. To be fixed properly in 1.11.
738 // See issues 23598 and 23605.
739 func DisableErrorForFlag0() {
740 fmt.Printf("%0t", true)
744 func dbg(format string, args ...interface{}) {
748 fmt.Printf(format, args...)
751 func PointersToCompoundTypes() {
752 stringSlice := []string{"a", "b"}
753 fmt.Printf("%s", &stringSlice) // not an error
755 intSlice := []int{3, 4}
756 fmt.Printf("%s", &intSlice) // want `Printf format %s has arg &intSlice of wrong type \*\[\]int`
758 stringArray := [2]string{"a", "b"}
759 fmt.Printf("%s", &stringArray) // not an error
761 intArray := [2]int{3, 4}
762 fmt.Printf("%s", &intArray) // want `Printf format %s has arg &intArray of wrong type \*\[2\]int`
764 stringStruct := struct{ F string }{"foo"}
765 fmt.Printf("%s", &stringStruct) // not an error
767 intStruct := struct{ F int }{3}
768 fmt.Printf("%s", &intStruct) // want `Printf format %s has arg &intStruct of wrong type \*struct{F int}`
770 stringMap := map[string]string{"foo": "bar"}
771 fmt.Printf("%s", &stringMap) // not an error
773 intMap := map[int]int{3: 4}
774 fmt.Printf("%s", &intMap) // want `Printf format %s has arg &intMap of wrong type \*map\[int\]int`
782 fmt.Printf("%s\n", T1{&T2{"x"}}) // want `Printf format %s has arg T1{&T2{.x.}} of wrong type a\.T1`
785 // Printf wrappers from external package
786 func externalPackage() {
787 b.Wrapf("%s", 1) // want "Wrapf format %s has arg 1 of wrong type int"
788 b.Wrap("%s", 1) // want "Wrap call has possible formatting directive %s"
790 b.Wrapf2("%s", 1) // want "Wrapf2 format %s has arg 1 of wrong type int"
793 func PointerVerbs() {
794 // Use booleans, so that we don't just format the elements like in
795 // PointersToCompoundTypes. Bools can only be formatted with verbs like
796 // %t and %v, and none of the ones below.
800 map_ := map[bool]bool{}
801 chan_ := make(chan bool)
802 func_ := func(bool) {}
804 // %p, %b, %d, %o, %O, %x, and %X all support pointers.
805 fmt.Printf("%p", ptr)
806 fmt.Printf("%b", ptr)
807 fmt.Printf("%d", ptr)
808 fmt.Printf("%o", ptr)
809 fmt.Printf("%O", ptr)
810 fmt.Printf("%x", ptr)
811 fmt.Printf("%X", ptr)
813 // %p, %b, %d, %o, %O, %x, and %X all support channels.
814 fmt.Printf("%p", chan_)
815 fmt.Printf("%b", chan_)
816 fmt.Printf("%d", chan_)
817 fmt.Printf("%o", chan_)
818 fmt.Printf("%O", chan_)
819 fmt.Printf("%x", chan_)
820 fmt.Printf("%X", chan_)
822 // %p is the only one that supports funcs.
823 fmt.Printf("%p", func_)
824 fmt.Printf("%b", func_) // want `Printf format %b arg func_ is a func value, not called`
825 fmt.Printf("%d", func_) // want `Printf format %d arg func_ is a func value, not called`
826 fmt.Printf("%o", func_) // want `Printf format %o arg func_ is a func value, not called`
827 fmt.Printf("%O", func_) // want `Printf format %O arg func_ is a func value, not called`
828 fmt.Printf("%x", func_) // want `Printf format %x arg func_ is a func value, not called`
829 fmt.Printf("%X", func_) // want `Printf format %X arg func_ is a func value, not called`
831 // %p is the only one that supports all slices, by printing the address
832 // of the 0th element.
833 fmt.Printf("%p", slice) // supported; address of 0th element
834 fmt.Printf("%b", slice) // want `Printf format %b has arg slice of wrong type \[\]bool`
836 fmt.Printf("%d", slice) // want `Printf format %d has arg slice of wrong type \[\]bool`
838 fmt.Printf("%o", slice) // want `Printf format %o has arg slice of wrong type \[\]bool`
839 fmt.Printf("%O", slice) // want `Printf format %O has arg slice of wrong type \[\]bool`
841 fmt.Printf("%x", slice) // want `Printf format %x has arg slice of wrong type \[\]bool`
842 fmt.Printf("%X", slice) // want `Printf format %X has arg slice of wrong type \[\]bool`
844 // None support arrays.
845 fmt.Printf("%p", array) // want `Printf format %p has arg array of wrong type \[3\]bool`
846 fmt.Printf("%b", array) // want `Printf format %b has arg array of wrong type \[3\]bool`
847 fmt.Printf("%d", array) // want `Printf format %d has arg array of wrong type \[3\]bool`
848 fmt.Printf("%o", array) // want `Printf format %o has arg array of wrong type \[3\]bool`
849 fmt.Printf("%O", array) // want `Printf format %O has arg array of wrong type \[3\]bool`
850 fmt.Printf("%x", array) // want `Printf format %x has arg array of wrong type \[3\]bool`
851 fmt.Printf("%X", array) // want `Printf format %X has arg array of wrong type \[3\]bool`
853 // %p is the only one that supports all maps.
854 fmt.Printf("%p", map_) // supported; address of 0th element
855 fmt.Printf("%b", map_) // want `Printf format %b has arg map_ of wrong type map\[bool\]bool`
857 fmt.Printf("%d", map_) // want `Printf format %d has arg map_ of wrong type map\[bool\]bool`
859 fmt.Printf("%o", map_) // want `Printf format %o has arg map_ of wrong type map\[bool\]bool`
860 fmt.Printf("%O", map_) // want `Printf format %O has arg map_ of wrong type map\[bool\]bool`
862 fmt.Printf("%x", map_) // want `Printf format %x has arg map_ of wrong type map\[bool\]bool`
863 fmt.Printf("%X", map_) // want `Printf format %X has arg map_ of wrong type map\[bool\]bool`