1 // Package pkg is amazing.
13 func (Error) Error() string { return "" }
25 // Some good format/argtypes
27 fmt.Printf("%b %b %b", 3, i, x)
28 fmt.Printf("%c %c %c %c", 3, i, 'x', r)
29 fmt.Printf("%d %d %d", 3, i, imap)
30 fmt.Printf("%e %e %e %e", 3e9, x, fslice, c)
31 fmt.Printf("%E %E %E %E", 3e9, x, fslice, c)
32 fmt.Printf("%f %f %f %f", 3e9, x, fslice, c)
33 fmt.Printf("%F %F %F %F", 3e9, x, fslice, c)
34 fmt.Printf("%g %g %g %g", 3e9, x, fslice, c)
35 fmt.Printf("%G %G %G %G", 3e9, x, fslice, c)
36 fmt.Printf("%b %b %b %b", 3e9, x, fslice, c)
37 fmt.Printf("%o %o", 3, i)
39 fmt.Printf("%q %q %q %q", 3, i, 'x', r)
40 fmt.Printf("%s %s %s", "hi", s, []byte{65})
41 fmt.Printf("%t %t", true, b)
42 fmt.Printf("%T %T", 3, i)
43 fmt.Printf("%U %U", 3, i)
44 fmt.Printf("%v %v", 3, i)
45 fmt.Printf("%x %x %x %x", 3, i, "hi", s)
46 fmt.Printf("%X %X %X %X", 3, i, "hi", s)
47 fmt.Printf("%.*s %d %g", 3, "hi", 23, 2.3)
48 fmt.Printf("%s", &stringerv)
49 fmt.Printf("%v", &stringerv)
50 fmt.Printf("%T", &stringerv)
51 fmt.Printf("%s", &embeddedStringerv)
52 fmt.Printf("%v", &embeddedStringerv)
53 fmt.Printf("%T", &embeddedStringerv)
54 fmt.Printf("%v", notstringerv)
55 fmt.Printf("%T", notstringerv)
56 fmt.Printf("%q", stringerarrayv)
57 fmt.Printf("%v", stringerarrayv)
58 fmt.Printf("%s", stringerarrayv)
59 fmt.Printf("%v", notstringerarrayv)
60 fmt.Printf("%T", notstringerarrayv)
61 fmt.Printf("%d", new(fmt.Formatter))
62 fmt.Printf("%f", new(big.Float))
63 fmt.Printf("%*%", 2) // Ridiculous but allowed.
64 fmt.Printf("%s", interface{}(nil)) // Nothing useful we can say.
66 fmt.Printf("%g", 1+2i)
67 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
68 // Some bad format/argTypes
69 fmt.Printf("%b", "hi") // want `Printf format %b has arg #1 of wrong type string`
70 _ = fmt.Sprintf("%b", "hi") // want `Printf format %b has arg #1 of wrong type string`
71 fmt.Fprintf(os.Stdout, "%b", "hi") // want `Printf format %b has arg #1 of wrong type string`
72 fmt.Printf("%t", c) // want `Printf format %t has arg #1 of wrong type complex64`
73 fmt.Printf("%t", 1+2i) // want `Printf format %t has arg #1 of wrong type complex128`
74 fmt.Printf("%c", 2.3) // want `Printf format %c has arg #1 of wrong type float64`
75 fmt.Printf("%d", 2.3) // want `Printf format %d has arg #1 of wrong type float64`
76 fmt.Printf("%e", "hi") // want `Printf format %e has arg #1 of wrong type string`
77 fmt.Printf("%E", true) // want `Printf format %E has arg #1 of wrong type bool`
78 fmt.Printf("%f", "hi") // want `Printf format %f has arg #1 of wrong type string`
79 fmt.Printf("%F", 'x') // want `Printf format %F has arg #1 of wrong type rune`
80 fmt.Printf("%g", "hi") // want `Printf format %g has arg #1 of wrong type string`
81 fmt.Printf("%g", imap) // want `Printf format %g has arg #1 of wrong type map\[int\]int`
82 fmt.Printf("%G", i) // want `Printf format %G has arg #1 of wrong type int`
83 fmt.Printf("%o", x) // want `Printf format %o has arg #1 of wrong type float64`
84 fmt.Printf("%p", 23) // want `Printf format %p has arg #1 of wrong type int`
85 fmt.Printf("%q", x) // want `Printf format %q has arg #1 of wrong type float64`
86 fmt.Printf("%s", b) // want `Printf format %s has arg #1 of wrong type bool`
87 fmt.Printf("%s", byte(65)) // want `Printf format %s has arg #1 of wrong type byte`
88 fmt.Printf("%t", 23) // want `Printf format %t has arg #1 of wrong type int`
89 fmt.Printf("%U", x) // want `Printf format %U has arg #1 of wrong type float64`
91 fmt.Printf("%X", 2+3i)
92 fmt.Printf("%s", stringerv) // want `Printf format %s has arg #1 of wrong type CheckPrintf\.ptrStringer`
93 fmt.Printf("%t", stringerv) // want `Printf format %t has arg #1 of wrong type CheckPrintf\.ptrStringer`
94 fmt.Printf("%s", embeddedStringerv) // want `Printf format %s has arg #1 of wrong type CheckPrintf\.embeddedStringer`
95 fmt.Printf("%t", embeddedStringerv) // want `Printf format %t has arg #1 of wrong type CheckPrintf\.embeddedStringer`
96 fmt.Printf("%q", notstringerv) // want `Printf format %q has arg #1 of wrong type CheckPrintf\.notstringer`
97 fmt.Printf("%t", notstringerv) // want `Printf format %t has arg #1 of wrong type CheckPrintf\.notstringer`
98 fmt.Printf("%t", stringerarrayv) // want `Printf format %t has arg #1 of wrong type CheckPrintf\.stringerarray`
99 fmt.Printf("%t", notstringerarrayv) // want `Printf format %t has arg #1 of wrong type CheckPrintf\.notstringerarray`
100 fmt.Printf("%q", notstringerarrayv) // want `Printf format %q has arg #1 of wrong type CheckPrintf\.notstringerarray`
101 fmt.Printf("%d", BoolFormatter(true)) // want `Printf format %d has arg #1 of wrong type CheckPrintf\.BoolFormatter`
102 fmt.Printf("%z", FormatterVal(true)) // correct (the type is responsible for formatting)
103 fmt.Printf("%d", FormatterVal(true)) // correct (the type is responsible for formatting)
104 fmt.Printf("%s", nonemptyinterface) // correct (the type is responsible for formatting)
105 fmt.Printf("%.*s %d %6g", 3, "hi", 23, 'x') // want `Printf format %6g has arg #4 of wrong type rune`
106 fmt.Printf("%s", "hi", 3) // want `Printf call needs 1 args but has 2 args`
107 fmt.Printf("%"+("s"), "hi", 3) // want `Printf call needs 1 args but has 2 args`
108 fmt.Printf("%s%%%d", "hi", 3) // correct
109 fmt.Printf("%08s", "woo") // correct
110 fmt.Printf("% 8s", "woo") // correct
111 fmt.Printf("%.*d", 3, 3) // correct
112 fmt.Printf("%.*d x", 3, 3, 3, 3) // want `Printf call needs 2 args but has 4 args`
113 fmt.Printf("%.*d x", "hi", 3) // want `Printf format %\.\*d reads non-int arg #1 as argument of \*`
114 fmt.Printf("%.*d x", i, 3) // correct
115 fmt.Printf("%.*d x", s, 3) // want `Printf format %\.\*d reads non-int arg #1 as argument of \*`
116 fmt.Printf("%*% x", 0.22) // want `Printf format %\*% reads non-int arg #1 as argument of \*`
117 fmt.Printf("%q %q", multi()...) // ok
118 fmt.Printf("%#q", `blah`) // ok
119 const format = "%s %s\n"
120 fmt.Printf(format, "hi", "there")
121 fmt.Printf(format, "hi") // want `Printf format %s reads arg #2, but call has only 1 args`
122 fmt.Printf("%s %d %.3v %q", "str", 4) // want `Printf format %\.3v reads arg #3, but call has only 2 args`
124 fmt.Printf("%#s", FormatterVal(true)) // correct (the type is responsible for formatting)
125 fmt.Printf("d%", 2) // want `couldn't parse format string`
126 fmt.Printf("%d", percentDV)
127 fmt.Printf("%d", &percentDV)
128 fmt.Printf("%d", notPercentDV) // want `Printf format %d has arg #1 of wrong type CheckPrintf\.notPercentDStruct`
129 fmt.Printf("%d", ¬PercentDV) // want `Printf format %d has arg #1 of wrong type \*CheckPrintf\.notPercentDStruct`
130 fmt.Printf("%p", ¬PercentDV) // Works regardless: we print it as a pointer.
131 fmt.Printf("%q", &percentDV) // want `Printf format %q has arg #1 of wrong type \*CheckPrintf\.percentDStruct`
132 fmt.Printf("%s", percentSV)
133 fmt.Printf("%s", &percentSV)
134 // Good argument reorderings.
135 fmt.Printf("%[1]d", 3)
136 fmt.Printf("%[1]*d", 3, 1)
137 fmt.Printf("%[2]*[1]d", 1, 3)
138 fmt.Printf("%[2]*.[1]*[3]d", 2, 3, 4)
139 fmt.Fprintf(os.Stderr, "%[2]*.[1]*[3]d", 2, 3, 4) // Use Fprintf to make sure we count arguments correctly.
140 // Bad argument reorderings.
141 fmt.Printf("%[xd", 3) // want `couldn't parse format string`
142 fmt.Printf("%[x]d x", 3) // want `couldn't parse format string`
143 fmt.Printf("%[3]*s x", "hi", 2) // want `Printf format %\[3\]\*s reads arg #3, but call has only 2 args`
144 fmt.Printf("%[3]d x", 2) // want `Printf format %\[3\]d reads arg #3, but call has only 1 args`
145 fmt.Printf("%[2]*.[1]*[3]d x", 2, "hi", 4) // want `Printf format %\[2\]\*\.\[1\]\*\[3\]d reads non-int arg #2 as argument of \*`
146 fmt.Printf("%[0]s x", "arg1") // want `Printf format %\[0\]s reads invalid arg 0; indices are 1-based`
147 fmt.Printf("%[0]d x", 1) // want `Printf format %\[0\]d reads invalid arg 0; indices are 1-based`
149 // Interfaces can be used with any verb.
150 var iface interface {
151 ToTheMadness() bool // Method ToTheMadness usually returns false
153 fmt.Printf("%f", iface) // ok: fmt treats interfaces as transparent and iface may well have a float concrete type
154 // Can print functions in many ways
155 fmt.Printf("%s", someFunction) // want `Printf format %s has arg #1 of wrong type func\(\)`
156 fmt.Printf("%d", someFunction) // ok: maybe someone wants to see the pointer
157 fmt.Printf("%v", someFunction) // ok: maybe someone wants to see the pointer in decimal
158 fmt.Printf("%p", someFunction) // ok: maybe someone wants to see the pointer
159 fmt.Printf("%T", someFunction) // ok: maybe someone wants to see the type
160 // Bug: used to recur forever.
161 fmt.Printf("%p %x", recursiveStructV, recursiveStructV.next)
162 fmt.Printf("%p %x", recursiveStruct1V, recursiveStruct1V.next)
163 fmt.Printf("%p %x", recursiveSliceV, recursiveSliceV)
164 //fmt.Printf("%p %x", recursiveMapV, recursiveMapV)
167 fmt.Printf("%d %[3]d %d %[2]d x", 1, 2, 3, 4) // OK
168 fmt.Printf("%d %[0]d %d %[2]d x", 1, 2, 3, 4) // want `Printf format %\[0\]d reads invalid arg 0; indices are 1-based`
169 fmt.Printf("%d %[3]d %d %[-2]d x", 1, 2, 3, 4) // want `couldn't parse format string`
170 fmt.Printf("%d %[3]d %d %[5]d x", 1, 2, 3, 4) // want `Printf format %\[5\]d reads arg #5, but call has only 4 args`
171 fmt.Printf("%d %[3]d %-10d %[2]d x", 1, 2, 3) // want `Printf format %-10d reads arg #4, but call has only 3 args`
172 fmt.Printf("%[1][3]d x", 1, 2) // want `couldn't parse format string`
173 fmt.Printf("%[1]d x", 1, 2) // OK
174 fmt.Printf("%d %[3]d %d %[2]d x", 1, 2, 3, 4, 5) // OK
176 fmt.Printf(someString(), "hello") // OK
178 // d accepts pointers as long as they're not to structs.
179 // pointers to structs are dereferenced and walked.
182 // staticcheck's own checks, based on bugs in go vet; see https://github.com/golang/go/issues/27672
195 fmt.Printf("%s\n", &x1)
196 fmt.Printf("%s\n", t1) // want `Printf format %s has arg #1 of wrong type CheckPrintf\.T1`
197 var x2 struct{ A *int }
198 fmt.Printf("%p\n", x2) // want `Printf format %p has arg #1 of wrong type struct\{A \*int\}`
200 fmt.Printf("%p", x3) // want `Printf format %p has arg #1 of wrong type \[2\]int`
202 ue := unexportedError{nil}
206 // staticcheck's own checks, based on our own bugs
207 fmt.Printf("%s", Error(0))
208 fmt.Printf("%x", unsafe.Pointer(uintptr(0)))
211 func someString() string { return "X" }
213 // A function we use as a function value; it has no other purpose.
214 func someFunction() {}
216 // multi is used by the test.
217 func multi() []interface{} {
223 func (stringer) String() string { return "string" }
225 type ptrStringer float64
227 var stringerv ptrStringer
229 func (*ptrStringer) String() string {
233 type embeddedStringer struct {
239 var embeddedStringerv embeddedStringer
241 type notstringer struct {
245 var notstringerv notstringer
247 type stringerarray [4]float64
249 func (stringerarray) String() string {
253 var stringerarrayv stringerarray
255 type notstringerarray [4]float64
257 var notstringerarrayv notstringerarray
259 var nonemptyinterface = interface {
263 // A data type we can print with "%d".
264 type percentDStruct struct {
270 var percentDV percentDStruct
272 // A data type we cannot print correctly with "%d".
273 type notPercentDStruct struct {
279 var notPercentDV notPercentDStruct
281 // A data type we can print with "%s".
282 type percentSStruct struct {
288 var percentSV percentSStruct
290 type BoolFormatter bool
292 func (*BoolFormatter) Format(fmt.State, rune) {
295 // Formatter with value receiver
296 type FormatterVal bool
298 func (FormatterVal) Format(fmt.State, rune) {
301 type RecursiveSlice []RecursiveSlice
303 var recursiveSliceV = &RecursiveSlice{}
305 type RecursiveMap map[int]RecursiveMap
307 var recursiveMapV = make(RecursiveMap)
309 type RecursiveStruct struct {
310 next *RecursiveStruct
313 var recursiveStructV = &RecursiveStruct{}
315 type RecursiveStruct1 struct {
316 next *RecursiveStruct2
319 type RecursiveStruct2 struct {
320 next *RecursiveStruct1
323 var recursiveStruct1V = &RecursiveStruct1{}
325 type unexportedInterface struct {
329 // Issue 17798: unexported ptrStringer cannot be formatted.
330 type unexportedStringer struct {
333 type unexportedStringerOtherFields struct {
339 // Issue 17798: unexported error cannot be formatted.
340 type unexportedError struct {
343 type unexportedErrorOtherFields struct {
349 type errorer struct{}
351 func (e errorer) Error() string { return "errorer" }
353 type unexportedCustomError struct {
357 type errorInterface interface {
362 type unexportedErrorInterface struct {
366 func UnexportedStringerOrError() {
367 fmt.Printf("%s", unexportedInterface{"foo"}) // ok; prints {foo}
368 fmt.Printf("%s", unexportedInterface{3}) // ok; we can't see the problem
370 us := unexportedStringer{}
371 fmt.Printf("%s", us) // want `Printf format %s has arg #1 of wrong type CheckPrintf\.unexportedStringer`
372 fmt.Printf("%s", &us) // want `Printf format %s has arg #1 of wrong type \*CheckPrintf\.unexportedStringer`
374 usf := unexportedStringerOtherFields{
378 fmt.Printf("%s", usf) // want `Printf format %s has arg #1 of wrong type CheckPrintf\.unexportedStringerOtherFields`
379 fmt.Printf("%s", &usf) // want `Printf format %s has arg #1 of wrong type \*CheckPrintf\.unexportedStringerOtherFields`
381 intSlice := []int{3, 4}
382 fmt.Printf("%s", intSlice) // want `Printf format %s has arg #1 of wrong type \[\]int`
383 nonStringerArray := [1]unexportedStringer{{}}
384 fmt.Printf("%s", nonStringerArray) // want `Printf format %s has arg #1 of wrong type \[1\]CheckPrintf\.unexportedStringer`
385 fmt.Printf("%s", []stringer{3, 4}) // not an error
386 fmt.Printf("%s", [2]stringer{3, 4}) // not an error
389 // TODO: Disable complaint about '0' for Go 1.10. To be fixed properly in 1.11.
390 // See issues 23598 and 23605.
391 func DisableErrorForFlag0() {
392 fmt.Printf("%0t", true)
396 func dbg(format string, args ...interface{}) {
400 fmt.Printf(format, args...)