Debian Codegraph @

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

raw symbols SCIP
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 type errorTestOK int
55
56 func (errorTestOK) Error() string { return "" }
57
58 // This function never executes, but it serves as a simple test for the program.
59 // Test with make test.
60 func PrintfTests() {
61 var b bool
62 var i int
63 var r rune
64 var s string
65 var x float64
66 var p *int
67 var imap map[int]int
68 var fslice []float64
69 var c complex64
70 var err error
71 // Some good format/argtypes
72 fmt.Printf("")
73 fmt.Printf("%b %b %b", 3, i, x)
74 fmt.Printf("%c %c %c %c", 3, i, 'x', r)
75 fmt.Printf("%d %d %d", 3, i, imap)
76 fmt.Printf("%e %e %e %e", 3e9, x, fslice, c)
77 fmt.Printf("%E %E %E %E", 3e9, x, fslice, c)
78 fmt.Printf("%f %f %f %f", 3e9, x, fslice, c)
79 fmt.Printf("%F %F %F %F", 3e9, x, fslice, c)
80 fmt.Printf("%g %g %g %g", 3e9, x, fslice, c)
81 fmt.Printf("%G %G %G %G", 3e9, x, fslice, c)
82 fmt.Printf("%b %b %b %b", 3e9, x, fslice, c)
83 fmt.Printf("%o %o", 3, i)
84 fmt.Printf("%O %O", 3, i)
85 fmt.Printf("%p", p)
86 fmt.Printf("%q %q %q", byte(i), 'x', r)
87 fmt.Printf("%s %s %s", "hi", s, []byte{65})
88 fmt.Printf("%t %t", true, b)
89 fmt.Printf("%T %T", 3, i)
90 fmt.Printf("%U %U", 3, i)
91 fmt.Printf("%v %v", 3, i)
92 fmt.Printf("%x %x %x %x %x %x %x", 3, i, "hi", s, x, c, fslice)
93 fmt.Printf("%X %X %X %X %X %X %X", 3, i, "hi", s, x, c, fslice)
94 fmt.Printf("%.*s %d %g", 3, "hi", 23, 2.3)
95 fmt.Printf("%s", &stringerv)
96 fmt.Printf("%v", &stringerv)
97 fmt.Printf("%T", &stringerv)
98 fmt.Printf("%s", &embeddedStringerv)
99 fmt.Printf("%v", &embeddedStringerv)
100 fmt.Printf("%T", &embeddedStringerv)
101 fmt.Printf("%v", notstringerv)
102 fmt.Printf("%T", notstringerv)
103 fmt.Printf("%q", stringerarrayv)
104 fmt.Printf("%v", stringerarrayv)
105 fmt.Printf("%s", stringerarrayv)
106 fmt.Printf("%v", notstringerarrayv)
107 fmt.Printf("%T", notstringerarrayv)
108 fmt.Printf("%d", new(fmt.Formatter))
109 fmt.Printf("%*%", 2) // Ridiculous but allowed.
110 fmt.Printf("%s", interface{}(nil)) // Nothing useful we can say.
111 fmt.Printf("%a", interface{}(new(BoolFormatter))) // Could be a fmt.Formatter.
112
113 fmt.Printf("%g", 1+2i)
114 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
115 // Some bad format/argTypes
116 fmt.Printf("%b", "hi") // want "fmt.Printf format %b has arg \x22hi\x22 of wrong type string"
117 fmt.Printf("%t", c) // want "fmt.Printf format %t has arg c of wrong type complex64"
118 fmt.Printf("%t", 1+2i) // want `fmt.Printf format %t has arg 1 \+ 2i of wrong type complex128`
119 fmt.Printf("%c", 2.3) // want "fmt.Printf format %c has arg 2.3 of wrong type float64"
120 fmt.Printf("%d", 2.3) // want "fmt.Printf format %d has arg 2.3 of wrong type float64"
121 fmt.Printf("%e", "hi") // want `fmt.Printf format %e has arg "hi" of wrong type string`
122 fmt.Printf("%E", true) // want "fmt.Printf format %E has arg true of wrong type bool"
123 fmt.Printf("%f", "hi") // want "fmt.Printf format %f has arg \x22hi\x22 of wrong type string"
124 fmt.Printf("%F", 'x') // want "fmt.Printf format %F has arg 'x' of wrong type rune"
125 fmt.Printf("%g", "hi") // want `fmt.Printf format %g has arg "hi" of wrong type string`
126 fmt.Printf("%g", imap) // want `fmt.Printf format %g has arg imap of wrong type map\[int\]int`
127 fmt.Printf("%G", i) // want "fmt.Printf format %G has arg i of wrong type int"
128 fmt.Printf("%o", x) // want "fmt.Printf format %o has arg x of wrong type float64"
129 fmt.Printf("%O", x) // want "fmt.Printf format %O has arg x of wrong type float64"
130 fmt.Printf("%p", nil) // want "fmt.Printf format %p has arg nil of wrong type untyped nil"
131 fmt.Printf("%p", 23) // want "fmt.Printf format %p has arg 23 of wrong type int"
132 fmt.Printf("%q", x) // want "fmt.Printf format %q has arg x of wrong type float64"
133 fmt.Printf("%s", b) // want "fmt.Printf format %s has arg b of wrong type bool"
134 fmt.Printf("%s", byte(65)) // want `fmt.Printf format %s has arg byte\(65\) of wrong type byte`
135 fmt.Printf("%t", 23) // want "fmt.Printf format %t has arg 23 of wrong type int"
136 fmt.Printf("%U", x) // want "fmt.Printf format %U has arg x of wrong type float64"
137 fmt.Printf("%x", nil) // want "fmt.Printf format %x has arg nil of wrong type untyped nil"
138 fmt.Printf("%s", stringerv) // want "fmt.Printf format %s has arg stringerv of wrong type a.ptrStringer"
139 fmt.Printf("%t", stringerv) // want "fmt.Printf format %t has arg stringerv of wrong type a.ptrStringer"
140 fmt.Printf("%s", embeddedStringerv) // want "fmt.Printf format %s has arg embeddedStringerv of wrong type a.embeddedStringer"
141 fmt.Printf("%t", embeddedStringerv) // want "fmt.Printf format %t has arg embeddedStringerv of wrong type a.embeddedStringer"
142 fmt.Printf("%q", notstringerv) // want "fmt.Printf format %q has arg notstringerv of wrong type a.notstringer"
143 fmt.Printf("%t", notstringerv) // want "fmt.Printf format %t has arg notstringerv of wrong type a.notstringer"
144 fmt.Printf("%t", stringerarrayv) // want "fmt.Printf format %t has arg stringerarrayv of wrong type a.stringerarray"
145 fmt.Printf("%t", notstringerarrayv) // want "fmt.Printf format %t has arg notstringerarrayv of wrong type a.notstringerarray"
146 fmt.Printf("%q", notstringerarrayv) // want "fmt.Printf format %q has arg notstringerarrayv of wrong type a.notstringerarray"
147 fmt.Printf("%d", BoolFormatter(true)) // want `fmt.Printf format %d has arg BoolFormatter\(true\) of wrong type a.BoolFormatter`
148 fmt.Printf("%z", FormatterVal(true)) // correct (the type is responsible for formatting)
149 fmt.Printf("%d", FormatterVal(true)) // correct (the type is responsible for formatting)
150 fmt.Printf("%s", nonemptyinterface) // correct (the type is responsible for formatting)
151 fmt.Printf("%.*s %d %6g", 3, "hi", 23, 'x') // want "fmt.Printf format %6g has arg 'x' of wrong type rune"
152 fmt.Println() // not an error
153 fmt.Println("%s", "hi") // want "fmt.Println call has possible Printf formatting directive %s"
154 fmt.Println("%v", "hi") // want "fmt.Println call has possible Printf formatting directive %v"
155 fmt.Println("%T", "hi") // want "fmt.Println call has possible Printf formatting directive %T"
156 fmt.Println("%s"+" there", "hi") // want "fmt.Println call has possible Printf formatting directive %s"
157 fmt.Println("http://foo.com?q%2Fabc") // no diagnostic: %XX is excepted
158 fmt.Println("http://foo.com?q%2Fabc-%s") // want"fmt.Println call has possible Printf formatting directive %s"
159 fmt.Println("0.0%") // correct (trailing % couldn't be a formatting directive)
160 fmt.Printf("%s", "hi", 3) // want "fmt.Printf call needs 1 arg but has 2 args"
161 _ = fmt.Sprintf("%"+("s"), "hi", 3) // want "fmt.Sprintf call needs 1 arg but has 2 args"
162 fmt.Printf("%s%%%d", "hi", 3) // correct
163 fmt.Printf("%08s", "woo") // correct
164 fmt.Printf("% 8s", "woo") // correct
165 fmt.Printf("%.*d", 3, 3) // correct
166 fmt.Printf("%.*d x", 3, 3, 3, 3) // want "fmt.Printf call needs 2 args but has 4 args"
167 fmt.Printf("%.*d x", "hi", 3) // want `fmt.Printf format %.*d uses non-int "hi" as argument of \*`
168 fmt.Printf("%.*d x", i, 3) // correct
169 fmt.Printf("%.*d x", s, 3) // want `fmt.Printf format %.\*d uses non-int s as argument of \*`
170 fmt.Printf("%*% x", 0.22) // want `fmt.Printf format %\*% uses non-int 0.22 as argument of \*`
171 fmt.Printf("%q %q", multi()...) // ok
172 fmt.Printf("%#q", `blah`) // ok
173 fmt.Printf("%#b", 3) // ok
174 fmt.Printf("%q", 3) // ok before go1.26 (#72850)
175
176 // printf("now is the time", "buddy") // no error "a.printf call has arguments but no formatting directives"
177 Printf("now is the time", "buddy") // want "a.Printf call has arguments but no formatting directives"
178 Printf("hi") // ok
179 const format = "%s %s\n"
180 Printf(format, "hi", "there")
181 Printf(format, "hi") // want "a.Printf format %s reads arg #2, but call has 1 arg$"
182 Printf("%s %d %.3v %q", "str", 4) // want "a.Printf format %.3v reads arg #3, but call has 2 args"
183 f := new(ptrStringer)
184 f.Warn(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Warn call has possible Printf formatting directive %s`
185 f.Warnf(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Warnf call needs 1 arg but has 2 args`
186 f.Warnf(0, "%r", "hello") // want `\(\*a.ptrStringer\).Warnf format %r has unknown verb r`
187 f.Warnf(0, "%#s", "hello") // want `\(\*a.ptrStringer\).Warnf format %#s has unrecognized flag #`
188 f.Warn2(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Warn2 call has possible Printf formatting directive %s`
189 f.Warnf2(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Warnf2 call needs 1 arg but has 2 args`
190 f.Warnf2(0, "%r", "hello") // want `\(\*a.ptrStringer\).Warnf2 format %r has unknown verb r`
191 f.Warnf2(0, "%#s", "hello") // want `\(\*a.ptrStringer\).Warnf2 format %#s has unrecognized flag #`
192 f.Wrap(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Wrap call has possible Printf formatting directive %s`
193 f.Wrapf(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Wrapf call needs 1 arg but has 2 args`
194 f.Wrapf(0, "%r", "hello") // want `\(\*a.ptrStringer\).Wrapf format %r has unknown verb r`
195 f.Wrapf(0, "%#s", "hello") // want `\(\*a.ptrStringer\).Wrapf format %#s has unrecognized flag #`
196 f.Wrap2(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Wrap2 call has possible Printf formatting directive %s`
197 f.Wrapf2(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Wrapf2 call needs 1 arg but has 2 args`
198 f.Wrapf2(0, "%r", "hello") // want `\(\*a.ptrStringer\).Wrapf2 format %r has unknown verb r`
199 f.Wrapf2(0, "%#s", "hello") // want `\(\*a.ptrStringer\).Wrapf2 format %#s has unrecognized flag #`
200 fmt.Printf("%#s", FormatterVal(true)) // correct (the type is responsible for formatting)
201 Printf("d%", 2) // want "a.Printf format % is missing verb at end of string"
202 Printf("%d", percentDV)
203 Printf("%d", &percentDV)
204 Printf("%d", notPercentDV) // want "a.Printf format %d has arg notPercentDV of wrong type a.notPercentDStruct"
205 Printf("%d", &notPercentDV) // want `a.Printf format %d has arg &notPercentDV of wrong type \*a.notPercentDStruct`
206 Printf("%p", &notPercentDV) // Works regardless: we print it as a pointer.
207 Printf("%q", &percentDV) // want `a.Printf format %q has arg &percentDV of wrong type \*a.percentDStruct`
208 Printf("%s", percentSV)
209 Printf("%s", &percentSV)
210 // Good argument reorderings.
211 Printf("%[1]d", 3)
212 Printf("%[1]*d", 3, 1)
213 Printf("%[2]*[1]d", 1, 3)
214 Printf("%[2]*.[1]*[3]d", 2, 3, 4)
215 fmt.Fprintf(os.Stderr, "%[2]*.[1]*[3]d", 2, 3, 4) // Use Fprintf to make sure we count arguments correctly.
216 // Bad argument reorderings.
217 Printf("%[xd", 3) // want `a.Printf format %\[xd is missing closing \]`
218 Printf("%[x]d x", 3) // want `a.Printf format has invalid argument index \[x\]`
219 Printf("%[3]*s x", "hi", 2) // want `a.Printf format %\[3]\*s reads arg #3, but call has 2 args`
220 _ = fmt.Sprintf("%[3]d x", 2) // want `fmt.Sprintf format %\[3]d reads arg #3, but call has 1 arg`
221 Printf("%[2]*.[1]*[3]d x", 2, "hi", 4) // want `a.Printf format %\[2]\*\.\[1\]\*\[3\]d uses non-int \x22hi\x22 as argument of \*`
222 Printf("%[0]s x", "arg1") // want `a.Printf format has invalid argument index \[0\]`
223 Printf("%[0]d x", 1) // want `a.Printf format has invalid argument index \[0\]`
224 Printf("%[3]*.[2*[1]f", 1, 2, 3) // want `a.Printf format has invalid argument index \[2\*\[1\]`
225 // Something that satisfies the error interface.
226 var e error
227 fmt.Println(e.Error()) // ok
228 // Something that looks like an error interface but isn't, such as the (*T).Error method
229 // in the testing package.
230 var et1 *testing.T
231 et1.Error() // ok
232 et1.Error("hi") // ok
233 et1.Error("%d", 3) // want `\(\*testing.common\).Error call has possible Printf formatting directive %d`
234 et1.Errorf("%s", 1) // want `\(\*testing.common\).Errorf format %s has arg 1 of wrong type int`
235 var et3 errorTest3
236 et3.Error() // ok, not an error method.
237 var et4 errorTest4
238 et4.Error() // ok, not an error method.
239 var et5 errorTest5
240 et5.error() // ok, not an error method.
241 // Interfaces can be used with any verb.
242 var iface interface {
243 ToTheMadness() bool // Method ToTheMadness usually returns false
244 }
245 fmt.Printf("%f", iface) // ok: fmt treats interfaces as transparent and iface may well have a float concrete type
246 // Can't print a function.
247 Printf("%d", someFunction) // want "a.Printf format %d arg someFunction is a func value, not called"
248 Printf("%v", someFunction) // want "a.Printf format %v arg someFunction is a func value, not called"
249 Println(someFunction) // want "a.Println arg someFunction is a func value, not called"
250 Printf("%p", someFunction) // ok: maybe someone wants to see the pointer
251 Printf("%T", someFunction) // ok: maybe someone wants to see the type
252 // Bug: used to recur forever.
253 Printf("%p %x", recursiveStructV, recursiveStructV.next)
254 Printf("%p %x", recursiveStruct1V, recursiveStruct1V.next) // want `a.Printf format %x has arg recursiveStruct1V\.next of wrong type \*a\.RecursiveStruct2`
255 Printf("%p %x", recursiveSliceV, recursiveSliceV)
256 Printf("%p %x", recursiveMapV, recursiveMapV)
257 // Special handling for Log.
258 math.Log(3) // OK
259 var t *testing.T
260 t.Log("%d", 3) // want `\(\*testing.common\).Log call has possible Printf formatting directive %d`
261 t.Logf("%d", 3)
262 t.Logf("%d", "hi") // want `\(\*testing.common\).Logf format %d has arg "hi" of wrong type string`
263
264 Errorf(1, "%d", 3) // OK
265 Errorf(1, "%d", "hi") // want `a.Errorf format %d has arg "hi" of wrong type string`
266
267 // Multiple string arguments before variadic args
268 errorf("WARNING", "foobar") // OK
269 errorf("INFO", "s=%s, n=%d", "foo", 1) // OK
270 errorf("ERROR", "%d") // want "a.errorf format %d reads arg #1, but call has 0 args"
271
272 var tb testing.TB
273 tb.Errorf("%s", 1) // want `\(testing.TB\).Errorf format %s has arg 1 of wrong type int`
274
275 // Printf from external package
276 // externalprintf.Printf("%d", 42) // OK
277 // externalprintf.Printf("foobar") // OK
278 // level := 123
279 // externalprintf.Logf(level, "%d", 42) // OK
280 // externalprintf.Errorf(level, level, "foo %q bar", "foobar") // OK
281 // externalprintf.Logf(level, "%d") // no error "Logf format %d reads arg #1, but call has 0 args"
282 // var formatStr = "%s %s"
283 // externalprintf.Sprintf(formatStr, "a", "b") // OK
284 // externalprintf.Logf(level, formatStr, "a", "b") // OK
285
286 // user-defined Println-like functions
287 ss := &someStruct{}
288 ss.Log(someFunction, "foo") // OK
289 ss.Error(someFunction, someFunction) // OK
290 ss.Println() // OK
291 ss.Println(1.234, "foo") // OK
292 ss.Println(1, someFunction) // no error "Println arg someFunction is a func value, not called"
293 ss.log(someFunction) // OK
294 ss.log(someFunction, "bar", 1.33) // OK
295 ss.log(someFunction, someFunction) // no error "log arg someFunction is a func value, not called"
296
297 // indexed arguments
298 Printf("%d %[3]d %d %[2]d x", 1, 2, 3, 4) // OK
299 Printf("%d %[0]d %d %[2]d x", 1, 2, 3, 4) // want `a.Printf format has invalid argument index \[0\]`
300 Printf("%d %[3]d %d %[-2]d x", 1, 2, 3, 4) // want `a.Printf format has invalid argument index \[-2\]`
301 Printf("%d %[3]d %d %[2234234234234]d x", 1, 2, 3, 4) // want `a.Printf format has invalid argument index \[2234234234234\]`
302 Printf("%d %[3]d %-10d %[2]d x", 1, 2, 3) // want "a.Printf format %-10d reads arg #4, but call has 3 args"
303 Printf("%[1][3]d x", 1, 2) // want `a.Printf format %\[1\]\[ has unknown verb \[`
304 Printf("%[1]d x", 1, 2) // OK
305 Printf("%d %[3]d %d %[2]d x", 1, 2, 3, 4, 5) // OK
306
307 // wrote Println but meant Fprintln
308 Printf("%p\n", os.Stdout) // OK
309 Println(os.Stdout, "hello") // want "a.Println does not take io.Writer but has first arg os.Stdout"
310
311 Printf(someString(), "hello") // OK
312
313 // Printf wrappers in package log should be detected automatically
314 logpkg.Fatal("%d", 1) // want "log.Fatal call has possible Printf formatting directive %d"
315 logpkg.Fatalf("%d", "x") // want `log.Fatalf format %d has arg "x" of wrong type string`
316 logpkg.Fatalln("%d", 1) // want "log.Fatalln call has possible Printf formatting directive %d"
317 logpkg.Panic("%d", 1) // want "log.Panic call has possible Printf formatting directive %d"
318 logpkg.Panicf("%d", "x") // want `log.Panicf format %d has arg "x" of wrong type string`
319 logpkg.Panicln("%d", 1) // want "log.Panicln call has possible Printf formatting directive %d"
320 logpkg.Print("%d", 1) // want "log.Print call has possible Printf formatting directive %d"
321 logpkg.Printf("%d", "x") // want `log.Printf format %d has arg "x" of wrong type string`
322 logpkg.Println("%d", 1) // want "log.Println call has possible Printf formatting directive %d"
323
324 // Methods too.
325 var l *logpkg.Logger
326 l.Fatal("%d", 1) // want `\(\*log.Logger\).Fatal call has possible Printf formatting directive %d`
327 l.Fatalf("%d", "x") // want `\(\*log.Logger\).Fatalf format %d has arg "x" of wrong type string`
328 l.Fatalln("%d", 1) // want `\(\*log.Logger\).Fatalln call has possible Printf formatting directive %d`
329 l.Panic("%d", 1) // want `\(\*log.Logger\).Panic call has possible Printf formatting directive %d`
330 l.Panicf("%d", "x") // want `\(\*log.Logger\).Panicf format %d has arg "x" of wrong type string`
331 l.Panicln("%d", 1) // want `\(\*log.Logger\).Panicln call has possible Printf formatting directive %d`
332 l.Print("%d", 1) // want `\(\*log.Logger\).Print call has possible Printf formatting directive %d`
333 l.Printf("%d", "x") // want `\(\*log.Logger\).Printf format %d has arg "x" of wrong type string`
334 l.Println("%d", 1) // want `\(\*log.Logger\).Println call has possible Printf formatting directive %d`
335
336 // Issue 26486
337 dbg("", 1) // no error "call has arguments but no formatting directive"
338
339 // %w
340 var errSubset interface {
341 Error() string
342 A()
343 }
344 _ = fmt.Errorf("%w", err) // OK
345 _ = fmt.Errorf("%#w", err) // OK
346 _ = fmt.Errorf("%[2]w %[1]s", "x", err) // OK
347 _ = fmt.Errorf("%[2]w %[1]s", e, "x") // want `fmt.Errorf format %\[2\]w has arg "x" of wrong type string`
348 _ = fmt.Errorf("%w", "x") // want `fmt.Errorf format %w has arg "x" of wrong type string`
349 _ = fmt.Errorf("%w %w", err, err) // OK
350 _ = fmt.Errorf("%w", interface{}(nil)) // want `fmt.Errorf format %w has arg interface{}\(nil\) of wrong type interface{}`
351 _ = fmt.Errorf("%w", errorTestOK(0)) // concrete value implements error
352 _ = fmt.Errorf("%w", errSubset) // interface value implements error
353 fmt.Printf("%w", err) // want `fmt.Printf does not support error-wrapping directive %w`
354 var wt *testing.T
355 wt.Errorf("%w", err) // want `\(\*testing.common\).Errorf does not support error-wrapping directive %w`
356 wt.Errorf("%[1][3]d x", 1, 2) // want `\(\*testing.common\).Errorf format %\[1\]\[ has unknown verb \[`
357 wt.Errorf("%[1]d x", 1, 2) // OK
358 // Errorf is a printfWrapper, not an errorfWrapper.
359 Errorf(0, "%w", err) // want `a.Errorf does not support error-wrapping directive %w`
360 // %w should work on fmt.Errorf-based wrappers.
361 var es errorfStruct
362 var eis errorfIntStruct
363 var ess errorfStringStruct
364 es.Errorf("%w", err) // OK
365 eis.Errorf(0, "%w", err) // OK
366 ess.Errorf("ERROR", "%w", err) // OK
367 fmt.Appendf(nil, "%d", "123") // want `wrong type`
368 fmt.Append(nil, "%d", 123) // want `fmt.Append call has possible Printf formatting directive %d`
369
370 }
371
372 func someString() string { return "X" }
373
374 type someStruct struct{}
375
376 // Log is non-variadic user-define Println-like function.
377 // Calls to this func must be skipped when checking
378 // for Println-like arguments.
379 func (ss *someStruct) Log(f func(), s string) {}
380
381 // Error is variadic user-define Println-like function.
382 // Calls to this func mustn't be checked for Println-like arguments,
383 // since variadic arguments type isn't interface{}.
384 func (ss *someStruct) Error(args ...func()) {}
385
386 // Println is variadic user-defined Println-like function.
387 // Calls to this func must be checked for Println-like arguments.
388 func (ss *someStruct) Println(args ...interface{}) {}
389
390 // log is variadic user-defined Println-like function.
391 // Calls to this func must be checked for Println-like arguments.
392 func (ss *someStruct) log(f func(), args ...interface{}) {}
393
394 // A function we use as a function value; it has no other purpose.
395 func someFunction() {}
396
397 // Printf is used by the test so we must declare it.
398 func Printf(format string, args ...interface{}) { // want Printf:"printfWrapper"
399 fmt.Printf(format, args...)
400 }
401
402 // Println is used by the test so we must declare it.
403 func Println(args ...interface{}) { // want Println:"printWrapper"
404 fmt.Println(args...)
405 }
406
407 // printf is used by the test so we must declare it.
408 func printf(format string, args ...interface{}) { // want printf:"printfWrapper"
409 fmt.Printf(format, args...)
410 }
411
412 // Errorf is used by the test for a case in which the first parameter
413 // is not a format string.
414 func Errorf(i int, format string, args ...interface{}) { // want Errorf:"printfWrapper"
415 fmt.Sprintf(format, args...)
416 }
417
418 // errorf is used by the test for a case in which the function accepts multiple
419 // string parameters before variadic arguments
420 func errorf(level, format string, args ...interface{}) { // want errorf:"printfWrapper"
421 fmt.Sprintf(format, args...)
422 }
423
424 type errorfStruct struct{}
425
426 // Errorf is used to test %w works on errorf wrappers.
427 func (errorfStruct) Errorf(format string, args ...interface{}) { // want Errorf:"errorfWrapper"
428 _ = fmt.Errorf(format, args...)
429 }
430
431 type errorfStringStruct struct{}
432
433 // Errorf is used by the test for a case in which the function accepts multiple
434 // string parameters before variadic arguments
435 func (errorfStringStruct) Errorf(level, format string, args ...interface{}) { // want Errorf:"errorfWrapper"
436 _ = fmt.Errorf(format, args...)
437 }
438
439 type errorfIntStruct struct{}
440
441 // Errorf is used by the test for a case in which the first parameter
442 // is not a format string.
443 func (errorfIntStruct) Errorf(i int, format string, args ...interface{}) { // want Errorf:"errorfWrapper"
444 _ = fmt.Errorf(format, args...)
445 }
446
447 // multi is used by the test.
448 func multi() []interface{} {
449 panic("don't call - testing only")
450 }
451
452 type stringer int
453
454 func (stringer) String() string { return "string" }
455
456 type ptrStringer float64
457
458 var stringerv ptrStringer
459
460 func (*ptrStringer) String() string {
461 return "string"
462 }
463
464 func (p *ptrStringer) Warn2(x int, args ...interface{}) string { // want Warn2:"printWrapper"
465 return p.Warn(x, args...)
466 }
467
468 func (p *ptrStringer) Warnf2(x int, format string, args ...interface{}) string { // want Warnf2:"printfWrapper"
469 return p.Warnf(x, format, args...)
470 }
471
472 // During testing -printf.funcs flag matches Warn.
473 func (*ptrStringer) Warn(x int, args ...interface{}) string {
474 return "warn"
475 }
476
477 // During testing -printf.funcs flag matches Warnf.
478 func (*ptrStringer) Warnf(x int, format string, args ...interface{}) string {
479 return "warnf"
480 }
481
482 func (p *ptrStringer) Wrap2(x int, args ...interface{}) string { // want Wrap2:"printWrapper"
483 return p.Wrap(x, args...)
484 }
485
486 func (p *ptrStringer) Wrapf2(x int, format string, args ...interface{}) string { // want Wrapf2:"printfWrapper"
487 return p.Wrapf(x, format, args...)
488 }
489
490 func (*ptrStringer) Wrap(x int, args ...interface{}) string { // want Wrap:"printWrapper"
491 return fmt.Sprint(args...)
492 }
493
494 func (*ptrStringer) Wrapf(x int, format string, args ...interface{}) string { // want Wrapf:"printfWrapper"
495 return fmt.Sprintf(format, args...)
496 }
497
498 func (*ptrStringer) BadWrap(x int, args ...interface{}) string {
499 return fmt.Sprint(args) // want "missing ... in args forwarded to print-like function"
500 }
501
502 func (*ptrStringer) BadWrapf(x int, format string, args ...interface{}) string {
503 return fmt.Sprintf(format, args) // want "missing ... in args forwarded to printf-like function"
504 }
505
506 func (*ptrStringer) WrapfFalsePositive(x int, arg1 string, arg2 ...interface{}) string {
507 return fmt.Sprintf("%s %v", arg1, arg2)
508 }
509
510 type embeddedStringer struct {
511 foo string
512 ptrStringer
513 bar int
514 }
515
516 var embeddedStringerv embeddedStringer
517
518 type notstringer struct {
519 f float64
520 }
521
522 var notstringerv notstringer
523
524 type stringerarray [4]float64
525
526 func (stringerarray) String() string {
527 return "string"
528 }
529
530 var stringerarrayv stringerarray
531
532 type notstringerarray [4]float64
533
534 var notstringerarrayv notstringerarray
535
536 var nonemptyinterface = interface {
537 f()
538 }(nil)
539
540 // A data type we can print with "%d".
541 type percentDStruct struct {
542 a int
543 b []byte
544 c *float64
545 }
546
547 var percentDV percentDStruct
548
549 // A data type we cannot print correctly with "%d".
550 type notPercentDStruct struct {
551 a int
552 b []byte
553 c bool
554 }
555
556 var notPercentDV notPercentDStruct
557
558 // A data type we can print with "%s".
559 type percentSStruct struct {
560 a string
561 b []byte
562 C stringerarray
563 }
564
565 var percentSV percentSStruct
566
567 type recursiveStringer int
568
569 func (s recursiveStringer) String() string {
570 _ = fmt.Sprintf("%d", s)
571 _ = fmt.Sprintf("%#v", s)
572 _ = fmt.Sprintf("%v", s) // want `fmt.Sprintf format %v with arg s causes recursive \(a.recursiveStringer\).String method call`
573 _ = fmt.Sprintf("%v", &s) // want `fmt.Sprintf format %v with arg &s causes recursive \(a.recursiveStringer\).String method call`
574 _ = fmt.Sprintf("%#x", s) // want `fmt.Sprintf format %#x with arg s causes recursive \(a.recursiveStringer\).String method call`
575 _ = fmt.Sprintf("%#x", &s) // want `fmt.Sprintf format %#x with arg &s causes recursive \(a.recursiveStringer\).String method call`
576 _ = fmt.Sprintf("%#X", s) // want `fmt.Sprintf format %#X with arg s causes recursive \(a.recursiveStringer\).String method call`
577 _ = fmt.Sprintf("%#X", &s) // want `fmt.Sprintf format %#X with arg &s causes recursive \(a.recursiveStringer\).String method call`
578 _ = fmt.Sprintf("%#q", s) // want `fmt.Sprintf format %#q with arg s causes recursive \(a.recursiveStringer\).String method call`
579 _ = fmt.Sprintf("%#q", &s) // want `fmt.Sprintf format %#q with arg &s causes recursive \(a.recursiveStringer\).String method call`
580 _ = fmt.Sprintf("%T", s) // ok; does not recursively call String
581 return fmt.Sprintln(s) // want `fmt.Sprintln arg s causes recursive call to \(a.recursiveStringer\).String method`
582 }
583
584 type recursivePtrStringer int
585
586 func (p *recursivePtrStringer) String() string {
587 _ = fmt.Sprintf("%v", *p)
588 _ = fmt.Sprint(&p) // ok; prints address
589 return fmt.Sprintln(p) // want `fmt.Sprintln arg p causes recursive call to \(\*a.recursivePtrStringer\).String method`
590 }
591
592 type recursiveError int
593
594 func (s recursiveError) Error() string {
595 _ = fmt.Sprintf("%d", s)
596 _ = fmt.Sprintf("%#v", s)
597 _ = fmt.Sprintf("%v", s) // want `fmt.Sprintf format %v with arg s causes recursive \(a.recursiveError\).Error method call`
598 _ = fmt.Sprintf("%v", &s) // want `fmt.Sprintf format %v with arg &s causes recursive \(a.recursiveError\).Error method call`
599 _ = fmt.Sprintf("%#x", s) // want `fmt.Sprintf format %#x with arg s causes recursive \(a.recursiveError\).Error method call`
600 _ = fmt.Sprintf("%#x", &s) // want `fmt.Sprintf format %#x with arg &s causes recursive \(a.recursiveError\).Error method call`
601 _ = fmt.Sprintf("%#X", s) // want `fmt.Sprintf format %#X with arg s causes recursive \(a.recursiveError\).Error method call`
602 _ = fmt.Sprintf("%#X", &s) // want `fmt.Sprintf format %#X with arg &s causes recursive \(a.recursiveError\).Error method call`
603 _ = fmt.Sprintf("%#q", s) // want `fmt.Sprintf format %#q with arg s causes recursive \(a.recursiveError\).Error method call`
604 _ = fmt.Sprintf("%#q", &s) // want `fmt.Sprintf format %#q with arg &s causes recursive \(a.recursiveError\).Error method call`
605 _ = fmt.Sprintf("%T", s) // ok; does not recursively call Error
606 return fmt.Sprintln(s) // want `fmt.Sprintln arg s causes recursive call to \(a.recursiveError\).Error method`
607 }
608
609 type recursivePtrError int
610
611 func (p *recursivePtrError) Error() string {
612 _ = fmt.Sprintf("%v", *p)
613 _ = fmt.Sprint(&p) // ok; prints address
614 return fmt.Sprintln(p) // want `fmt.Sprintln arg p causes recursive call to \(\*a.recursivePtrError\).Error method`
615 }
616
617 type recursiveStringerAndError int
618
619 func (s recursiveStringerAndError) String() string {
620 _ = fmt.Sprintf("%d", s)
621 _ = fmt.Sprintf("%#v", s)
622 _ = fmt.Sprintf("%v", s) // want `fmt.Sprintf format %v with arg s causes recursive \(a.recursiveStringerAndError\).String method call`
623 _ = fmt.Sprintf("%v", &s) // want `fmt.Sprintf format %v with arg &s causes recursive \(a.recursiveStringerAndError\).String method call`
624 _ = fmt.Sprintf("%#x", s) // want `fmt.Sprintf format %#x with arg s causes recursive \(a.recursiveStringerAndError\).String method call`
625 _ = fmt.Sprintf("%#x", &s) // want `fmt.Sprintf format %#x with arg &s causes recursive \(a.recursiveStringerAndError\).String method call`
626 _ = fmt.Sprintf("%#X", s) // want `fmt.Sprintf format %#X with arg s causes recursive \(a.recursiveStringerAndError\).String method call`
627 _ = fmt.Sprintf("%#X", &s) // want `fmt.Sprintf format %#X with arg &s causes recursive \(a.recursiveStringerAndError\).String method call`
628 _ = fmt.Sprintf("%#q", s) // want `fmt.Sprintf format %#q with arg s causes recursive \(a.recursiveStringerAndError\).String method call`
629 _ = fmt.Sprintf("%#q", &s) // want `fmt.Sprintf format %#q with arg &s causes recursive \(a.recursiveStringerAndError\).String method call`
630 _ = fmt.Sprintf("%T", s) // ok; does not recursively call String
631 return fmt.Sprintln(s) // want `fmt.Sprintln arg s causes recursive call to \(a.recursiveStringerAndError\).String method`
632 }
633
634 func (s recursiveStringerAndError) Error() string {
635 _ = fmt.Sprintf("%d", s)
636 _ = fmt.Sprintf("%#v", s)
637 _ = fmt.Sprintf("%v", s) // want `fmt.Sprintf format %v with arg s causes recursive \(a.recursiveStringerAndError\).Error method call`
638 _ = fmt.Sprintf("%v", &s) // want `fmt.Sprintf format %v with arg &s causes recursive \(a.recursiveStringerAndError\).Error method call`
639 _ = fmt.Sprintf("%#x", s) // want `fmt.Sprintf format %#x with arg s causes recursive \(a.recursiveStringerAndError\).Error method call`
640 _ = fmt.Sprintf("%#x", &s) // want `fmt.Sprintf format %#x with arg &s causes recursive \(a.recursiveStringerAndError\).Error method call`
641 _ = fmt.Sprintf("%#X", s) // want `fmt.Sprintf format %#X with arg s causes recursive \(a.recursiveStringerAndError\).Error method call`
642 _ = fmt.Sprintf("%#X", &s) // want `fmt.Sprintf format %#X with arg &s causes recursive \(a.recursiveStringerAndError\).Error method call`
643 _ = fmt.Sprintf("%#q", s) // want `fmt.Sprintf format %#q with arg s causes recursive \(a.recursiveStringerAndError\).Error method call`
644 _ = fmt.Sprintf("%#q", &s) // want `fmt.Sprintf format %#q with arg &s causes recursive \(a.recursiveStringerAndError\).Error method call`
645 _ = fmt.Sprintf("%T", s) // ok; does not recursively call Error
646 return fmt.Sprintln(s) // want `fmt.Sprintln arg s causes recursive call to \(a.recursiveStringerAndError\).Error method`
647 }
648
649 type recursivePtrStringerAndError int
650
651 func (p *recursivePtrStringerAndError) String() string {
652 _ = fmt.Sprintf("%v", *p)
653 _ = fmt.Sprint(&p) // ok; prints address
654 return fmt.Sprintln(p) // want `fmt.Sprintln arg p causes recursive call to \(\*a.recursivePtrStringerAndError\).String method`
655 }
656
657 func (p *recursivePtrStringerAndError) Error() string {
658 _ = fmt.Sprintf("%v", *p)
659 _ = fmt.Sprint(&p) // ok; prints address
660 return fmt.Sprintln(p) // want `fmt.Sprintln arg p causes recursive call to \(\*a.recursivePtrStringerAndError\).Error method`
661 }
662
663 // implements a String() method but with non-matching return types
664 type nonStringerWrongReturn int
665
666 func (s nonStringerWrongReturn) String() (string, error) {
667 return "", fmt.Errorf("%v", s)
668 }
669
670 // implements a String() method but with non-matching arguments
671 type nonStringerWrongArgs int
672
673 func (s nonStringerWrongArgs) String(i int) string {
674 return fmt.Sprintf("%d%v", i, s)
675 }
676
677 type cons struct {
678 car int
679 cdr *cons
680 }
681
682 func (cons *cons) String() string {
683 if cons == nil {
684 return "nil"
685 }
686 _ = fmt.Sprint(cons.cdr) // don't want "recursive call" diagnostic
687 return fmt.Sprintf("(%d . %v)", cons.car, cons.cdr) // don't want "recursive call" diagnostic
688 }
689
690 type BoolFormatter bool
691
692 func (*BoolFormatter) Format(fmt.State, rune) {
693 }
694
695 // Formatter with value receiver
696 type FormatterVal bool
697
698 func (FormatterVal) Format(fmt.State, rune) {
699 }
700
701 type RecursiveSlice []RecursiveSlice
702
703 var recursiveSliceV = &RecursiveSlice{}
704
705 type RecursiveMap map[int]RecursiveMap
706
707 var recursiveMapV = make(RecursiveMap)
708
709 type RecursiveStruct struct {
710 next *RecursiveStruct
711 }
712
713 var recursiveStructV = &RecursiveStruct{}
714
715 type RecursiveStruct1 struct {
716 next *RecursiveStruct2
717 }
718
719 type RecursiveStruct2 struct {
720 next *RecursiveStruct1
721 }
722
723 var recursiveStruct1V = &RecursiveStruct1{}
724
725 type unexportedInterface struct {
726 f interface{}
727 }
728
729 // Issue 17798: unexported ptrStringer cannot be formatted.
730 type unexportedStringer struct {
731 t ptrStringer
732 }
733
734 type unexportedStringerOtherFields struct {
735 s string
736 t ptrStringer
737 S string
738 }
739
740 // Issue 17798: unexported error cannot be formatted.
741 type unexportedError struct {
742 e error
743 }
744
745 type unexportedErrorOtherFields struct {
746 s string
747 e error
748 S string
749 }
750
751 type errorer struct{}
752
753 func (e errorer) Error() string { return "errorer" }
754
755 type unexportedCustomError struct {
756 e errorer
757 }
758
759 type errorInterface interface {
760 error
761 ExtraMethod()
762 }
763
764 type unexportedErrorInterface struct {
765 e errorInterface
766 }
767
768 func UnexportedStringerOrError() {
769 fmt.Printf("%s", unexportedInterface{"foo"}) // ok; prints {foo}
770 fmt.Printf("%s", unexportedInterface{3}) // ok; we can't see the problem
771
772 us := unexportedStringer{}
773 fmt.Printf("%s", us) // want "Printf format %s has arg us of wrong type a.unexportedStringer"
774 fmt.Printf("%s", &us) // want "Printf format %s has arg &us of wrong type [*]a.unexportedStringer"
775
776 usf := unexportedStringerOtherFields{
777 s: "foo",
778 S: "bar",
779 }
780 fmt.Printf("%s", usf) // want "Printf format %s has arg usf of wrong type a.unexportedStringerOtherFields"
781 fmt.Printf("%s", &usf) // want "Printf format %s has arg &usf of wrong type [*]a.unexportedStringerOtherFields"
782
783 ue := unexportedError{
784 e: &errorer{},
785 }
786 fmt.Printf("%s", ue) // want "Printf format %s has arg ue of wrong type a.unexportedError"
787 fmt.Printf("%s", &ue) // want "Printf format %s has arg &ue of wrong type [*]a.unexportedError"
788
789 uef := unexportedErrorOtherFields{
790 s: "foo",
791 e: &errorer{},
792 S: "bar",
793 }
794 fmt.Printf("%s", uef) // want "Printf format %s has arg uef of wrong type a.unexportedErrorOtherFields"
795 fmt.Printf("%s", &uef) // want "Printf format %s has arg &uef of wrong type [*]a.unexportedErrorOtherFields"
796
797 uce := unexportedCustomError{
798 e: errorer{},
799 }
800 fmt.Printf("%s", uce) // want "Printf format %s has arg uce of wrong type a.unexportedCustomError"
801
802 uei := unexportedErrorInterface{}
803 fmt.Printf("%s", uei) // want "Printf format %s has arg uei of wrong type a.unexportedErrorInterface"
804 fmt.Println("foo\n", "bar") // not an error
805
806 fmt.Println("foo\n") // want "Println arg list ends with redundant newline"
807 fmt.Println("foo" + "\n") // want "Println arg list ends with redundant newline"
808 fmt.Println("foo\\n") // not an error
809 fmt.Println(`foo\n`) // not an error
810
811 intSlice := []int{3, 4}
812 fmt.Printf("%s", intSlice) // want `fmt.Printf format %s has arg intSlice of wrong type \[\]int`
813 nonStringerArray := [1]unexportedStringer{{}}
814 fmt.Printf("%s", nonStringerArray) // want `fmt.Printf format %s has arg nonStringerArray of wrong type \[1\]a.unexportedStringer`
815 fmt.Printf("%s", []stringer{3, 4}) // not an error
816 fmt.Printf("%s", [2]stringer{3, 4}) // not an error
817 }
818
819 // TODO: Disable complaint about '0' for Go 1.10. To be fixed properly in 1.11.
820 // See issues 23598 and 23605.
821 func DisableErrorForFlag0() {
822 fmt.Printf("%0t", true)
823 }
824
825 // Issue 26486.
826 func dbg(format string, args ...interface{}) {
827 if format == "" {
828 format = "%v"
829 }
830 fmt.Printf(format, args...)
831 }
832
833 func PointersToCompoundTypes() {
834 stringSlice := []string{"a", "b"}
835 fmt.Printf("%s", &stringSlice) // not an error
836
837 intSlice := []int{3, 4}
838 fmt.Printf("%s", &intSlice) // want `fmt.Printf format %s has arg &intSlice of wrong type \*\[\]int`
839
840 stringArray := [2]string{"a", "b"}
841 fmt.Printf("%s", &stringArray) // not an error
842
843 intArray := [2]int{3, 4}
844 fmt.Printf("%s", &intArray) // want `fmt.Printf format %s has arg &intArray of wrong type \*\[2\]int`
845
846 stringStruct := struct{ F string }{"foo"}
847 fmt.Printf("%s", &stringStruct) // not an error
848
849 intStruct := struct{ F int }{3}
850 fmt.Printf("%s", &intStruct) // want `fmt.Printf format %s has arg &intStruct of wrong type \*struct{F int}`
851
852 stringMap := map[string]string{"foo": "bar"}
853 fmt.Printf("%s", &stringMap) // not an error
854
855 intMap := map[int]int{3: 4}
856 fmt.Printf("%s", &intMap) // want `fmt.Printf format %s has arg &intMap of wrong type \*map\[int\]int`
857
858 type T2 struct {
859 X string
860 }
861 type T1 struct {
862 X *T2
863 }
864 fmt.Printf("%s\n", T1{&T2{"x"}}) // want `fmt.Printf format %s has arg T1{&T2{.x.}} of wrong type a\.T1`
865 }
866
867 // Printf wrappers from external package
868 func externalPackage() {
869 b.Wrapf("%s", 1) // want "Wrapf format %s has arg 1 of wrong type int"
870 b.Wrap("%s", 1) // want "Wrap call has possible Printf formatting directive %s"
871 b.NoWrap("%s", 1)
872 b.Wrapf2("%s", 1) // want "Wrapf2 format %s has arg 1 of wrong type int"
873 }
874
875 func PointerVerbs() {
876 // Use booleans, so that we don't just format the elements like in
877 // PointersToCompoundTypes. Bools can only be formatted with verbs like
878 // %t and %v, and none of the ones below.
879 ptr := new(bool)
880 slice := []bool{}
881 array := [3]bool{}
882 map_ := map[bool]bool{}
883 chan_ := make(chan bool)
884 func_ := func(bool) {}
885
886 // %p, %b, %d, %o, %O, %x, and %X all support pointers.
887 fmt.Printf("%p", ptr)
888 fmt.Printf("%b", ptr)
889 fmt.Printf("%d", ptr)
890 fmt.Printf("%o", ptr)
891 fmt.Printf("%O", ptr)
892 fmt.Printf("%x", ptr)
893 fmt.Printf("%X", ptr)
894
895 // %p, %b, %d, %o, %O, %x, and %X all support channels.
896 fmt.Printf("%p", chan_)
897 fmt.Printf("%b", chan_)
898 fmt.Printf("%d", chan_)
899 fmt.Printf("%o", chan_)
900 fmt.Printf("%O", chan_)
901 fmt.Printf("%x", chan_)
902 fmt.Printf("%X", chan_)
903
904 // %p is the only one that supports funcs.
905 fmt.Printf("%p", func_)
906 fmt.Printf("%b", func_) // want `fmt.Printf format %b arg func_ is a func value, not called`
907 fmt.Printf("%d", func_) // want `fmt.Printf format %d arg func_ is a func value, not called`
908 fmt.Printf("%o", func_) // want `fmt.Printf format %o arg func_ is a func value, not called`
909 fmt.Printf("%O", func_) // want `fmt.Printf format %O arg func_ is a func value, not called`
910 fmt.Printf("%x", func_) // want `fmt.Printf format %x arg func_ is a func value, not called`
911 fmt.Printf("%X", func_) // want `fmt.Printf format %X arg func_ is a func value, not called`
912
913 // %p is the only one that supports all slices, by printing the address
914 // of the 0th element.
915 fmt.Printf("%p", slice) // supported; address of 0th element
916 fmt.Printf("%b", slice) // want `fmt.Printf format %b has arg slice of wrong type \[\]bool`
917
918 fmt.Printf("%d", slice) // want `fmt.Printf format %d has arg slice of wrong type \[\]bool`
919
920 fmt.Printf("%o", slice) // want `fmt.Printf format %o has arg slice of wrong type \[\]bool`
921 fmt.Printf("%O", slice) // want `fmt.Printf format %O has arg slice of wrong type \[\]bool`
922
923 fmt.Printf("%x", slice) // want `fmt.Printf format %x has arg slice of wrong type \[\]bool`
924 fmt.Printf("%X", slice) // want `fmt.Printf format %X has arg slice of wrong type \[\]bool`
925
926 // None support arrays.
927 fmt.Printf("%p", array) // want `fmt.Printf format %p has arg array of wrong type \[3\]bool`
928 fmt.Printf("%b", array) // want `fmt.Printf format %b has arg array of wrong type \[3\]bool`
929 fmt.Printf("%d", array) // want `fmt.Printf format %d has arg array of wrong type \[3\]bool`
930 fmt.Printf("%o", array) // want `fmt.Printf format %o has arg array of wrong type \[3\]bool`
931 fmt.Printf("%O", array) // want `fmt.Printf format %O has arg array of wrong type \[3\]bool`
932 fmt.Printf("%x", array) // want `fmt.Printf format %x has arg array of wrong type \[3\]bool`
933 fmt.Printf("%X", array) // want `fmt.Printf format %X has arg array of wrong type \[3\]bool`
934
935 // %p is the only one that supports all maps.
936 fmt.Printf("%p", map_) // supported; address of 0th element
937 fmt.Printf("%b", map_) // want `fmt.Printf format %b has arg map_ of wrong type map\[bool\]bool`
938
939 fmt.Printf("%d", map_) // want `fmt.Printf format %d has arg map_ of wrong type map\[bool\]bool`
940
941 fmt.Printf("%o", map_) // want `fmt.Printf format %o has arg map_ of wrong type map\[bool\]bool`
942 fmt.Printf("%O", map_) // want `fmt.Printf format %O has arg map_ of wrong type map\[bool\]bool`
943
944 fmt.Printf("%x", map_) // want `fmt.Printf format %x has arg map_ of wrong type map\[bool\]bool`
945 fmt.Printf("%X", map_) // want `fmt.Printf format %X has arg map_ of wrong type map\[bool\]bool`
946 }
947
948 // Tests of calls to anonymous print{,f}-wrapper
949 // functions assigned to variables (local/package/field).
950
951 // Test a local assigned an anonymous function using :=.
952 func _() {
953 printf := func(format string, args ...any) { // want printf:"printfWrapper"
954 println(fmt.Sprintf(format, args...))
955 }
956 printf("%s", 123) // want `printf format %s has arg 123 of wrong type int`
957 printf = nil // this doesn't undo the variable's printf-wrapper status
958 printf = func(format string, args ...any) {} // nor does this
959 printf("%s", 123) // want `printf format %s has arg 123 of wrong type int`
960 }
961
962 // Test a global variable.
963 func _() {
964 globalPrintf("%s", 123) // want `globalPrintf format %s has arg 123 of wrong type int`
965
966 // This assignment causes calls to GlobalWrapf2 to
967 // be checked as a wrapper in this package only.
968 b.GlobalWrapf2 = func(format string, args ...any) {
969 println(fmt.Sprintf(format, args...))
970 }
971 b.GlobalWrapf("%s", 123) // want `GlobalWrapf format %s has arg 123 of wrong type int`
972 b.GlobalWrapf2("%s", 123) // want `GlobalWrapf2 format %s has arg 123 of wrong type int`
973 b.GlobalNonWrapf("%s", 123) // nope
974 }
975
976 var globalPrintf = func(format string, args ...any) { // want globalPrintf:"printfWrapper"
977 println(fmt.Sprintf(format, args...))
978 }
979
980 // Test a non-wrapper anonymous function with a plausible signature.
981 func _() {
982 notprintf := func(format string, args ...any) {}
983 notprintf("%s", 123)
984 }
985
986 // Test '=' assignment.
987 // Even calls through the var before it is
988 // assigned a printf-like value are checked.
989 func _() {
990 var printf func(bogus int, format string, args ...any) // want printf:"printfWrapper"
991 printf(0, "%s", 123) // want `printf format %s has arg 123 of wrong type int`
992 printf = func(bogus int, format string, args ...any) {
993 println(fmt.Sprintf(format, args...))
994 }
995 printf(0, "%s", 123) // want `printf format %s has arg 123 of wrong type int`
996 }
997
998 // Test of literal wrapper function assigned to local variable.
999 func _() {
1000 var printf = func(format string, args ...any) { // want printf:"printfWrapper"
1001 println(fmt.Sprintf(format, args...))
1002 }
1003 printf("%s", 123) // want `printf format %s has arg 123 of wrong type int`
1004 }
1005
1006 // Test of literal wrapper function assigned to struct field.
1007 func _() {
1008 var local struct {
1009 printf func(format string, args ...any) // want printf:"printfWrapper"
1010 }
1011 local.printf = func(format string, args ...any) {
1012 println(fmt.Sprintf(format, args...))
1013 }
1014 local.printf("%s", 123) // want `printf format %s has arg 123 of wrong type int`
1015
1016 // This assignment causes calls to Struct.Wrapf to
1017 // be checked as a wrapper in this package only.
1018 b.Struct.Wrapf2 = func(format string, args ...any) {
1019 println(fmt.Sprintf(format, args...))
1020 }
1021 b.Struct.Wrapf("%s", 123) // want `Wrapf format %s has arg 123 of wrong type int`
1022 b.Struct.Wrapf2("%s", 123) // want `Wrapf2 format %s has arg 123 of wrong type int`
1023 b.Struct.NonWrapf("%s", 123) // nope
1024
1025 // variant using generic struct type.
1026 {
1027 type S[T any] struct {
1028 printf func(x T, format string, args ...any) // want printf:"printfWrapper"
1029 }
1030 new(S[bool]).printf = func(_ bool, format string, args ...any) {
1031 println(fmt.Sprintf(format, args...))
1032 }
1033 new(S[int]).printf(0, "%s", 123) // want `printf format %s has arg 123 of wrong type int`
1034 }
1035 }