golang.org/x/tools@v0.21.0/go/analysis/passes/printf/testdata/src/a/a.go (about) 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 %q", 3, 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("0.0%") // correct (trailing % couldn't be a formatting directive) 158 fmt.Printf("%s", "hi", 3) // want "fmt.Printf call needs 1 arg but has 2 args" 159 _ = fmt.Sprintf("%"+("s"), "hi", 3) // want "fmt.Sprintf call needs 1 arg but has 2 args" 160 fmt.Printf("%s%%%d", "hi", 3) // correct 161 fmt.Printf("%08s", "woo") // correct 162 fmt.Printf("% 8s", "woo") // correct 163 fmt.Printf("%.*d", 3, 3) // correct 164 fmt.Printf("%.*d x", 3, 3, 3, 3) // want "fmt.Printf call needs 2 args but has 4 args" 165 fmt.Printf("%.*d x", "hi", 3) // want `fmt.Printf format %.*d uses non-int "hi" as argument of \*` 166 fmt.Printf("%.*d x", i, 3) // correct 167 fmt.Printf("%.*d x", s, 3) // want `fmt.Printf format %.\*d uses non-int s as argument of \*` 168 fmt.Printf("%*% x", 0.22) // want `fmt.Printf format %\*% uses non-int 0.22 as argument of \*` 169 fmt.Printf("%q %q", multi()...) // ok 170 fmt.Printf("%#q", `blah`) // ok 171 fmt.Printf("%#b", 3) // ok 172 // printf("now is the time", "buddy") // no error "a.printf call has arguments but no formatting directives" 173 Printf("now is the time", "buddy") // want "a.Printf call has arguments but no formatting directives" 174 Printf("hi") // ok 175 const format = "%s %s\n" 176 Printf(format, "hi", "there") 177 Printf(format, "hi") // want "a.Printf format %s reads arg #2, but call has 1 arg$" 178 Printf("%s %d %.3v %q", "str", 4) // want "a.Printf format %.3v reads arg #3, but call has 2 args" 179 f := new(ptrStringer) 180 f.Warn(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Warn call has possible Printf formatting directive %s` 181 f.Warnf(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Warnf call needs 1 arg but has 2 args` 182 f.Warnf(0, "%r", "hello") // want `\(\*a.ptrStringer\).Warnf format %r has unknown verb r` 183 f.Warnf(0, "%#s", "hello") // want `\(\*a.ptrStringer\).Warnf format %#s has unrecognized flag #` 184 f.Warn2(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Warn2 call has possible Printf formatting directive %s` 185 f.Warnf2(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Warnf2 call needs 1 arg but has 2 args` 186 f.Warnf2(0, "%r", "hello") // want `\(\*a.ptrStringer\).Warnf2 format %r has unknown verb r` 187 f.Warnf2(0, "%#s", "hello") // want `\(\*a.ptrStringer\).Warnf2 format %#s has unrecognized flag #` 188 f.Wrap(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Wrap call has possible Printf formatting directive %s` 189 f.Wrapf(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Wrapf call needs 1 arg but has 2 args` 190 f.Wrapf(0, "%r", "hello") // want `\(\*a.ptrStringer\).Wrapf format %r has unknown verb r` 191 f.Wrapf(0, "%#s", "hello") // want `\(\*a.ptrStringer\).Wrapf format %#s has unrecognized flag #` 192 f.Wrap2(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Wrap2 call has possible Printf formatting directive %s` 193 f.Wrapf2(0, "%s", "hello", 3) // want `\(\*a.ptrStringer\).Wrapf2 call needs 1 arg but has 2 args` 194 f.Wrapf2(0, "%r", "hello") // want `\(\*a.ptrStringer\).Wrapf2 format %r has unknown verb r` 195 f.Wrapf2(0, "%#s", "hello") // want `\(\*a.ptrStringer\).Wrapf2 format %#s has unrecognized flag #` 196 fmt.Printf("%#s", FormatterVal(true)) // correct (the type is responsible for formatting) 197 Printf("d%", 2) // want "a.Printf format % is missing verb at end of string" 198 Printf("%d", percentDV) 199 Printf("%d", &percentDV) 200 Printf("%d", notPercentDV) // want "a.Printf format %d has arg notPercentDV of wrong type a.notPercentDStruct" 201 Printf("%d", ¬PercentDV) // want `a.Printf format %d has arg ¬PercentDV of wrong type \*a.notPercentDStruct` 202 Printf("%p", ¬PercentDV) // Works regardless: we print it as a pointer. 203 Printf("%q", &percentDV) // want `a.Printf format %q has arg &percentDV of wrong type \*a.percentDStruct` 204 Printf("%s", percentSV) 205 Printf("%s", &percentSV) 206 // Good argument reorderings. 207 Printf("%[1]d", 3) 208 Printf("%[1]*d", 3, 1) 209 Printf("%[2]*[1]d", 1, 3) 210 Printf("%[2]*.[1]*[3]d", 2, 3, 4) 211 fmt.Fprintf(os.Stderr, "%[2]*.[1]*[3]d", 2, 3, 4) // Use Fprintf to make sure we count arguments correctly. 212 // Bad argument reorderings. 213 Printf("%[xd", 3) // want `a.Printf format %\[xd is missing closing \]` 214 Printf("%[x]d x", 3) // want `a.Printf format has invalid argument index \[x\]` 215 Printf("%[3]*s x", "hi", 2) // want `a.Printf format has invalid argument index \[3\]` 216 _ = fmt.Sprintf("%[3]d x", 2) // want `fmt.Sprintf format has invalid argument index \[3\]` 217 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 \*` 218 Printf("%[0]s x", "arg1") // want `a.Printf format has invalid argument index \[0\]` 219 Printf("%[0]d x", 1) // want `a.Printf format has invalid argument index \[0\]` 220 Printf("%[3]*.[2*[1]f", 1, 2, 3) // want `a.Printf format has invalid argument index \[2\*\[1\]` 221 // Something that satisfies the error interface. 222 var e error 223 fmt.Println(e.Error()) // ok 224 // Something that looks like an error interface but isn't, such as the (*T).Error method 225 // in the testing package. 226 var et1 *testing.T 227 et1.Error() // ok 228 et1.Error("hi") // ok 229 et1.Error("%d", 3) // want `\(\*testing.common\).Error call has possible Printf formatting directive %d` 230 et1.Errorf("%s", 1) // want `\(\*testing.common\).Errorf format %s has arg 1 of wrong type int` 231 var et3 errorTest3 232 et3.Error() // ok, not an error method. 233 var et4 errorTest4 234 et4.Error() // ok, not an error method. 235 var et5 errorTest5 236 et5.error() // ok, not an error method. 237 // Interfaces can be used with any verb. 238 var iface interface { 239 ToTheMadness() bool // Method ToTheMadness usually returns false 240 } 241 fmt.Printf("%f", iface) // ok: fmt treats interfaces as transparent and iface may well have a float concrete type 242 // Can't print a function. 243 Printf("%d", someFunction) // want "a.Printf format %d arg someFunction is a func value, not called" 244 Printf("%v", someFunction) // want "a.Printf format %v arg someFunction is a func value, not called" 245 Println(someFunction) // want "a.Println arg someFunction is a func value, not called" 246 Printf("%p", someFunction) // ok: maybe someone wants to see the pointer 247 Printf("%T", someFunction) // ok: maybe someone wants to see the type 248 // Bug: used to recur forever. 249 Printf("%p %x", recursiveStructV, recursiveStructV.next) 250 Printf("%p %x", recursiveStruct1V, recursiveStruct1V.next) // want `a.Printf format %x has arg recursiveStruct1V\.next of wrong type \*a\.RecursiveStruct2` 251 Printf("%p %x", recursiveSliceV, recursiveSliceV) 252 Printf("%p %x", recursiveMapV, recursiveMapV) 253 // Special handling for Log. 254 math.Log(3) // OK 255 var t *testing.T 256 t.Log("%d", 3) // want `\(\*testing.common\).Log call has possible Printf formatting directive %d` 257 t.Logf("%d", 3) 258 t.Logf("%d", "hi") // want `\(\*testing.common\).Logf format %d has arg "hi" of wrong type string` 259 260 Errorf(1, "%d", 3) // OK 261 Errorf(1, "%d", "hi") // want `a.Errorf format %d has arg "hi" of wrong type string` 262 263 // Multiple string arguments before variadic args 264 errorf("WARNING", "foobar") // OK 265 errorf("INFO", "s=%s, n=%d", "foo", 1) // OK 266 errorf("ERROR", "%d") // want "a.errorf format %d reads arg #1, but call has 0 args" 267 268 var tb testing.TB 269 tb.Errorf("%s", 1) // want `\(testing.TB\).Errorf format %s has arg 1 of wrong type int` 270 271 // Printf from external package 272 // externalprintf.Printf("%d", 42) // OK 273 // externalprintf.Printf("foobar") // OK 274 // level := 123 275 // externalprintf.Logf(level, "%d", 42) // OK 276 // externalprintf.Errorf(level, level, "foo %q bar", "foobar") // OK 277 // externalprintf.Logf(level, "%d") // no error "Logf format %d reads arg #1, but call has 0 args" 278 // var formatStr = "%s %s" 279 // externalprintf.Sprintf(formatStr, "a", "b") // OK 280 // externalprintf.Logf(level, formatStr, "a", "b") // OK 281 282 // user-defined Println-like functions 283 ss := &someStruct{} 284 ss.Log(someFunction, "foo") // OK 285 ss.Error(someFunction, someFunction) // OK 286 ss.Println() // OK 287 ss.Println(1.234, "foo") // OK 288 ss.Println(1, someFunction) // no error "Println arg someFunction is a func value, not called" 289 ss.log(someFunction) // OK 290 ss.log(someFunction, "bar", 1.33) // OK 291 ss.log(someFunction, someFunction) // no error "log arg someFunction is a func value, not called" 292 293 // indexed arguments 294 Printf("%d %[3]d %d %[2]d x", 1, 2, 3, 4) // OK 295 Printf("%d %[0]d %d %[2]d x", 1, 2, 3, 4) // want `a.Printf format has invalid argument index \[0\]` 296 Printf("%d %[3]d %d %[-2]d x", 1, 2, 3, 4) // want `a.Printf format has invalid argument index \[-2\]` 297 Printf("%d %[3]d %d %[2234234234234]d x", 1, 2, 3, 4) // want `a.Printf format has invalid argument index \[2234234234234\]` 298 Printf("%d %[3]d %-10d %[2]d x", 1, 2, 3) // want "a.Printf format %-10d reads arg #4, but call has 3 args" 299 Printf("%[1][3]d x", 1, 2) // want `a.Printf format %\[1\]\[ has unknown verb \[` 300 Printf("%[1]d x", 1, 2) // OK 301 Printf("%d %[3]d %d %[2]d x", 1, 2, 3, 4, 5) // OK 302 303 // wrote Println but meant Fprintln 304 Printf("%p\n", os.Stdout) // OK 305 Println(os.Stdout, "hello") // want "a.Println does not take io.Writer but has first arg os.Stdout" 306 307 Printf(someString(), "hello") // OK 308 309 // Printf wrappers in package log should be detected automatically 310 logpkg.Fatal("%d", 1) // want "log.Fatal call has possible Printf formatting directive %d" 311 logpkg.Fatalf("%d", "x") // want `log.Fatalf format %d has arg "x" of wrong type string` 312 logpkg.Fatalln("%d", 1) // want "log.Fatalln call has possible Printf formatting directive %d" 313 logpkg.Panic("%d", 1) // want "log.Panic call has possible Printf formatting directive %d" 314 logpkg.Panicf("%d", "x") // want `log.Panicf format %d has arg "x" of wrong type string` 315 logpkg.Panicln("%d", 1) // want "log.Panicln call has possible Printf formatting directive %d" 316 logpkg.Print("%d", 1) // want "log.Print call has possible Printf formatting directive %d" 317 logpkg.Printf("%d", "x") // want `log.Printf format %d has arg "x" of wrong type string` 318 logpkg.Println("%d", 1) // want "log.Println call has possible Printf formatting directive %d" 319 320 // Methods too. 321 var l *logpkg.Logger 322 l.Fatal("%d", 1) // want `\(\*log.Logger\).Fatal call has possible Printf formatting directive %d` 323 l.Fatalf("%d", "x") // want `\(\*log.Logger\).Fatalf format %d has arg "x" of wrong type string` 324 l.Fatalln("%d", 1) // want `\(\*log.Logger\).Fatalln call has possible Printf formatting directive %d` 325 l.Panic("%d", 1) // want `\(\*log.Logger\).Panic call has possible Printf formatting directive %d` 326 l.Panicf("%d", "x") // want `\(\*log.Logger\).Panicf format %d has arg "x" of wrong type string` 327 l.Panicln("%d", 1) // want `\(\*log.Logger\).Panicln call has possible Printf formatting directive %d` 328 l.Print("%d", 1) // want `\(\*log.Logger\).Print call has possible Printf formatting directive %d` 329 l.Printf("%d", "x") // want `\(\*log.Logger\).Printf format %d has arg "x" of wrong type string` 330 l.Println("%d", 1) // want `\(\*log.Logger\).Println call has possible Printf formatting directive %d` 331 332 // Issue 26486 333 dbg("", 1) // no error "call has arguments but no formatting directive" 334 335 // %w 336 var errSubset interface { 337 Error() string 338 A() 339 } 340 _ = fmt.Errorf("%w", err) // OK 341 _ = fmt.Errorf("%#w", err) // OK 342 _ = fmt.Errorf("%[2]w %[1]s", "x", err) // OK 343 _ = fmt.Errorf("%[2]w %[1]s", e, "x") // want `fmt.Errorf format %\[2\]w has arg "x" of wrong type string` 344 _ = fmt.Errorf("%w", "x") // want `fmt.Errorf format %w has arg "x" of wrong type string` 345 _ = fmt.Errorf("%w %w", err, err) // OK 346 _ = fmt.Errorf("%w", interface{}(nil)) // want `fmt.Errorf format %w has arg interface{}\(nil\) of wrong type interface{}` 347 _ = fmt.Errorf("%w", errorTestOK(0)) // concrete value implements error 348 _ = fmt.Errorf("%w", errSubset) // interface value implements error 349 fmt.Printf("%w", err) // want `fmt.Printf does not support error-wrapping directive %w` 350 var wt *testing.T 351 wt.Errorf("%w", err) // want `\(\*testing.common\).Errorf does not support error-wrapping directive %w` 352 wt.Errorf("%[1][3]d x", 1, 2) // want `\(\*testing.common\).Errorf format %\[1\]\[ has unknown verb \[` 353 wt.Errorf("%[1]d x", 1, 2) // OK 354 // Errorf is a printfWrapper, not an errorfWrapper. 355 Errorf(0, "%w", err) // want `a.Errorf does not support error-wrapping directive %w` 356 // %w should work on fmt.Errorf-based wrappers. 357 var es errorfStruct 358 var eis errorfIntStruct 359 var ess errorfStringStruct 360 es.Errorf("%w", err) // OK 361 eis.Errorf(0, "%w", err) // OK 362 ess.Errorf("ERROR", "%w", err) // OK 363 fmt.Appendf(nil, "%d", "123") // want `wrong type` 364 fmt.Append(nil, "%d", 123) // want `fmt.Append call has possible Printf formatting directive %d` 365 366 } 367 368 func someString() string { return "X" } 369 370 type someStruct struct{} 371 372 // Log is non-variadic user-define Println-like function. 373 // Calls to this func must be skipped when checking 374 // for Println-like arguments. 375 func (ss *someStruct) Log(f func(), s string) {} 376 377 // Error is variadic user-define Println-like function. 378 // Calls to this func mustn't be checked for Println-like arguments, 379 // since variadic arguments type isn't interface{}. 380 func (ss *someStruct) Error(args ...func()) {} 381 382 // Println is variadic user-defined Println-like function. 383 // Calls to this func must be checked for Println-like arguments. 384 func (ss *someStruct) Println(args ...interface{}) {} 385 386 // log is variadic user-defined Println-like function. 387 // Calls to this func must be checked for Println-like arguments. 388 func (ss *someStruct) log(f func(), args ...interface{}) {} 389 390 // A function we use as a function value; it has no other purpose. 391 func someFunction() {} 392 393 // Printf is used by the test so we must declare it. 394 func Printf(format string, args ...interface{}) { // want Printf:"printfWrapper" 395 fmt.Printf(format, args...) 396 } 397 398 // Println is used by the test so we must declare it. 399 func Println(args ...interface{}) { // want Println:"printWrapper" 400 fmt.Println(args...) 401 } 402 403 // printf is used by the test so we must declare it. 404 func printf(format string, args ...interface{}) { // want printf:"printfWrapper" 405 fmt.Printf(format, args...) 406 } 407 408 // Errorf is used by the test for a case in which the first parameter 409 // is not a format string. 410 func Errorf(i int, format string, args ...interface{}) { // want Errorf:"printfWrapper" 411 fmt.Sprintf(format, args...) 412 } 413 414 // errorf is used by the test for a case in which the function accepts multiple 415 // string parameters before variadic arguments 416 func errorf(level, format string, args ...interface{}) { // want errorf:"printfWrapper" 417 fmt.Sprintf(format, args...) 418 } 419 420 type errorfStruct struct{} 421 422 // Errorf is used to test %w works on errorf wrappers. 423 func (errorfStruct) Errorf(format string, args ...interface{}) { // want Errorf:"errorfWrapper" 424 _ = fmt.Errorf(format, args...) 425 } 426 427 type errorfStringStruct struct{} 428 429 // Errorf is used by the test for a case in which the function accepts multiple 430 // string parameters before variadic arguments 431 func (errorfStringStruct) Errorf(level, format string, args ...interface{}) { // want Errorf:"errorfWrapper" 432 _ = fmt.Errorf(format, args...) 433 } 434 435 type errorfIntStruct struct{} 436 437 // Errorf is used by the test for a case in which the first parameter 438 // is not a format string. 439 func (errorfIntStruct) Errorf(i int, format string, args ...interface{}) { // want Errorf:"errorfWrapper" 440 _ = fmt.Errorf(format, args...) 441 } 442 443 // multi is used by the test. 444 func multi() []interface{} { 445 panic("don't call - testing only") 446 } 447 448 type stringer int 449 450 func (stringer) String() string { return "string" } 451 452 type ptrStringer float64 453 454 var stringerv ptrStringer 455 456 func (*ptrStringer) String() string { 457 return "string" 458 } 459 460 func (p *ptrStringer) Warn2(x int, args ...interface{}) string { // want Warn2:"printWrapper" 461 return p.Warn(x, args...) 462 } 463 464 func (p *ptrStringer) Warnf2(x int, format string, args ...interface{}) string { // want Warnf2:"printfWrapper" 465 return p.Warnf(x, format, args...) 466 } 467 468 // During testing -printf.funcs flag matches Warn. 469 func (*ptrStringer) Warn(x int, args ...interface{}) string { 470 return "warn" 471 } 472 473 // During testing -printf.funcs flag matches Warnf. 474 func (*ptrStringer) Warnf(x int, format string, args ...interface{}) string { 475 return "warnf" 476 } 477 478 func (p *ptrStringer) Wrap2(x int, args ...interface{}) string { // want Wrap2:"printWrapper" 479 return p.Wrap(x, args...) 480 } 481 482 func (p *ptrStringer) Wrapf2(x int, format string, args ...interface{}) string { // want Wrapf2:"printfWrapper" 483 return p.Wrapf(x, format, args...) 484 } 485 486 func (*ptrStringer) Wrap(x int, args ...interface{}) string { // want Wrap:"printWrapper" 487 return fmt.Sprint(args...) 488 } 489 490 func (*ptrStringer) Wrapf(x int, format string, args ...interface{}) string { // want Wrapf:"printfWrapper" 491 return fmt.Sprintf(format, args...) 492 } 493 494 func (*ptrStringer) BadWrap(x int, args ...interface{}) string { 495 return fmt.Sprint(args) // want "missing ... in args forwarded to print-like function" 496 } 497 498 func (*ptrStringer) BadWrapf(x int, format string, args ...interface{}) string { 499 return fmt.Sprintf(format, args) // want "missing ... in args forwarded to printf-like function" 500 } 501 502 func (*ptrStringer) WrapfFalsePositive(x int, arg1 string, arg2 ...interface{}) string { 503 return fmt.Sprintf("%s %v", arg1, arg2) 504 } 505 506 type embeddedStringer struct { 507 foo string 508 ptrStringer 509 bar int 510 } 511 512 var embeddedStringerv embeddedStringer 513 514 type notstringer struct { 515 f float64 516 } 517 518 var notstringerv notstringer 519 520 type stringerarray [4]float64 521 522 func (stringerarray) String() string { 523 return "string" 524 } 525 526 var stringerarrayv stringerarray 527 528 type notstringerarray [4]float64 529 530 var notstringerarrayv notstringerarray 531 532 var nonemptyinterface = interface { 533 f() 534 }(nil) 535 536 // A data type we can print with "%d". 537 type percentDStruct struct { 538 a int 539 b []byte 540 c *float64 541 } 542 543 var percentDV percentDStruct 544 545 // A data type we cannot print correctly with "%d". 546 type notPercentDStruct struct { 547 a int 548 b []byte 549 c bool 550 } 551 552 var notPercentDV notPercentDStruct 553 554 // A data type we can print with "%s". 555 type percentSStruct struct { 556 a string 557 b []byte 558 C stringerarray 559 } 560 561 var percentSV percentSStruct 562 563 type recursiveStringer int 564 565 func (s recursiveStringer) String() string { 566 _ = fmt.Sprintf("%d", s) 567 _ = fmt.Sprintf("%#v", s) 568 _ = fmt.Sprintf("%v", s) // want `fmt.Sprintf format %v with arg s causes recursive \(a.recursiveStringer\).String method call` 569 _ = fmt.Sprintf("%v", &s) // want `fmt.Sprintf format %v with arg &s causes recursive \(a.recursiveStringer\).String method call` 570 _ = fmt.Sprintf("%T", s) // ok; does not recursively call String 571 return fmt.Sprintln(s) // want `fmt.Sprintln arg s causes recursive call to \(a.recursiveStringer\).String method` 572 } 573 574 type recursivePtrStringer int 575 576 func (p *recursivePtrStringer) String() string { 577 _ = fmt.Sprintf("%v", *p) 578 _ = fmt.Sprint(&p) // ok; prints address 579 return fmt.Sprintln(p) // want `fmt.Sprintln arg p causes recursive call to \(\*a.recursivePtrStringer\).String method` 580 } 581 582 type recursiveError int 583 584 func (s recursiveError) Error() string { 585 _ = fmt.Sprintf("%d", s) 586 _ = fmt.Sprintf("%#v", s) 587 _ = fmt.Sprintf("%v", s) // want `fmt.Sprintf format %v with arg s causes recursive \(a.recursiveError\).Error method call` 588 _ = fmt.Sprintf("%v", &s) // want `fmt.Sprintf format %v with arg &s causes recursive \(a.recursiveError\).Error method call` 589 _ = fmt.Sprintf("%T", s) // ok; does not recursively call Error 590 return fmt.Sprintln(s) // want `fmt.Sprintln arg s causes recursive call to \(a.recursiveError\).Error method` 591 } 592 593 type recursivePtrError int 594 595 func (p *recursivePtrError) Error() string { 596 _ = fmt.Sprintf("%v", *p) 597 _ = fmt.Sprint(&p) // ok; prints address 598 return fmt.Sprintln(p) // want `fmt.Sprintln arg p causes recursive call to \(\*a.recursivePtrError\).Error method` 599 } 600 601 type recursiveStringerAndError int 602 603 func (s recursiveStringerAndError) String() string { 604 _ = fmt.Sprintf("%d", s) 605 _ = fmt.Sprintf("%#v", s) 606 _ = fmt.Sprintf("%v", s) // want `fmt.Sprintf format %v with arg s causes recursive \(a.recursiveStringerAndError\).String method call` 607 _ = fmt.Sprintf("%v", &s) // want `fmt.Sprintf format %v with arg &s causes recursive \(a.recursiveStringerAndError\).String method call` 608 _ = fmt.Sprintf("%T", s) // ok; does not recursively call String 609 return fmt.Sprintln(s) // want `fmt.Sprintln arg s causes recursive call to \(a.recursiveStringerAndError\).String method` 610 } 611 612 func (s recursiveStringerAndError) Error() string { 613 _ = fmt.Sprintf("%d", s) 614 _ = fmt.Sprintf("%#v", s) 615 _ = fmt.Sprintf("%v", s) // want `fmt.Sprintf format %v with arg s causes recursive \(a.recursiveStringerAndError\).Error method call` 616 _ = fmt.Sprintf("%v", &s) // want `fmt.Sprintf format %v with arg &s causes recursive \(a.recursiveStringerAndError\).Error method call` 617 _ = fmt.Sprintf("%T", s) // ok; does not recursively call Error 618 return fmt.Sprintln(s) // want `fmt.Sprintln arg s causes recursive call to \(a.recursiveStringerAndError\).Error method` 619 } 620 621 type recursivePtrStringerAndError int 622 623 func (p *recursivePtrStringerAndError) String() string { 624 _ = fmt.Sprintf("%v", *p) 625 _ = fmt.Sprint(&p) // ok; prints address 626 return fmt.Sprintln(p) // want `fmt.Sprintln arg p causes recursive call to \(\*a.recursivePtrStringerAndError\).String method` 627 } 628 629 func (p *recursivePtrStringerAndError) Error() string { 630 _ = fmt.Sprintf("%v", *p) 631 _ = fmt.Sprint(&p) // ok; prints address 632 return fmt.Sprintln(p) // want `fmt.Sprintln arg p causes recursive call to \(\*a.recursivePtrStringerAndError\).Error method` 633 } 634 635 // implements a String() method but with non-matching return types 636 type nonStringerWrongReturn int 637 638 func (s nonStringerWrongReturn) String() (string, error) { 639 return "", fmt.Errorf("%v", s) 640 } 641 642 // implements a String() method but with non-matching arguments 643 type nonStringerWrongArgs int 644 645 func (s nonStringerWrongArgs) String(i int) string { 646 return fmt.Sprintf("%d%v", i, s) 647 } 648 649 type cons struct { 650 car int 651 cdr *cons 652 } 653 654 func (cons *cons) String() string { 655 if cons == nil { 656 return "nil" 657 } 658 _ = fmt.Sprint(cons.cdr) // don't want "recursive call" diagnostic 659 return fmt.Sprintf("(%d . %v)", cons.car, cons.cdr) // don't want "recursive call" diagnostic 660 } 661 662 type BoolFormatter bool 663 664 func (*BoolFormatter) Format(fmt.State, rune) { 665 } 666 667 // Formatter with value receiver 668 type FormatterVal bool 669 670 func (FormatterVal) Format(fmt.State, rune) { 671 } 672 673 type RecursiveSlice []RecursiveSlice 674 675 var recursiveSliceV = &RecursiveSlice{} 676 677 type RecursiveMap map[int]RecursiveMap 678 679 var recursiveMapV = make(RecursiveMap) 680 681 type RecursiveStruct struct { 682 next *RecursiveStruct 683 } 684 685 var recursiveStructV = &RecursiveStruct{} 686 687 type RecursiveStruct1 struct { 688 next *RecursiveStruct2 689 } 690 691 type RecursiveStruct2 struct { 692 next *RecursiveStruct1 693 } 694 695 var recursiveStruct1V = &RecursiveStruct1{} 696 697 type unexportedInterface struct { 698 f interface{} 699 } 700 701 // Issue 17798: unexported ptrStringer cannot be formatted. 702 type unexportedStringer struct { 703 t ptrStringer 704 } 705 706 type unexportedStringerOtherFields struct { 707 s string 708 t ptrStringer 709 S string 710 } 711 712 // Issue 17798: unexported error cannot be formatted. 713 type unexportedError struct { 714 e error 715 } 716 717 type unexportedErrorOtherFields struct { 718 s string 719 e error 720 S string 721 } 722 723 type errorer struct{} 724 725 func (e errorer) Error() string { return "errorer" } 726 727 type unexportedCustomError struct { 728 e errorer 729 } 730 731 type errorInterface interface { 732 error 733 ExtraMethod() 734 } 735 736 type unexportedErrorInterface struct { 737 e errorInterface 738 } 739 740 func UnexportedStringerOrError() { 741 fmt.Printf("%s", unexportedInterface{"foo"}) // ok; prints {foo} 742 fmt.Printf("%s", unexportedInterface{3}) // ok; we can't see the problem 743 744 us := unexportedStringer{} 745 fmt.Printf("%s", us) // want "Printf format %s has arg us of wrong type a.unexportedStringer" 746 fmt.Printf("%s", &us) // want "Printf format %s has arg &us of wrong type [*]a.unexportedStringer" 747 748 usf := unexportedStringerOtherFields{ 749 s: "foo", 750 S: "bar", 751 } 752 fmt.Printf("%s", usf) // want "Printf format %s has arg usf of wrong type a.unexportedStringerOtherFields" 753 fmt.Printf("%s", &usf) // want "Printf format %s has arg &usf of wrong type [*]a.unexportedStringerOtherFields" 754 755 ue := unexportedError{ 756 e: &errorer{}, 757 } 758 fmt.Printf("%s", ue) // want "Printf format %s has arg ue of wrong type a.unexportedError" 759 fmt.Printf("%s", &ue) // want "Printf format %s has arg &ue of wrong type [*]a.unexportedError" 760 761 uef := unexportedErrorOtherFields{ 762 s: "foo", 763 e: &errorer{}, 764 S: "bar", 765 } 766 fmt.Printf("%s", uef) // want "Printf format %s has arg uef of wrong type a.unexportedErrorOtherFields" 767 fmt.Printf("%s", &uef) // want "Printf format %s has arg &uef of wrong type [*]a.unexportedErrorOtherFields" 768 769 uce := unexportedCustomError{ 770 e: errorer{}, 771 } 772 fmt.Printf("%s", uce) // want "Printf format %s has arg uce of wrong type a.unexportedCustomError" 773 774 uei := unexportedErrorInterface{} 775 fmt.Printf("%s", uei) // want "Printf format %s has arg uei of wrong type a.unexportedErrorInterface" 776 fmt.Println("foo\n", "bar") // not an error 777 778 fmt.Println("foo\n") // want "Println arg list ends with redundant newline" 779 fmt.Println("foo" + "\n") // want "Println arg list ends with redundant newline" 780 fmt.Println("foo\\n") // not an error 781 fmt.Println(`foo\n`) // not an error 782 783 intSlice := []int{3, 4} 784 fmt.Printf("%s", intSlice) // want `fmt.Printf format %s has arg intSlice of wrong type \[\]int` 785 nonStringerArray := [1]unexportedStringer{{}} 786 fmt.Printf("%s", nonStringerArray) // want `fmt.Printf format %s has arg nonStringerArray of wrong type \[1\]a.unexportedStringer` 787 fmt.Printf("%s", []stringer{3, 4}) // not an error 788 fmt.Printf("%s", [2]stringer{3, 4}) // not an error 789 } 790 791 // TODO: Disable complaint about '0' for Go 1.10. To be fixed properly in 1.11. 792 // See issues 23598 and 23605. 793 func DisableErrorForFlag0() { 794 fmt.Printf("%0t", true) 795 } 796 797 // Issue 26486. 798 func dbg(format string, args ...interface{}) { 799 if format == "" { 800 format = "%v" 801 } 802 fmt.Printf(format, args...) 803 } 804 805 func PointersToCompoundTypes() { 806 stringSlice := []string{"a", "b"} 807 fmt.Printf("%s", &stringSlice) // not an error 808 809 intSlice := []int{3, 4} 810 fmt.Printf("%s", &intSlice) // want `fmt.Printf format %s has arg &intSlice of wrong type \*\[\]int` 811 812 stringArray := [2]string{"a", "b"} 813 fmt.Printf("%s", &stringArray) // not an error 814 815 intArray := [2]int{3, 4} 816 fmt.Printf("%s", &intArray) // want `fmt.Printf format %s has arg &intArray of wrong type \*\[2\]int` 817 818 stringStruct := struct{ F string }{"foo"} 819 fmt.Printf("%s", &stringStruct) // not an error 820 821 intStruct := struct{ F int }{3} 822 fmt.Printf("%s", &intStruct) // want `fmt.Printf format %s has arg &intStruct of wrong type \*struct{F int}` 823 824 stringMap := map[string]string{"foo": "bar"} 825 fmt.Printf("%s", &stringMap) // not an error 826 827 intMap := map[int]int{3: 4} 828 fmt.Printf("%s", &intMap) // want `fmt.Printf format %s has arg &intMap of wrong type \*map\[int\]int` 829 830 type T2 struct { 831 X string 832 } 833 type T1 struct { 834 X *T2 835 } 836 fmt.Printf("%s\n", T1{&T2{"x"}}) // want `fmt.Printf format %s has arg T1{&T2{.x.}} of wrong type a\.T1` 837 } 838 839 // Printf wrappers from external package 840 func externalPackage() { 841 b.Wrapf("%s", 1) // want "Wrapf format %s has arg 1 of wrong type int" 842 b.Wrap("%s", 1) // want "Wrap call has possible Printf formatting directive %s" 843 b.NoWrap("%s", 1) 844 b.Wrapf2("%s", 1) // want "Wrapf2 format %s has arg 1 of wrong type int" 845 } 846 847 func PointerVerbs() { 848 // Use booleans, so that we don't just format the elements like in 849 // PointersToCompoundTypes. Bools can only be formatted with verbs like 850 // %t and %v, and none of the ones below. 851 ptr := new(bool) 852 slice := []bool{} 853 array := [3]bool{} 854 map_ := map[bool]bool{} 855 chan_ := make(chan bool) 856 func_ := func(bool) {} 857 858 // %p, %b, %d, %o, %O, %x, and %X all support pointers. 859 fmt.Printf("%p", ptr) 860 fmt.Printf("%b", ptr) 861 fmt.Printf("%d", ptr) 862 fmt.Printf("%o", ptr) 863 fmt.Printf("%O", ptr) 864 fmt.Printf("%x", ptr) 865 fmt.Printf("%X", ptr) 866 867 // %p, %b, %d, %o, %O, %x, and %X all support channels. 868 fmt.Printf("%p", chan_) 869 fmt.Printf("%b", chan_) 870 fmt.Printf("%d", chan_) 871 fmt.Printf("%o", chan_) 872 fmt.Printf("%O", chan_) 873 fmt.Printf("%x", chan_) 874 fmt.Printf("%X", chan_) 875 876 // %p is the only one that supports funcs. 877 fmt.Printf("%p", func_) 878 fmt.Printf("%b", func_) // want `fmt.Printf format %b arg func_ is a func value, not called` 879 fmt.Printf("%d", func_) // want `fmt.Printf format %d arg func_ is a func value, not called` 880 fmt.Printf("%o", func_) // want `fmt.Printf format %o arg func_ is a func value, not called` 881 fmt.Printf("%O", func_) // want `fmt.Printf format %O arg func_ is a func value, not called` 882 fmt.Printf("%x", func_) // want `fmt.Printf format %x arg func_ is a func value, not called` 883 fmt.Printf("%X", func_) // want `fmt.Printf format %X arg func_ is a func value, not called` 884 885 // %p is the only one that supports all slices, by printing the address 886 // of the 0th element. 887 fmt.Printf("%p", slice) // supported; address of 0th element 888 fmt.Printf("%b", slice) // want `fmt.Printf format %b has arg slice of wrong type \[\]bool` 889 890 fmt.Printf("%d", slice) // want `fmt.Printf format %d has arg slice of wrong type \[\]bool` 891 892 fmt.Printf("%o", slice) // want `fmt.Printf format %o has arg slice of wrong type \[\]bool` 893 fmt.Printf("%O", slice) // want `fmt.Printf format %O has arg slice of wrong type \[\]bool` 894 895 fmt.Printf("%x", slice) // want `fmt.Printf format %x has arg slice of wrong type \[\]bool` 896 fmt.Printf("%X", slice) // want `fmt.Printf format %X has arg slice of wrong type \[\]bool` 897 898 // None support arrays. 899 fmt.Printf("%p", array) // want `fmt.Printf format %p has arg array of wrong type \[3\]bool` 900 fmt.Printf("%b", array) // want `fmt.Printf format %b has arg array of wrong type \[3\]bool` 901 fmt.Printf("%d", array) // want `fmt.Printf format %d has arg array of wrong type \[3\]bool` 902 fmt.Printf("%o", array) // want `fmt.Printf format %o has arg array of wrong type \[3\]bool` 903 fmt.Printf("%O", array) // want `fmt.Printf format %O has arg array of wrong type \[3\]bool` 904 fmt.Printf("%x", array) // want `fmt.Printf format %x has arg array of wrong type \[3\]bool` 905 fmt.Printf("%X", array) // want `fmt.Printf format %X has arg array of wrong type \[3\]bool` 906 907 // %p is the only one that supports all maps. 908 fmt.Printf("%p", map_) // supported; address of 0th element 909 fmt.Printf("%b", map_) // want `fmt.Printf format %b has arg map_ of wrong type map\[bool\]bool` 910 911 fmt.Printf("%d", map_) // want `fmt.Printf format %d has arg map_ of wrong type map\[bool\]bool` 912 913 fmt.Printf("%o", map_) // want `fmt.Printf format %o has arg map_ of wrong type map\[bool\]bool` 914 fmt.Printf("%O", map_) // want `fmt.Printf format %O has arg map_ of wrong type map\[bool\]bool` 915 916 fmt.Printf("%x", map_) // want `fmt.Printf format %x has arg map_ of wrong type map\[bool\]bool` 917 fmt.Printf("%X", map_) // want `fmt.Printf format %X has arg map_ of wrong type map\[bool\]bool` 918 }