github.com/dannin/go@v0.0.0-20161031215817-d35dfd405eaa/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("%v", notstringerv) 91 fmt.Printf("%T", notstringerv) 92 fmt.Printf("%q", stringerarrayv) 93 fmt.Printf("%v", stringerarrayv) 94 fmt.Printf("%s", stringerarrayv) 95 fmt.Printf("%v", notstringerarrayv) 96 fmt.Printf("%T", notstringerarrayv) 97 fmt.Printf("%d", new(Formatter)) 98 fmt.Printf("%*%", 2) // Ridiculous but allowed. 99 fmt.Printf("%s", interface{}(nil)) // Nothing useful we can say. 100 101 fmt.Printf("%g", 1+2i) 102 // Some bad format/argTypes 103 fmt.Printf("%b", "hi") // ERROR "arg .hi. for printf verb %b of wrong type" 104 fmt.Printf("%t", c) // ERROR "arg c for printf verb %t of wrong type" 105 fmt.Printf("%t", 1+2i) // ERROR "arg 1 \+ 2i for printf verb %t of wrong type" 106 fmt.Printf("%c", 2.3) // ERROR "arg 2.3 for printf verb %c of wrong type" 107 fmt.Printf("%d", 2.3) // ERROR "arg 2.3 for printf verb %d of wrong type" 108 fmt.Printf("%e", "hi") // ERROR "arg .hi. for printf verb %e of wrong type" 109 fmt.Printf("%E", true) // ERROR "arg true for printf verb %E of wrong type" 110 fmt.Printf("%f", "hi") // ERROR "arg .hi. for printf verb %f of wrong type" 111 fmt.Printf("%F", 'x') // ERROR "arg 'x' for printf verb %F of wrong type" 112 fmt.Printf("%g", "hi") // ERROR "arg .hi. for printf verb %g of wrong type" 113 fmt.Printf("%g", imap) // ERROR "arg imap for printf verb %g of wrong type" 114 fmt.Printf("%G", i) // ERROR "arg i for printf verb %G of wrong type" 115 fmt.Printf("%o", x) // ERROR "arg x for printf verb %o of wrong type" 116 fmt.Printf("%p", 23) // ERROR "arg 23 for printf verb %p of wrong type" 117 fmt.Printf("%q", x) // ERROR "arg x for printf verb %q of wrong type" 118 fmt.Printf("%s", b) // ERROR "arg b for printf verb %s of wrong type" 119 fmt.Printf("%s", byte(65)) // ERROR "arg byte\(65\) for printf verb %s of wrong type" 120 fmt.Printf("%t", 23) // ERROR "arg 23 for printf verb %t of wrong type" 121 fmt.Printf("%U", x) // ERROR "arg x for printf verb %U of wrong type" 122 fmt.Printf("%x", nil) // ERROR "arg nil for printf verb %x of wrong type" 123 fmt.Printf("%X", 2.3) // ERROR "arg 2.3 for printf verb %X of wrong type" 124 fmt.Printf("%s", stringerv) // ERROR "arg stringerv for printf verb %s of wrong type" 125 fmt.Printf("%t", stringerv) // ERROR "arg stringerv for printf verb %t of wrong type" 126 fmt.Printf("%q", notstringerv) // ERROR "arg notstringerv for printf verb %q of wrong type" 127 fmt.Printf("%t", notstringerv) // ERROR "arg notstringerv for printf verb %t of wrong type" 128 fmt.Printf("%t", stringerarrayv) // ERROR "arg stringerarrayv for printf verb %t of wrong type" 129 fmt.Printf("%t", notstringerarrayv) // ERROR "arg notstringerarrayv for printf verb %t of wrong type" 130 fmt.Printf("%q", notstringerarrayv) // ERROR "arg notstringerarrayv for printf verb %q of wrong type" 131 fmt.Printf("%d", Formatter(true)) // correct (the type is responsible for formatting) 132 fmt.Printf("%s", nonemptyinterface) // correct (the dynamic type of nonemptyinterface may be a stringer) 133 fmt.Printf("%.*s %d %g", 3, "hi", 23, 'x') // ERROR "arg 'x' for printf verb %g of wrong type" 134 fmt.Println() // not an error 135 fmt.Println("%s", "hi") // ERROR "possible formatting directive in Println call" 136 fmt.Println("0.0%") // correct (trailing % couldn't be a formatting directive) 137 fmt.Printf("%s", "hi", 3) // ERROR "wrong number of args for format in Printf call" 138 _ = fmt.Sprintf("%"+("s"), "hi", 3) // ERROR "wrong number of args for format in Sprintf call" 139 fmt.Printf("%s%%%d", "hi", 3) // correct 140 fmt.Printf("%08s", "woo") // correct 141 fmt.Printf("% 8s", "woo") // correct 142 fmt.Printf("%.*d", 3, 3) // correct 143 fmt.Printf("%.*d", 3, 3, 3, 3) // ERROR "wrong number of args for format in Printf call.*4 args" 144 fmt.Printf("%.*d", "hi", 3) // ERROR "arg .hi. for \* in printf format not of type int" 145 fmt.Printf("%.*d", i, 3) // correct 146 fmt.Printf("%.*d", s, 3) // ERROR "arg s for \* in printf format not of type int" 147 fmt.Printf("%*%", 0.22) // ERROR "arg 0.22 for \* in printf format not of type int" 148 fmt.Printf("%q %q", multi()...) // ok 149 fmt.Printf("%#q", `blah`) // ok 150 printf("now is the time", "buddy") // ERROR "no formatting directive" 151 Printf("now is the time", "buddy") // ERROR "no formatting directive" 152 Printf("hi") // ok 153 const format = "%s %s\n" 154 Printf(format, "hi", "there") 155 Printf(format, "hi") // ERROR "missing argument for Printf..%s..: format reads arg 2, have only 1" 156 Printf("%s %d %.3v %q", "str", 4) // ERROR "missing argument for Printf..%.3v..: format reads arg 3, have only 2" 157 f := new(stringer) 158 f.Warn(0, "%s", "hello", 3) // ERROR "possible formatting directive in Warn call" 159 f.Warnf(0, "%s", "hello", 3) // ERROR "wrong number of args for format in Warnf call" 160 f.Warnf(0, "%r", "hello") // ERROR "unrecognized printf verb" 161 f.Warnf(0, "%#s", "hello") // ERROR "unrecognized printf flag" 162 Printf("d%", 2) // ERROR "missing verb at end of format string in Printf call" 163 Printf("%d", percentDV) 164 Printf("%d", &percentDV) 165 Printf("%d", notPercentDV) // ERROR "arg notPercentDV for printf verb %d of wrong type" 166 Printf("%d", ¬PercentDV) // ERROR "arg ¬PercentDV for printf verb %d of wrong type" 167 Printf("%p", ¬PercentDV) // Works regardless: we print it as a pointer. 168 Printf("%s", percentSV) 169 Printf("%s", &percentSV) 170 // Good argument reorderings. 171 Printf("%[1]d", 3) 172 Printf("%[1]*d", 3, 1) 173 Printf("%[2]*[1]d", 1, 3) 174 Printf("%[2]*.[1]*[3]d", 2, 3, 4) 175 fmt.Fprintf(os.Stderr, "%[2]*.[1]*[3]d", 2, 3, 4) // Use Fprintf to make sure we count arguments correctly. 176 // Bad argument reorderings. 177 Printf("%[xd", 3) // ERROR "bad syntax for printf argument index: \[xd\]" 178 Printf("%[x]d", 3) // ERROR "bad syntax for printf argument index: \[x\]" 179 Printf("%[3]*s", "hi", 2) // ERROR "missing argument for Printf.* reads arg 3, have only 2" 180 _ = fmt.Sprintf("%[3]d", 2) // ERROR "missing argument for Sprintf.* reads arg 3, have only 1" 181 Printf("%[2]*.[1]*[3]d", 2, "hi", 4) // ERROR "arg .hi. for \* in printf format not of type int" 182 Printf("%[0]s", "arg1") // ERROR "index value \[0\] for Printf.*; indexes start at 1" 183 Printf("%[0]d", 1) // ERROR "index value \[0\] for Printf.*; indexes start at 1" 184 // Something that satisfies the error interface. 185 var e error 186 fmt.Println(e.Error()) // ok 187 // Something that looks like an error interface but isn't, such as the (*T).Error method 188 // in the testing package. 189 var et1 errorTest1 190 fmt.Println(et1.Error()) // ok 191 fmt.Println(et1.Error("hi")) // ok 192 fmt.Println(et1.Error("%d", 3)) // ERROR "possible formatting directive in Error call" 193 var et2 errorTest2 194 et2.Error() // ok 195 et2.Error("hi") // ok, not an error method. 196 et2.Error("%d", 3) // ERROR "possible formatting directive in Error call" 197 var et3 errorTest3 198 et3.Error() // ok, not an error method. 199 var et4 errorTest4 200 et4.Error() // ok, not an error method. 201 var et5 errorTest5 202 et5.error() // ok, not an error method. 203 // Interfaces can be used with any verb. 204 var iface interface { 205 ToTheMadness() bool // Method ToTheMadness usually returns false 206 } 207 fmt.Printf("%f", iface) // ok: fmt treats interfaces as transparent and iface may well have a float concrete type 208 // Can't print a function. 209 Printf("%d", someFunction) // ERROR "arg someFunction in printf call is a function value, not a function call" 210 Printf("%v", someFunction) // ERROR "arg someFunction in printf call is a function value, not a function call" 211 Println(someFunction) // ERROR "arg someFunction in Println call is a function value, not a function call" 212 Printf("%p", someFunction) // ok: maybe someone wants to see the pointer 213 Printf("%T", someFunction) // ok: maybe someone wants to see the type 214 // Bug: used to recur forever. 215 Printf("%p %x", recursiveStructV, recursiveStructV.next) 216 Printf("%p %x", recursiveStruct1V, recursiveStruct1V.next) 217 Printf("%p %x", recursiveSliceV, recursiveSliceV) 218 Printf("%p %x", recursiveMapV, recursiveMapV) 219 // Special handling for Log. 220 math.Log(3) // OK 221 Log(3) // OK 222 Log("%d", 3) // ERROR "possible formatting directive in Log call" 223 Logf("%d", 3) 224 Logf("%d", "hi") // ERROR "arg .hi. for printf verb %d of wrong type: string" 225 226 Errorf(1, "%d", 3) // OK 227 Errorf(1, "%d", "hi") // ERROR "arg .hi. for printf verb %d of wrong type: string" 228 229 // Multiple string arguments before variadic args 230 errorf("WARNING", "foobar") // OK 231 errorf("INFO", "s=%s, n=%d", "foo", 1) // OK 232 errorf("ERROR", "%d") // ERROR "format reads arg 1, have only 0 args" 233 234 // Printf from external package 235 externalprintf.Printf("%d", 42) // OK 236 externalprintf.Printf("foobar") // OK 237 level := 123 238 externalprintf.Logf(level, "%d", 42) // OK 239 externalprintf.Errorf(level, level, "foo %q bar", "foobar") // OK 240 externalprintf.Logf(level, "%d") // ERROR "format reads arg 1, have only 0 args" 241 var formatStr = "%s %s" 242 externalprintf.Sprintf(formatStr, "a", "b") // OK 243 externalprintf.Logf(level, formatStr, "a", "b") // OK 244 245 // user-defined Println-like functions 246 ss := &someStruct{} 247 ss.Log(someFunction, "foo") // OK 248 ss.Error(someFunction, someFunction) // OK 249 ss.Println() // OK 250 ss.Println(1.234, "foo") // OK 251 ss.Println(1, someFunction) // ERROR "arg someFunction in Println call is a function value, not a function call" 252 ss.log(someFunction) // OK 253 ss.log(someFunction, "bar", 1.33) // OK 254 ss.log(someFunction, someFunction) // ERROR "arg someFunction in log call is a function value, not a function call" 255 256 // indexed arguments 257 Printf("%d %[3]d %d %[2]d", 1, 2, 3, 4) // OK 258 Printf("%d %[0]d %d %[2]d", 1, 2, 3, 4) // ERROR "indexes start at 1" 259 Printf("%d %[3]d %d %[-2]d", 1, 2, 3, 4) // ERROR "bad syntax for printf argument index: \[-2\]" 260 Printf("%d %[3]d %d %[2234234234234]d", 1, 2, 3, 4) // ERROR "bad syntax for printf argument index: .+ value out of range" 261 Printf("%d %[3]d %d %[2]d", 1, 2, 3) // ERROR "format reads arg 4, have only 3 args" 262 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" 263 Printf("%[1][3]d", 1, 2) // ERROR "unrecognized printf verb '\['" 264 } 265 266 type someStruct struct{} 267 268 // Log is non-variadic user-define Println-like function. 269 // Calls to this func must be skipped when checking 270 // for Println-like arguments. 271 func (ss *someStruct) Log(f func(), s string) {} 272 273 // Error is variadic user-define Println-like function. 274 // Calls to this func mustn't be checked for Println-like arguments, 275 // since variadic arguments type isn't interface{}. 276 func (ss *someStruct) Error(args ...func()) {} 277 278 // Println is variadic user-defined Println-like function. 279 // Calls to this func must be checked for Println-like arguments. 280 func (ss *someStruct) Println(args ...interface{}) {} 281 282 // log is variadic user-defined Println-like function. 283 // Calls to this func must be checked for Println-like arguments. 284 func (ss *someStruct) log(f func(), args ...interface{}) {} 285 286 // A function we use as a function value; it has no other purpose. 287 func someFunction() {} 288 289 // Printf is used by the test so we must declare it. 290 func Printf(format string, args ...interface{}) { 291 panic("don't call - testing only") 292 } 293 294 // Println is used by the test so we must declare it. 295 func Println(args ...interface{}) { 296 panic("don't call - testing only") 297 } 298 299 // Logf is used by the test so we must declare it. 300 func Logf(format string, args ...interface{}) { 301 panic("don't call - testing only") 302 } 303 304 // Log is used by the test so we must declare it. 305 func Log(args ...interface{}) { 306 panic("don't call - testing only") 307 } 308 309 // printf is used by the test so we must declare it. 310 func printf(format string, args ...interface{}) { 311 panic("don't call - testing only") 312 } 313 314 // Errorf is used by the test for a case in which the first parameter 315 // is not a format string. 316 func Errorf(i int, format string, args ...interface{}) { 317 panic("don't call - testing only") 318 } 319 320 // errorf is used by the test for a case in which the function accepts multiple 321 // string parameters before variadic arguments 322 func errorf(level, format string, args ...interface{}) { 323 panic("don't call - testing only") 324 } 325 326 // multi is used by the test. 327 func multi() []interface{} { 328 panic("don't call - testing only") 329 } 330 331 type stringer float64 332 333 var stringerv stringer 334 335 func (*stringer) String() string { 336 return "string" 337 } 338 339 func (*stringer) Warn(int, ...interface{}) string { 340 return "warn" 341 } 342 343 func (*stringer) Warnf(int, string, ...interface{}) string { 344 return "warnf" 345 } 346 347 type notstringer struct { 348 f float64 349 } 350 351 var notstringerv notstringer 352 353 type stringerarray [4]float64 354 355 func (stringerarray) String() string { 356 return "string" 357 } 358 359 var stringerarrayv stringerarray 360 361 type notstringerarray [4]float64 362 363 var notstringerarrayv notstringerarray 364 365 var nonemptyinterface = interface { 366 f() 367 }(nil) 368 369 // A data type we can print with "%d". 370 type percentDStruct struct { 371 a int 372 b []byte 373 c *float64 374 } 375 376 var percentDV percentDStruct 377 378 // A data type we cannot print correctly with "%d". 379 type notPercentDStruct struct { 380 a int 381 b []byte 382 c bool 383 } 384 385 var notPercentDV notPercentDStruct 386 387 // A data type we can print with "%s". 388 type percentSStruct struct { 389 a string 390 b []byte 391 c stringerarray 392 } 393 394 var percentSV percentSStruct 395 396 type recursiveStringer int 397 398 func (s recursiveStringer) String() string { 399 _ = fmt.Sprintf("%d", s) 400 _ = fmt.Sprintf("%#v", s) 401 _ = fmt.Sprintf("%v", s) // ERROR "arg s for printf causes recursive call to String method" 402 _ = fmt.Sprintf("%v", &s) // ERROR "arg &s for printf causes recursive call to String method" 403 _ = fmt.Sprintf("%T", s) // ok; does not recursively call String 404 return fmt.Sprintln(s) // ERROR "arg s in Sprintln call causes recursive call to String method" 405 } 406 407 type recursivePtrStringer int 408 409 func (p *recursivePtrStringer) String() string { 410 _ = fmt.Sprintf("%v", *p) 411 return fmt.Sprintln(p) // ERROR "arg p in Sprintln call causes recursive call to String method" 412 } 413 414 type Formatter bool 415 416 func (*Formatter) Format(fmt.State, rune) { 417 } 418 419 type RecursiveSlice []RecursiveSlice 420 421 var recursiveSliceV = &RecursiveSlice{} 422 423 type RecursiveMap map[int]RecursiveMap 424 425 var recursiveMapV = make(RecursiveMap) 426 427 type RecursiveStruct struct { 428 next *RecursiveStruct 429 } 430 431 var recursiveStructV = &RecursiveStruct{} 432 433 type RecursiveStruct1 struct { 434 next *Recursive2Struct 435 } 436 437 type RecursiveStruct2 struct { 438 next *Recursive1Struct 439 } 440 441 var recursiveStruct1V = &RecursiveStruct1{} 442 443 // Fix for issue 7149: Missing return type on String method caused fault. 444 func (int) String() { 445 return "" 446 } 447 448 func (s *unknownStruct) Fprintln(w io.Writer, s string) {} 449 450 func UnknownStructFprintln() { 451 s := unknownStruct{} 452 s.Fprintln(os.Stdout, "hello, world!") // OK 453 }