github.com/mattn/go@v0.0.0-20171011075504-07f7db3ea99f/src/cmd/vet/testdata/print.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 testdata 8 9 import ( 10 "fmt" 11 "io" 12 "math" 13 "os" 14 "unsafe" // just for test case printing unsafe.Pointer 15 16 // For testing printf-like functions from external package. 17 "github.com/foobar/externalprintf" 18 ) 19 20 func UnsafePointerPrintfTest() { 21 var up unsafe.Pointer 22 fmt.Printf("%p, %x %X", up, up, up) 23 } 24 25 // Error methods that do not satisfy the Error interface and should be checked. 26 type errorTest1 int 27 28 func (errorTest1) Error(...interface{}) string { 29 return "hi" 30 } 31 32 type errorTest2 int // Analogous to testing's *T type. 33 func (errorTest2) Error(...interface{}) { 34 } 35 36 type errorTest3 int 37 38 func (errorTest3) Error() { // No return value. 39 } 40 41 type errorTest4 int 42 43 func (errorTest4) Error() int { // Different return type. 44 return 3 45 } 46 47 type errorTest5 int 48 49 func (errorTest5) error() { // niladic; don't complain if no args (was bug) 50 } 51 52 // This function never executes, but it serves as a simple test for the program. 53 // Test with make test. 54 func PrintfTests() { 55 var b bool 56 var i int 57 var r rune 58 var s string 59 var x float64 60 var p *int 61 var imap map[int]int 62 var fslice []float64 63 var c complex64 64 // Some good format/argtypes 65 fmt.Printf("") 66 fmt.Printf("%b %b %b", 3, i, x) 67 fmt.Printf("%c %c %c %c", 3, i, 'x', r) 68 fmt.Printf("%d %d %d", 3, i, imap) 69 fmt.Printf("%e %e %e %e", 3e9, x, fslice, c) 70 fmt.Printf("%E %E %E %E", 3e9, x, fslice, c) 71 fmt.Printf("%f %f %f %f", 3e9, x, fslice, c) 72 fmt.Printf("%F %F %F %F", 3e9, x, fslice, c) 73 fmt.Printf("%g %g %g %g", 3e9, x, fslice, c) 74 fmt.Printf("%G %G %G %G", 3e9, x, fslice, c) 75 fmt.Printf("%b %b %b %b", 3e9, x, fslice, c) 76 fmt.Printf("%o %o", 3, i) 77 fmt.Printf("%p %p", p, nil) 78 fmt.Printf("%q %q %q %q", 3, i, 'x', r) 79 fmt.Printf("%s %s %s", "hi", s, []byte{65}) 80 fmt.Printf("%t %t", true, b) 81 fmt.Printf("%T %T", 3, i) 82 fmt.Printf("%U %U", 3, i) 83 fmt.Printf("%v %v", 3, i) 84 fmt.Printf("%x %x %x %x", 3, i, "hi", s) 85 fmt.Printf("%X %X %X %X", 3, i, "hi", s) 86 fmt.Printf("%.*s %d %g", 3, "hi", 23, 2.3) 87 fmt.Printf("%s", &stringerv) 88 fmt.Printf("%v", &stringerv) 89 fmt.Printf("%T", &stringerv) 90 fmt.Printf("%s", &embeddedStringerv) 91 fmt.Printf("%v", &embeddedStringerv) 92 fmt.Printf("%T", &embeddedStringerv) 93 fmt.Printf("%v", notstringerv) 94 fmt.Printf("%T", notstringerv) 95 fmt.Printf("%q", stringerarrayv) 96 fmt.Printf("%v", stringerarrayv) 97 fmt.Printf("%s", stringerarrayv) 98 fmt.Printf("%v", notstringerarrayv) 99 fmt.Printf("%T", notstringerarrayv) 100 fmt.Printf("%d", new(Formatter)) 101 fmt.Printf("%*%", 2) // Ridiculous but allowed. 102 fmt.Printf("%s", interface{}(nil)) // Nothing useful we can say. 103 104 fmt.Printf("%g", 1+2i) 105 fmt.Printf("%#e %#E %#f %#F %#g %#G", 1.2, 1.2, 1.2, 1.2, 1.2, 1.2) // OK since Go 1.9 106 // Some bad format/argTypes 107 fmt.Printf("%b", "hi") // ERROR "arg .hi. for printf verb %b of wrong type" 108 fmt.Printf("%t", c) // ERROR "arg c for printf verb %t of wrong type" 109 fmt.Printf("%t", 1+2i) // ERROR "arg 1 \+ 2i for printf verb %t of wrong type" 110 fmt.Printf("%c", 2.3) // ERROR "arg 2.3 for printf verb %c of wrong type" 111 fmt.Printf("%d", 2.3) // ERROR "arg 2.3 for printf verb %d of wrong type" 112 fmt.Printf("%e", "hi") // ERROR "arg .hi. for printf verb %e of wrong type" 113 fmt.Printf("%E", true) // ERROR "arg true for printf verb %E of wrong type" 114 fmt.Printf("%f", "hi") // ERROR "arg .hi. for printf verb %f of wrong type" 115 fmt.Printf("%F", 'x') // ERROR "arg 'x' for printf verb %F of wrong type" 116 fmt.Printf("%g", "hi") // ERROR "arg .hi. for printf verb %g of wrong type" 117 fmt.Printf("%g", imap) // ERROR "arg imap for printf verb %g of wrong type" 118 fmt.Printf("%G", i) // ERROR "arg i for printf verb %G of wrong type" 119 fmt.Printf("%o", x) // ERROR "arg x for printf verb %o of wrong type" 120 fmt.Printf("%p", 23) // ERROR "arg 23 for printf verb %p of wrong type" 121 fmt.Printf("%q", x) // ERROR "arg x for printf verb %q of wrong type" 122 fmt.Printf("%s", b) // ERROR "arg b for printf verb %s of wrong type" 123 fmt.Printf("%s", byte(65)) // ERROR "arg byte\(65\) for printf verb %s of wrong type" 124 fmt.Printf("%t", 23) // ERROR "arg 23 for printf verb %t of wrong type" 125 fmt.Printf("%U", x) // ERROR "arg x for printf verb %U of wrong type" 126 fmt.Printf("%x", nil) // ERROR "arg nil for printf verb %x of wrong type" 127 fmt.Printf("%X", 2.3) // ERROR "arg 2.3 for printf verb %X of wrong type" 128 fmt.Printf("%s", stringerv) // ERROR "arg stringerv for printf verb %s of wrong type" 129 fmt.Printf("%t", stringerv) // ERROR "arg stringerv for printf verb %t of wrong type" 130 fmt.Printf("%s", embeddedStringerv) // ERROR "arg embeddedStringerv for printf verb %s of wrong type" 131 fmt.Printf("%t", embeddedStringerv) // ERROR "arg embeddedStringerv for printf verb %t of wrong type" 132 fmt.Printf("%q", notstringerv) // ERROR "arg notstringerv for printf verb %q of wrong type" 133 fmt.Printf("%t", notstringerv) // ERROR "arg notstringerv for printf verb %t of wrong type" 134 fmt.Printf("%t", stringerarrayv) // ERROR "arg stringerarrayv for printf verb %t of wrong type" 135 fmt.Printf("%t", notstringerarrayv) // ERROR "arg notstringerarrayv for printf verb %t of wrong type" 136 fmt.Printf("%q", notstringerarrayv) // ERROR "arg notstringerarrayv for printf verb %q of wrong type" 137 fmt.Printf("%d", Formatter(true)) // ERROR "arg Formatter\(true\) for printf verb %d of wrong type: testdata.Formatter" 138 fmt.Printf("%z", FormatterVal(true)) // correct (the type is responsible for formatting) 139 fmt.Printf("%d", FormatterVal(true)) // correct (the type is responsible for formatting) 140 fmt.Printf("%s", nonemptyinterface) // correct (the type is responsible for formatting) 141 fmt.Printf("%.*s %d %g", 3, "hi", 23, 'x') // ERROR "arg 'x' for printf verb %g of wrong type" 142 fmt.Println() // not an error 143 fmt.Println("%s", "hi") // ERROR "possible formatting directive in Println call" 144 fmt.Println("0.0%") // correct (trailing % couldn't be a formatting directive) 145 fmt.Printf("%s", "hi", 3) // ERROR "wrong number of args for format in Printf call" 146 _ = fmt.Sprintf("%"+("s"), "hi", 3) // ERROR "wrong number of args for format in Sprintf call" 147 fmt.Printf("%s%%%d", "hi", 3) // correct 148 fmt.Printf("%08s", "woo") // correct 149 fmt.Printf("% 8s", "woo") // correct 150 fmt.Printf("%.*d", 3, 3) // correct 151 fmt.Printf("%.*d", 3, 3, 3, 3) // ERROR "wrong number of args for format in Printf call.*4 args" 152 fmt.Printf("%.*d", "hi", 3) // ERROR "arg .hi. for \* in printf format not of type int" 153 fmt.Printf("%.*d", i, 3) // correct 154 fmt.Printf("%.*d", s, 3) // ERROR "arg s for \* in printf format not of type int" 155 fmt.Printf("%*%", 0.22) // ERROR "arg 0.22 for \* in printf format not of type int" 156 fmt.Printf("%q %q", multi()...) // ok 157 fmt.Printf("%#q", `blah`) // ok 158 printf("now is the time", "buddy") // ERROR "no formatting directive" 159 Printf("now is the time", "buddy") // ERROR "no formatting directive" 160 Printf("hi") // ok 161 const format = "%s %s\n" 162 Printf(format, "hi", "there") 163 Printf(format, "hi") // ERROR "missing argument for Printf..%s..: format reads arg 2, have only 1" 164 Printf("%s %d %.3v %q", "str", 4) // ERROR "missing argument for Printf..%.3v..: format reads arg 3, have only 2" 165 f := new(stringer) 166 f.Warn(0, "%s", "hello", 3) // ERROR "possible formatting directive in Warn call" 167 f.Warnf(0, "%s", "hello", 3) // ERROR "wrong number of args for format in Warnf call" 168 f.Warnf(0, "%r", "hello") // ERROR "unrecognized printf verb" 169 f.Warnf(0, "%#s", "hello") // ERROR "unrecognized printf flag" 170 Printf("d%", 2) // ERROR "missing verb at end of format string in Printf call" 171 Printf("%d", percentDV) 172 Printf("%d", &percentDV) 173 Printf("%d", notPercentDV) // ERROR "arg notPercentDV for printf verb %d of wrong type" 174 Printf("%d", ¬PercentDV) // ERROR "arg ¬PercentDV for printf verb %d of wrong type" 175 Printf("%p", ¬PercentDV) // Works regardless: we print it as a pointer. 176 Printf("%s", percentSV) 177 Printf("%s", &percentSV) 178 // Good argument reorderings. 179 Printf("%[1]d", 3) 180 Printf("%[1]*d", 3, 1) 181 Printf("%[2]*[1]d", 1, 3) 182 Printf("%[2]*.[1]*[3]d", 2, 3, 4) 183 fmt.Fprintf(os.Stderr, "%[2]*.[1]*[3]d", 2, 3, 4) // Use Fprintf to make sure we count arguments correctly. 184 // Bad argument reorderings. 185 Printf("%[xd", 3) // ERROR "bad syntax for printf argument index: \[xd\]" 186 Printf("%[x]d", 3) // ERROR "bad syntax for printf argument index: \[x\]" 187 Printf("%[3]*s", "hi", 2) // ERROR "missing argument for Printf.* reads arg 3, have only 2" 188 _ = fmt.Sprintf("%[3]d", 2) // ERROR "missing argument for Sprintf.* reads arg 3, have only 1" 189 Printf("%[2]*.[1]*[3]d", 2, "hi", 4) // ERROR "arg .hi. for \* in printf format not of type int" 190 Printf("%[0]s", "arg1") // ERROR "index value \[0\] for Printf.*; indexes start at 1" 191 Printf("%[0]d", 1) // ERROR "index value \[0\] for Printf.*; indexes start at 1" 192 // Something that satisfies the error interface. 193 var e error 194 fmt.Println(e.Error()) // ok 195 // Something that looks like an error interface but isn't, such as the (*T).Error method 196 // in the testing package. 197 var et1 errorTest1 198 fmt.Println(et1.Error()) // ok 199 fmt.Println(et1.Error("hi")) // ok 200 fmt.Println(et1.Error("%d", 3)) // ERROR "possible formatting directive in Error call" 201 var et2 errorTest2 202 et2.Error() // ok 203 et2.Error("hi") // ok, not an error method. 204 et2.Error("%d", 3) // ERROR "possible formatting directive in Error call" 205 var et3 errorTest3 206 et3.Error() // ok, not an error method. 207 var et4 errorTest4 208 et4.Error() // ok, not an error method. 209 var et5 errorTest5 210 et5.error() // ok, not an error method. 211 // Interfaces can be used with any verb. 212 var iface interface { 213 ToTheMadness() bool // Method ToTheMadness usually returns false 214 } 215 fmt.Printf("%f", iface) // ok: fmt treats interfaces as transparent and iface may well have a float concrete type 216 // Can't print a function. 217 Printf("%d", someFunction) // ERROR "arg someFunction in printf call is a function value, not a function call" 218 Printf("%v", someFunction) // ERROR "arg someFunction in printf call is a function value, not a function call" 219 Println(someFunction) // ERROR "arg someFunction in Println call is a function value, not a function call" 220 Printf("%p", someFunction) // ok: maybe someone wants to see the pointer 221 Printf("%T", someFunction) // ok: maybe someone wants to see the type 222 // Bug: used to recur forever. 223 Printf("%p %x", recursiveStructV, recursiveStructV.next) 224 Printf("%p %x", recursiveStruct1V, recursiveStruct1V.next) 225 Printf("%p %x", recursiveSliceV, recursiveSliceV) 226 Printf("%p %x", recursiveMapV, recursiveMapV) 227 // Special handling for Log. 228 math.Log(3) // OK 229 Log(3) // OK 230 Log("%d", 3) // ERROR "possible formatting directive in Log call" 231 Logf("%d", 3) 232 Logf("%d", "hi") // ERROR "arg .hi. for printf verb %d of wrong type: string" 233 234 Errorf(1, "%d", 3) // OK 235 Errorf(1, "%d", "hi") // ERROR "arg .hi. for printf verb %d of wrong type: string" 236 237 // Multiple string arguments before variadic args 238 errorf("WARNING", "foobar") // OK 239 errorf("INFO", "s=%s, n=%d", "foo", 1) // OK 240 errorf("ERROR", "%d") // ERROR "format reads arg 1, have only 0 args" 241 242 // Printf from external package 243 externalprintf.Printf("%d", 42) // OK 244 externalprintf.Printf("foobar") // OK 245 level := 123 246 externalprintf.Logf(level, "%d", 42) // OK 247 externalprintf.Errorf(level, level, "foo %q bar", "foobar") // OK 248 externalprintf.Logf(level, "%d") // ERROR "format reads arg 1, have only 0 args" 249 var formatStr = "%s %s" 250 externalprintf.Sprintf(formatStr, "a", "b") // OK 251 externalprintf.Logf(level, formatStr, "a", "b") // OK 252 253 // user-defined Println-like functions 254 ss := &someStruct{} 255 ss.Log(someFunction, "foo") // OK 256 ss.Error(someFunction, someFunction) // OK 257 ss.Println() // OK 258 ss.Println(1.234, "foo") // OK 259 ss.Println(1, someFunction) // ERROR "arg someFunction in Println call is a function value, not a function call" 260 ss.log(someFunction) // OK 261 ss.log(someFunction, "bar", 1.33) // OK 262 ss.log(someFunction, someFunction) // ERROR "arg someFunction in log call is a function value, not a function call" 263 264 // indexed arguments 265 Printf("%d %[3]d %d %[2]d", 1, 2, 3, 4) // OK 266 Printf("%d %[0]d %d %[2]d", 1, 2, 3, 4) // ERROR "indexes start at 1" 267 Printf("%d %[3]d %d %[-2]d", 1, 2, 3, 4) // ERROR "bad syntax for printf argument index: \[-2\]" 268 Printf("%d %[3]d %d %[2234234234234]d", 1, 2, 3, 4) // ERROR "bad syntax for printf argument index: .+ value out of range" 269 Printf("%d %[3]d %d %[2]d", 1, 2, 3) // ERROR "format reads arg 4, have only 3 args" 270 Printf("%d %[3]d %d %[2]d", 1, 2, 3, 4, 5) // ERROR "wrong number of args for format in Printf call: 4 needed but 5 args" 271 Printf("%[1][3]d", 1, 2) // ERROR "unrecognized printf verb '\['" 272 } 273 274 type someStruct struct{} 275 276 // Log is non-variadic user-define Println-like function. 277 // Calls to this func must be skipped when checking 278 // for Println-like arguments. 279 func (ss *someStruct) Log(f func(), s string) {} 280 281 // Error is variadic user-define Println-like function. 282 // Calls to this func mustn't be checked for Println-like arguments, 283 // since variadic arguments type isn't interface{}. 284 func (ss *someStruct) Error(args ...func()) {} 285 286 // Println is variadic user-defined Println-like function. 287 // Calls to this func must be checked for Println-like arguments. 288 func (ss *someStruct) Println(args ...interface{}) {} 289 290 // log is variadic user-defined Println-like function. 291 // Calls to this func must be checked for Println-like arguments. 292 func (ss *someStruct) log(f func(), args ...interface{}) {} 293 294 // A function we use as a function value; it has no other purpose. 295 func someFunction() {} 296 297 // Printf is used by the test so we must declare it. 298 func Printf(format string, args ...interface{}) { 299 panic("don't call - testing only") 300 } 301 302 // Println is used by the test so we must declare it. 303 func Println(args ...interface{}) { 304 panic("don't call - testing only") 305 } 306 307 // Logf is used by the test so we must declare it. 308 func Logf(format string, args ...interface{}) { 309 panic("don't call - testing only") 310 } 311 312 // Log is used by the test so we must declare it. 313 func Log(args ...interface{}) { 314 panic("don't call - testing only") 315 } 316 317 // printf is used by the test so we must declare it. 318 func printf(format string, args ...interface{}) { 319 panic("don't call - testing only") 320 } 321 322 // Errorf is used by the test for a case in which the first parameter 323 // is not a format string. 324 func Errorf(i int, format string, args ...interface{}) { 325 panic("don't call - testing only") 326 } 327 328 // errorf is used by the test for a case in which the function accepts multiple 329 // string parameters before variadic arguments 330 func errorf(level, format string, args ...interface{}) { 331 panic("don't call - testing only") 332 } 333 334 // multi is used by the test. 335 func multi() []interface{} { 336 panic("don't call - testing only") 337 } 338 339 type stringer float64 340 341 var stringerv stringer 342 343 func (*stringer) String() string { 344 return "string" 345 } 346 347 func (*stringer) Warn(int, ...interface{}) string { 348 return "warn" 349 } 350 351 func (*stringer) Warnf(int, string, ...interface{}) string { 352 return "warnf" 353 } 354 355 type embeddedStringer struct { 356 foo string 357 stringer 358 bar int 359 } 360 361 var embeddedStringerv embeddedStringer 362 363 type notstringer struct { 364 f float64 365 } 366 367 var notstringerv notstringer 368 369 type stringerarray [4]float64 370 371 func (stringerarray) String() string { 372 return "string" 373 } 374 375 var stringerarrayv stringerarray 376 377 type notstringerarray [4]float64 378 379 var notstringerarrayv notstringerarray 380 381 var nonemptyinterface = interface { 382 f() 383 }(nil) 384 385 // A data type we can print with "%d". 386 type percentDStruct struct { 387 a int 388 b []byte 389 c *float64 390 } 391 392 var percentDV percentDStruct 393 394 // A data type we cannot print correctly with "%d". 395 type notPercentDStruct struct { 396 a int 397 b []byte 398 c bool 399 } 400 401 var notPercentDV notPercentDStruct 402 403 // A data type we can print with "%s". 404 type percentSStruct struct { 405 a string 406 b []byte 407 C stringerarray 408 } 409 410 var percentSV percentSStruct 411 412 type recursiveStringer int 413 414 func (s recursiveStringer) String() string { 415 _ = fmt.Sprintf("%d", s) 416 _ = fmt.Sprintf("%#v", s) 417 _ = fmt.Sprintf("%v", s) // ERROR "arg s for printf causes recursive call to String method" 418 _ = fmt.Sprintf("%v", &s) // ERROR "arg &s for printf causes recursive call to String method" 419 _ = fmt.Sprintf("%T", s) // ok; does not recursively call String 420 return fmt.Sprintln(s) // ERROR "arg s in Sprintln call causes recursive call to String method" 421 } 422 423 type recursivePtrStringer int 424 425 func (p *recursivePtrStringer) String() string { 426 _ = fmt.Sprintf("%v", *p) 427 return fmt.Sprintln(p) // ERROR "arg p in Sprintln call causes recursive call to String method" 428 } 429 430 type Formatter bool 431 432 func (*Formatter) Format(fmt.State, rune) { 433 } 434 435 // Formatter with value receiver 436 type FormatterVal bool 437 438 func (FormatterVal) Format(fmt.State, rune) { 439 } 440 441 type RecursiveSlice []RecursiveSlice 442 443 var recursiveSliceV = &RecursiveSlice{} 444 445 type RecursiveMap map[int]RecursiveMap 446 447 var recursiveMapV = make(RecursiveMap) 448 449 type RecursiveStruct struct { 450 next *RecursiveStruct 451 } 452 453 var recursiveStructV = &RecursiveStruct{} 454 455 type RecursiveStruct1 struct { 456 next *Recursive2Struct 457 } 458 459 type RecursiveStruct2 struct { 460 next *Recursive1Struct 461 } 462 463 var recursiveStruct1V = &RecursiveStruct1{} 464 465 // Fix for issue 7149: Missing return type on String method caused fault. 466 func (int) String() { 467 return "" 468 } 469 470 func (s *unknownStruct) Fprintln(w io.Writer, s string) {} 471 472 func UnknownStructFprintln() { 473 s := unknownStruct{} 474 s.Fprintln(os.Stdout, "hello, world!") // OK 475 } 476 477 // Issue 17798: unexported stringer cannot be formatted. 478 type unexportedStringer struct { 479 t stringer 480 } 481 type unexportedStringerOtherFields struct { 482 s string 483 t stringer 484 S string 485 } 486 487 // Issue 17798: unexported error cannot be formatted. 488 type unexportedError struct { 489 e error 490 } 491 type unexportedErrorOtherFields struct { 492 s string 493 e error 494 S string 495 } 496 497 type errorer struct{} 498 499 func (e errorer) Error() string { return "errorer" } 500 501 func UnexportedStringerOrError() { 502 us := unexportedStringer{} 503 fmt.Printf("%s", us) // ERROR "arg us for printf verb %s of wrong type" 504 fmt.Printf("%s", &us) // ERROR "arg &us for printf verb %s of wrong type" 505 506 usf := unexportedStringerOtherFields{ 507 s: "foo", 508 S: "bar", 509 } 510 fmt.Printf("%s", usf) // ERROR "arg usf for printf verb %s of wrong type" 511 fmt.Printf("%s", &usf) // ERROR "arg &usf for printf verb %s of wrong type" 512 513 ue := unexportedError{ 514 e: &errorer{}, 515 } 516 fmt.Printf("%s", ue) // ERROR "arg ue for printf verb %s of wrong type" 517 fmt.Printf("%s", &ue) // ERROR "arg &ue for printf verb %s of wrong type" 518 519 uef := unexportedErrorOtherFields{ 520 s: "foo", 521 e: &errorer{}, 522 S: "bar", 523 } 524 fmt.Printf("%s", uef) // ERROR "arg uef for printf verb %s of wrong type" 525 fmt.Printf("%s", &uef) // ERROR "arg &uef for printf verb %s of wrong type" 526 }