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
16 // For testing printf-like functions from external package.
17 // "github.com/foobar/externalprintf"
20 func UnsafePointerPrintfTest() {
22 fmt.Printf("%p, %x %X", up, up, up)
25 // Error methods that do not satisfy the Error interface and should be checked.
28 func (errorTest1) Error(...interface{}) string {
32 type errorTest2 int // Analogous to testing's *T type.
33 func (errorTest2) Error(...interface{}) {
38 func (errorTest3) Error() { // No return value.
43 func (errorTest4) Error() int { // Different return type.
49 func (errorTest5) error() { // niladic; don't complain if no args (was bug)
52 // This function never executes, but it serves as a simple test for the program.
53 // Test with make test.
64 // Some good format/argtypes
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)
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.
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"
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", ¬PercentDV) // ERROR "Printf format %d has arg ¬PercentDV of wrong type \*.*print.notPercentDStruct"
190 Printf("%p", ¬PercentDV) // 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.
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.
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.
215 et1.Error("hi") // ok
216 et1.Error("%d", 3) // ERROR "Error call has possible Printf formatting directive %d"
218 et3.Error() // ok, not an error method.
220 et4.Error() // ok, not an error method.
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
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.
242 t.Log("%d", 3) // ERROR "Log call has possible Printf formatting directive %d"
244 t.Logf("%d", "hi") // ERROR "Logf format %d has arg \x22hi\x22 of wrong type string"
246 Errorf(1, "%d", 3) // OK
247 Errorf(1, "%d", "hi") // ERROR "Errorf format %d has arg \x22hi\x22 of wrong type string"
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"
254 // Printf from external package
255 // externalprintf.Printf("%d", 42) // OK
256 // externalprintf.Printf("foobar") // OK
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
265 // user-defined Println-like functions
267 ss.Log(someFunction, "foo") // OK
268 ss.Error(someFunction, someFunction) // 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"
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
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"
290 Printf(someString(), "hello") // OK
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"
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"
316 dbg("", 1) // no error "call has arguments but no formatting directive"
319 func someString() string { return "X" }
321 type someStruct struct{}
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.
326 func (ss *someStruct) Log(f func(), s string) {}
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{}.
331 func (ss *someStruct) Error(args ...func()) {}
333 // Println is variadic user-defined Println-like function.
334 // Calls to this func must be checked for Println-like arguments.
335 func (ss *someStruct) Println(args ...interface{}) {}
337 // log is variadic user-defined Println-like function.
338 // Calls to this func must be checked for Println-like arguments.
339 func (ss *someStruct) log(f func(), args ...interface{}) {}
341 // A function we use as a function value; it has no other purpose.
342 func someFunction() {}
344 // Printf is used by the test so we must declare it.
345 func Printf(format string, args ...interface{}) {
346 fmt.Printf(format, args...)
349 // Println is used by the test so we must declare it.
350 func Println(args ...interface{}) {
354 // printf is used by the test so we must declare it.
355 func printf(format string, args ...interface{}) {
356 fmt.Printf(format, args...)
359 // Errorf is used by the test for a case in which the first parameter
360 // is not a format string.
361 func Errorf(i int, format string, args ...interface{}) {
362 _ = fmt.Errorf(format, args...)
365 // errorf is used by the test for a case in which the function accepts multiple
366 // string parameters before variadic arguments
367 func errorf(level, format string, args ...interface{}) {
368 _ = fmt.Errorf(format, args...)
371 // multi is used by the test.
372 func multi() []interface{} {
373 panic("don't call - testing only")
378 func (stringer) String() string { return "string" }
380 type ptrStringer float64
382 var stringerv ptrStringer
384 func (*ptrStringer) String() string {
388 func (p *ptrStringer) Warn2(x int, args ...interface{}) string {
389 return p.Warn(x, args...)
392 func (p *ptrStringer) Warnf2(x int, format string, args ...interface{}) string {
393 return p.Warnf(x, format, args...)
396 func (*ptrStringer) Warn(x int, args ...interface{}) string {
400 func (*ptrStringer) Warnf(x int, format string, args ...interface{}) string {
404 func (p *ptrStringer) Wrap2(x int, args ...interface{}) string {
405 return p.Wrap(x, args...)
408 func (p *ptrStringer) Wrapf2(x int, format string, args ...interface{}) string {
409 return p.Wrapf(x, format, args...)
412 func (*ptrStringer) Wrap(x int, args ...interface{}) string {
413 return fmt.Sprint(args...)
416 func (*ptrStringer) Wrapf(x int, format string, args ...interface{}) string {
417 return fmt.Sprintf(format, args...)
420 func (*ptrStringer) BadWrap(x int, args ...interface{}) string {
421 return fmt.Sprint(args) // ERROR "missing ... in args forwarded to print-like function"
424 func (*ptrStringer) BadWrapf(x int, format string, args ...interface{}) string {
425 return fmt.Sprintf(format, args) // ERROR "missing ... in args forwarded to printf-like function"
428 func (*ptrStringer) WrapfFalsePositive(x int, arg1 string, arg2 ...interface{}) string {
429 return fmt.Sprintf("%s %v", arg1, arg2)
432 type embeddedStringer struct {
438 var embeddedStringerv embeddedStringer
440 type notstringer struct {
444 var notstringerv notstringer
446 type stringerarray [4]float64
448 func (stringerarray) String() string {
452 var stringerarrayv stringerarray
454 type notstringerarray [4]float64
456 var notstringerarrayv notstringerarray
458 var nonemptyinterface = interface {
462 // A data type we can print with "%d".
463 type percentDStruct struct {
469 var percentDV percentDStruct
471 // A data type we cannot print correctly with "%d".
472 type notPercentDStruct struct {
478 var notPercentDV notPercentDStruct
480 // A data type we can print with "%s".
481 type percentSStruct struct {
487 var percentSV percentSStruct
489 type recursiveStringer int
491 func (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"
500 type recursivePtrStringer int
502 func (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"
508 type BoolFormatter bool
510 func (*BoolFormatter) Format(fmt.State, rune) {
513 // Formatter with value receiver
514 type FormatterVal bool
516 func (FormatterVal) Format(fmt.State, rune) {
519 type RecursiveSlice []RecursiveSlice
521 var recursiveSliceV = &RecursiveSlice{}
523 type RecursiveMap map[int]RecursiveMap
525 var recursiveMapV = make(RecursiveMap)
527 type RecursiveStruct struct {
528 next *RecursiveStruct
531 var recursiveStructV = &RecursiveStruct{}
533 type RecursiveStruct1 struct {
534 next *RecursiveStruct2
537 type RecursiveStruct2 struct {
538 next *RecursiveStruct1
541 var recursiveStruct1V = &RecursiveStruct1{}
543 type unexportedInterface struct {
547 // Issue 17798: unexported ptrStringer cannot be formatted.
548 type unexportedStringer struct {
551 type unexportedStringerOtherFields struct {
557 // Issue 17798: unexported error cannot be formatted.
558 type unexportedError struct {
561 type unexportedErrorOtherFields struct {
567 type errorer struct{}
569 func (e errorer) Error() string { return "errorer" }
571 type unexportedCustomError struct {
575 type errorInterface interface {
580 type unexportedErrorInterface struct {
584 func UnexportedStringerOrError() {
585 fmt.Printf("%s", unexportedInterface{"foo"}) // ok; prints {foo}
586 fmt.Printf("%s", unexportedInterface{3}) // ok; we can't see the problem
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"
592 usf := unexportedStringerOtherFields{
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"
599 ue := unexportedError{
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"
605 uef := unexportedErrorOtherFields{
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"
613 uce := unexportedCustomError{
616 fmt.Printf("%s", uce) // ERROR "Printf format %s has arg uce of wrong type .*print.unexportedCustomError"
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
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
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
634 // TODO: Disable complaint about '0' for Go 1.10. To be fixed properly in 1.11.
635 // See issues 23598 and 23605.
636 func DisableErrorForFlag0() {
637 fmt.Printf("%0t", true)
641 func dbg(format string, args ...interface{}) {
645 fmt.Printf(format, args...)
648 func PointersToCompoundTypes() {
649 stringSlice := []string{"a", "b"}
650 fmt.Printf("%s", &stringSlice) // not an error
652 intSlice := []int{3, 4}
653 fmt.Printf("%s", &intSlice) // ERROR "Printf format %s has arg &intSlice of wrong type \*\[\]int"
655 stringArray := [2]string{"a", "b"}
656 fmt.Printf("%s", &stringArray) // not an error
658 intArray := [2]int{3, 4}
659 fmt.Printf("%s", &intArray) // ERROR "Printf format %s has arg &intArray of wrong type \*\[2\]int"
661 stringStruct := struct{ F string }{"foo"}
662 fmt.Printf("%s", &stringStruct) // not an error
664 intStruct := struct{ F int }{3}
665 fmt.Printf("%s", &intStruct) // ERROR "Printf format %s has arg &intStruct of wrong type \*struct{F int}"
667 stringMap := map[string]string{"foo": "bar"}
668 fmt.Printf("%s", &stringMap) // not an error
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"
679 fmt.Printf("%s\n", T1{&T2{"x"}}) // ERROR "Printf format %s has arg T1{&T2{.x.}} of wrong type .*print\.T1"