Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.0.0-20201105173854-bc9fc8d8c4bc / go / analysis / passes / printf / testdata / src / a / a.go
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
7 package a
8
9 import (
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
17         // For testing printf-like functions from external package.
18         // "github.com/foobar/externalprintf"
19         "b"
20 )
21
22 func UnsafePointerPrintfTest() {
23         var up unsafe.Pointer
24         fmt.Printf("%p, %x %X", up, up, up)
25 }
26
27 // Error methods that do not satisfy the Error interface and should be checked.
28 type errorTest1 int
29
30 func (errorTest1) Error(...interface{}) string {
31         return "hi"
32 }
33
34 type errorTest2 int // Analogous to testing's *T type.
35 func (errorTest2) Error(...interface{}) {
36 }
37
38 type errorTest3 int
39
40 func (errorTest3) Error() { // No return value.
41 }
42
43 type errorTest4 int
44
45 func (errorTest4) Error() int { // Different return type.
46         return 3
47 }
48
49 type errorTest5 int
50
51 func (errorTest5) error() { // niladic; don't complain if no args (was bug)
52 }
53
54 // This function never executes, but it serves as a simple test for the program.
55 // Test with make test.
56 func PrintfTests() {
57         var b bool
58         var i int
59         var r rune
60         var s string
61         var x float64
62         var p *int
63         var imap map[int]int
64         var fslice []float64
65         var c complex64
66         var err error
67         // Some good format/argtypes
68         fmt.Printf("")
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)
81         fmt.Printf("%p", p)
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.
108
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"
169         Printf("hi")                       // ok
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", &notPercentDV) // want `Printf format %d has arg &notPercentDV of wrong type \*a.notPercentDStruct`
197         Printf("%p", &notPercentDV) // 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.
202         Printf("%[1]d", 3)
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.
216         var e error
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.
220         var et1 *testing.T
221         et1.Error()         // ok
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"
225         var et3 errorTest3
226         et3.Error() // ok, not an error method.
227         var et4 errorTest4
228         et4.Error() // ok, not an error method.
229         var et5 errorTest5
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
234         }
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.
248         math.Log(3) // OK
249         var t *testing.T
250         t.Log("%d", 3) // want "Log call has possible formatting directive %d"
251         t.Logf("%d", 3)
252         t.Logf("%d", "hi") // want `Logf format %d has arg "hi" of wrong type string`
253
254         Errorf(1, "%d", 3)    // OK
255         Errorf(1, "%d", "hi") // want `Errorf format %d has arg "hi" of wrong type string`
256
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"
261
262         var tb testing.TB
263         tb.Errorf("%s", 1) // want "Errorf format %s has arg 1 of wrong type int"
264
265         // Printf from external package
266         // externalprintf.Printf("%d", 42) // OK
267         // externalprintf.Printf("foobar") // OK
268         // level := 123
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
275
276         // user-defined Println-like functions
277         ss := &someStruct{}
278         ss.Log(someFunction, "foo")          // OK
279         ss.Error(someFunction, someFunction) // OK
280         ss.Println()                         // 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"
286
287         // indexed arguments
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
296
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"
300
301         Printf(someString(), "hello") // OK
302
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"
313
314         // Methods too.
315         var l *logpkg.Logger
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"
325
326         // Issue 26486
327         dbg("", 1) // no error "call has arguments but no formatting directive"
328
329         // %w
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`
337         Errorf(0, "%w", err)
338 }
339
340 func someString() string { return "X" }
341
342 type someStruct struct{}
343
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) {}
348
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()) {}
353
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{}) {}
357
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{}) {}
361
362 // A function we use as a function value; it has no other purpose.
363 func someFunction() {}
364
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...)
368 }
369
370 // Println is used by the test so we must declare it.
371 func Println(args ...interface{}) { // want Println:"printWrapper"
372         fmt.Println(args...)
373 }
374
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...)
378 }
379
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...)
384 }
385
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...)
390 }
391
392 // multi is used by the test.
393 func multi() []interface{} {
394         panic("don't call - testing only")
395 }
396
397 type stringer int
398
399 func (stringer) String() string { return "string" }
400
401 type ptrStringer float64
402
403 var stringerv ptrStringer
404
405 func (*ptrStringer) String() string {
406         return "string"
407 }
408
409 func (p *ptrStringer) Warn2(x int, args ...interface{}) string { // want Warn2:"printWrapper"
410         return p.Warn(x, args...)
411 }
412
413 func (p *ptrStringer) Warnf2(x int, format string, args ...interface{}) string { // want Warnf2:"printfWrapper"
414         return p.Warnf(x, format, args...)
415 }
416
417 // During testing -printf.funcs flag matches Warn.
418 func (*ptrStringer) Warn(x int, args ...interface{}) string {
419         return "warn"
420 }
421
422 // During testing -printf.funcs flag matches Warnf.
423 func (*ptrStringer) Warnf(x int, format string, args ...interface{}) string {
424         return "warnf"
425 }
426
427 func (p *ptrStringer) Wrap2(x int, args ...interface{}) string { // want Wrap2:"printWrapper"
428         return p.Wrap(x, args...)
429 }
430
431 func (p *ptrStringer) Wrapf2(x int, format string, args ...interface{}) string { // want Wrapf2:"printfWrapper"
432         return p.Wrapf(x, format, args...)
433 }
434
435 func (*ptrStringer) Wrap(x int, args ...interface{}) string { // want Wrap:"printWrapper"
436         return fmt.Sprint(args...)
437 }
438
439 func (*ptrStringer) Wrapf(x int, format string, args ...interface{}) string { // want Wrapf:"printfWrapper"
440         return fmt.Sprintf(format, args...)
441 }
442
443 func (*ptrStringer) BadWrap(x int, args ...interface{}) string {
444         return fmt.Sprint(args) // want "missing ... in args forwarded to print-like function"
445 }
446
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"
449 }
450
451 func (*ptrStringer) WrapfFalsePositive(x int, arg1 string, arg2 ...interface{}) string {
452         return fmt.Sprintf("%s %v", arg1, arg2)
453 }
454
455 type embeddedStringer struct {
456         foo string
457         ptrStringer
458         bar int
459 }
460
461 var embeddedStringerv embeddedStringer
462
463 type notstringer struct {
464         f float64
465 }
466
467 var notstringerv notstringer
468
469 type stringerarray [4]float64
470
471 func (stringerarray) String() string {
472         return "string"
473 }
474
475 var stringerarrayv stringerarray
476
477 type notstringerarray [4]float64
478
479 var notstringerarrayv notstringerarray
480
481 var nonemptyinterface = interface {
482         f()
483 }(nil)
484
485 // A data type we can print with "%d".
486 type percentDStruct struct {
487         a int
488         b []byte
489         c *float64
490 }
491
492 var percentDV percentDStruct
493
494 // A data type we cannot print correctly with "%d".
495 type notPercentDStruct struct {
496         a int
497         b []byte
498         c bool
499 }
500
501 var notPercentDV notPercentDStruct
502
503 // A data type we can print with "%s".
504 type percentSStruct struct {
505         a string
506         b []byte
507         C stringerarray
508 }
509
510 var percentSV percentSStruct
511
512 type recursiveStringer int
513
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"
521 }
522
523 type recursivePtrStringer int
524
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"
529 }
530
531 type recursiveError int
532
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"
540 }
541
542 type recursivePtrError int
543
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"
548 }
549
550 type recursiveStringerAndError int
551
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"
559 }
560
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"
568 }
569
570 type recursivePtrStringerAndError int
571
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"
576 }
577
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"
582 }
583
584 // implements a String() method but with non-matching return types
585 type nonStringerWrongReturn int
586
587 func (s nonStringerWrongReturn) String() (string, error) {
588         return "", fmt.Errorf("%v", s)
589 }
590
591 // implements a String() method but with non-matching arguments
592 type nonStringerWrongArgs int
593
594 func (s nonStringerWrongArgs) String(i int) string {
595         return fmt.Sprintf("%d%v", i, s)
596 }
597
598 type cons struct {
599         car int
600         cdr *cons
601 }
602
603 func (cons *cons) String() string {
604         if cons == nil {
605                 return "nil"
606         }
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
609 }
610
611 type BoolFormatter bool
612
613 func (*BoolFormatter) Format(fmt.State, rune) {
614 }
615
616 // Formatter with value receiver
617 type FormatterVal bool
618
619 func (FormatterVal) Format(fmt.State, rune) {
620 }
621
622 type RecursiveSlice []RecursiveSlice
623
624 var recursiveSliceV = &RecursiveSlice{}
625
626 type RecursiveMap map[int]RecursiveMap
627
628 var recursiveMapV = make(RecursiveMap)
629
630 type RecursiveStruct struct {
631         next *RecursiveStruct
632 }
633
634 var recursiveStructV = &RecursiveStruct{}
635
636 type RecursiveStruct1 struct {
637         next *RecursiveStruct2
638 }
639
640 type RecursiveStruct2 struct {
641         next *RecursiveStruct1
642 }
643
644 var recursiveStruct1V = &RecursiveStruct1{}
645
646 type unexportedInterface struct {
647         f interface{}
648 }
649
650 // Issue 17798: unexported ptrStringer cannot be formatted.
651 type unexportedStringer struct {
652         t ptrStringer
653 }
654 type unexportedStringerOtherFields struct {
655         s string
656         t ptrStringer
657         S string
658 }
659
660 // Issue 17798: unexported error cannot be formatted.
661 type unexportedError struct {
662         e error
663 }
664 type unexportedErrorOtherFields struct {
665         s string
666         e error
667         S string
668 }
669
670 type errorer struct{}
671
672 func (e errorer) Error() string { return "errorer" }
673
674 type unexportedCustomError struct {
675         e errorer
676 }
677
678 type errorInterface interface {
679         error
680         ExtraMethod()
681 }
682
683 type unexportedErrorInterface struct {
684         e errorInterface
685 }
686
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
690
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"
694
695         usf := unexportedStringerOtherFields{
696                 s: "foo",
697                 S: "bar",
698         }
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"
701
702         ue := unexportedError{
703                 e: &errorer{},
704         }
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"
707
708         uef := unexportedErrorOtherFields{
709                 s: "foo",
710                 e: &errorer{},
711                 S: "bar",
712         }
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"
715
716         uce := unexportedCustomError{
717                 e: errorer{},
718         }
719         fmt.Printf("%s", uce) // want "Printf format %s has arg uce of wrong type a.unexportedCustomError"
720
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
724
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
728
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
735 }
736
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)
741 }
742
743 // Issue 26486.
744 func dbg(format string, args ...interface{}) {
745         if format == "" {
746                 format = "%v"
747         }
748         fmt.Printf(format, args...)
749 }
750
751 func PointersToCompoundTypes() {
752         stringSlice := []string{"a", "b"}
753         fmt.Printf("%s", &stringSlice) // not an error
754
755         intSlice := []int{3, 4}
756         fmt.Printf("%s", &intSlice) // want `Printf format %s has arg &intSlice of wrong type \*\[\]int`
757
758         stringArray := [2]string{"a", "b"}
759         fmt.Printf("%s", &stringArray) // not an error
760
761         intArray := [2]int{3, 4}
762         fmt.Printf("%s", &intArray) // want `Printf format %s has arg &intArray of wrong type \*\[2\]int`
763
764         stringStruct := struct{ F string }{"foo"}
765         fmt.Printf("%s", &stringStruct) // not an error
766
767         intStruct := struct{ F int }{3}
768         fmt.Printf("%s", &intStruct) // want `Printf format %s has arg &intStruct of wrong type \*struct{F int}`
769
770         stringMap := map[string]string{"foo": "bar"}
771         fmt.Printf("%s", &stringMap) // not an error
772
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`
775
776         type T2 struct {
777                 X string
778         }
779         type T1 struct {
780                 X *T2
781         }
782         fmt.Printf("%s\n", T1{&T2{"x"}}) // want `Printf format %s has arg T1{&T2{.x.}} of wrong type a\.T1`
783 }
784
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"
789         b.NoWrap("%s", 1)
790         b.Wrapf2("%s", 1) // want "Wrapf2 format %s has arg 1 of wrong type int"
791 }
792
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.
797         ptr := new(bool)
798         slice := []bool{}
799         array := [3]bool{}
800         map_ := map[bool]bool{}
801         chan_ := make(chan bool)
802         func_ := func(bool) {}
803
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)
812
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_)
821
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`
830
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`
835
836         fmt.Printf("%d", slice) // want `Printf format %d has arg slice of wrong type \[\]bool`
837
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`
840
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`
843
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`
852
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`
856
857         fmt.Printf("%d", map_) // want `Printf format %d has arg map_ of wrong type map\[bool\]bool`
858
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`
861
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`
864 }