github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/cmd/vet/testdata/src/print/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 print 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 // 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) 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(fmt.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 "Printf format %b has arg \x22hi\x22 of wrong type string" 108 fmt.Printf("%t", c) // ERROR "Printf format %t has arg c of wrong type complex64" 109 fmt.Printf("%t", 1+2i) // ERROR "Printf format %t has arg 1 \+ 2i of wrong type complex128" 110 fmt.Printf("%c", 2.3) // ERROR "Printf format %c has arg 2.3 of wrong type float64" 111 fmt.Printf("%d", 2.3) // ERROR "Printf format %d has arg 2.3 of wrong type float64" 112 fmt.Printf("%e", "hi") // ERROR "Printf format %e has arg \x22hi\x22 of wrong type string" 113 fmt.Printf("%E", true) // ERROR "Printf format %E has arg true of wrong type bool" 114 fmt.Printf("%f", "hi") // ERROR "Printf format %f has arg \x22hi\x22 of wrong type string" 115 fmt.Printf("%F", 'x') // ERROR "Printf format %F has arg 'x' of wrong type rune" 116 fmt.Printf("%g", "hi") // ERROR "Printf format %g has arg \x22hi\x22 of wrong type string" 117 fmt.Printf("%g", imap) // ERROR "Printf format %g has arg imap of wrong type map\[int\]int" 118 fmt.Printf("%G", i) // ERROR "Printf format %G has arg i of wrong type int" 119 fmt.Printf("%o", x) // ERROR "Printf format %o has arg x of wrong type float64" 120 fmt.Printf("%p", nil) // ERROR "Printf format %p has arg nil of wrong type untyped nil" 121 fmt.Printf("%p", 23) // ERROR "Printf format %p has arg 23 of wrong type int" 122 fmt.Printf("%q", x) // ERROR "Printf format %q has arg x of wrong type float64" 123 fmt.Printf("%s", b) // ERROR "Printf format %s has arg b of wrong type bool" 124 fmt.Printf("%s", byte(65)) // ERROR "Printf format %s has arg byte\(65\) of wrong type byte" 125 fmt.Printf("%t", 23) // ERROR "Printf format %t has arg 23 of wrong type int" 126 fmt.Printf("%U", x) // ERROR "Printf format %U has arg x of wrong type float64" 127 fmt.Printf("%x", nil) // ERROR "Printf format %x has arg nil of wrong type untyped nil" 128 fmt.Printf("%X", 2.3) // ERROR "Printf format %X has arg 2.3 of wrong type float64" 129 fmt.Printf("%s", stringerv) // ERROR "Printf format %s has arg stringerv of wrong type print.ptrStringer" 130 fmt.Printf("%t", stringerv) // ERROR "Printf format %t has arg stringerv of wrong type print.ptrStringer" 131 fmt.Printf("%s", embeddedStringerv) // ERROR "Printf format %s has arg embeddedStringerv of wrong type print.embeddedStringer" 132 fmt.Printf("%t", embeddedStringerv) // ERROR "Printf format %t has arg embeddedStringerv of wrong type print.embeddedStringer" 133 fmt.Printf("%q", notstringerv) // ERROR "Printf format %q has arg notstringerv of wrong type print.notstringer" 134 fmt.Printf("%t", notstringerv) // ERROR "Printf format %t has arg notstringerv of wrong type print.notstringer" 135 fmt.Printf("%t", stringerarrayv) // ERROR "Printf format %t has arg stringerarrayv of wrong type print.stringerarray" 136 fmt.Printf("%t", notstringerarrayv) // ERROR "Printf format %t has arg notstringerarrayv of wrong type print.notstringerarray" 137 fmt.Printf("%q", notstringerarrayv) // ERROR "Printf format %q has arg notstringerarrayv of wrong type print.notstringerarray" 138 fmt.Printf("%d", BoolFormatter(true)) // ERROR "Printf format %d has arg BoolFormatter\(true\) of wrong type print.BoolFormatter" 139 fmt.Printf("%z", FormatterVal(true)) // correct (the type is responsible for formatting) 140 fmt.Printf("%d", FormatterVal(true)) // correct (the type is responsible for formatting) 141 fmt.Printf("%s", nonemptyinterface) // correct (the type is responsible for formatting) 142 fmt.Printf("%.*s %d %6g", 3, "hi", 23, 'x') // ERROR "Printf format %6g has arg 'x' of wrong type rune" 143 fmt.Println() // not an error 144 fmt.Println("%s", "hi") // ERROR "Println call has possible formatting directive %s" 145 fmt.Println("%v", "hi") // ERROR "Println call has possible formatting directive %v" 146 fmt.Println("%T", "hi") // ERROR "Println call has possible formatting directive %T" 147 fmt.Println("0.0%") // correct (trailing % couldn't be a formatting directive) 148 fmt.Printf("%s", "hi", 3) // ERROR "Printf call needs 1 arg but has 2 args" 149 _ = fmt.Sprintf("%"+("s"), "hi", 3) // ERROR "Sprintf call needs 1 arg but has 2 args" 150 fmt.Printf("%s%%%d", "hi", 3) // correct 151 fmt.Printf("%08s", "woo") // correct 152 fmt.Printf("% 8s", "woo") // correct 153 fmt.Printf("%.*d", 3, 3) // correct 154 fmt.Printf("%.*d x", 3, 3, 3, 3) // ERROR "Printf call needs 2 args but has 4 args" 155 fmt.Printf("%.*d x", "hi", 3) // ERROR "Printf format %.*d uses non-int \x22hi\x22 as argument of \*" 156 fmt.Printf("%.*d x", i, 3) // correct 157 fmt.Printf("%.*d x", s, 3) // ERROR "Printf format %.\*d uses non-int s as argument of \*" 158 fmt.Printf("%*% x", 0.22) // ERROR "Printf format %\*% uses non-int 0.22 as argument of \*" 159 fmt.Printf("%q %q", multi()...) // ok 160 fmt.Printf("%#q", `blah`) // ok 161 // printf("now is the time", "buddy") // no error "printf call has arguments but no formatting directives" 162 Printf("now is the time", "buddy") // ERROR "Printf call has arguments but no formatting directives" 163 Printf("hi") // ok 164 const format = "%s %s\n" 165 Printf(format, "hi", "there") 166 Printf(format, "hi") // ERROR "Printf format %s reads arg #2, but call has 1 arg$" 167 Printf("%s %d %.3v %q", "str", 4) // ERROR "Printf format %.3v reads arg #3, but call has 2 args" 168 f := new(ptrStringer) 169 f.Warn(0, "%s", "hello", 3) // ERROR "Warn call has possible formatting directive %s" 170 f.Warnf(0, "%s", "hello", 3) // ERROR "Warnf call needs 1 arg but has 2 args" 171 f.Warnf(0, "%r", "hello") // ERROR "Warnf format %r has unknown verb r" 172 f.Warnf(0, "%#s", "hello") // ERROR "Warnf format %#s has unrecognized flag #" 173 f.Warn2(0, "%s", "hello", 3) // ERROR "Warn2 call has possible formatting directive %s" 174 f.Warnf2(0, "%s", "hello", 3) // ERROR "Warnf2 call needs 1 arg but has 2 args" 175 f.Warnf2(0, "%r", "hello") // ERROR "Warnf2 format %r has unknown verb r" 176 f.Warnf2(0, "%#s", "hello") // ERROR "Warnf2 format %#s has unrecognized flag #" 177 f.Wrap(0, "%s", "hello", 3) // ERROR "Wrap call has possible formatting directive %s" 178 f.Wrapf(0, "%s", "hello", 3) // ERROR "Wrapf call needs 1 arg but has 2 args" 179 f.Wrapf(0, "%r", "hello") // ERROR "Wrapf format %r has unknown verb r" 180 f.Wrapf(0, "%#s", "hello") // ERROR "Wrapf format %#s has unrecognized flag #" 181 f.Wrap2(0, "%s", "hello", 3) // ERROR "Wrap2 call has possible formatting directive %s" 182 f.Wrapf2(0, "%s", "hello", 3) // ERROR "Wrapf2 call needs 1 arg but has 2 args" 183 f.Wrapf2(0, "%r", "hello") // ERROR "Wrapf2 format %r has unknown verb r" 184 f.Wrapf2(0, "%#s", "hello") // ERROR "Wrapf2 format %#s has unrecognized flag #" 185 fmt.Printf("%#s", FormatterVal(true)) // correct (the type is responsible for formatting) 186 Printf("d%", 2) // ERROR "Printf format % is missing verb at end of string" 187 Printf("%d", percentDV) 188 Printf("%d", &percentDV) 189 Printf("%d", notPercentDV) // ERROR "Printf format %d has arg notPercentDV of wrong type print.notPercentDStruct" 190 Printf("%d", ¬PercentDV) // ERROR "Printf format %d has arg ¬PercentDV of wrong type \*print.notPercentDStruct" 191 Printf("%p", ¬PercentDV) // Works regardless: we print it as a pointer. 192 Printf("%q", &percentDV) // ERROR "Printf format %q has arg &percentDV of wrong type \*print.percentDStruct" 193 Printf("%s", percentSV) 194 Printf("%s", &percentSV) 195 // Good argument reorderings. 196 Printf("%[1]d", 3) 197 Printf("%[1]*d", 3, 1) 198 Printf("%[2]*[1]d", 1, 3) 199 Printf("%[2]*.[1]*[3]d", 2, 3, 4) 200 fmt.Fprintf(os.Stderr, "%[2]*.[1]*[3]d", 2, 3, 4) // Use Fprintf to make sure we count arguments correctly. 201 // Bad argument reorderings. 202 Printf("%[xd", 3) // ERROR "Printf format %\[xd is missing closing \]" 203 Printf("%[x]d x", 3) // ERROR "Printf format has invalid argument index \[x\]" 204 Printf("%[3]*s x", "hi", 2) // ERROR "Printf format has invalid argument index \[3\]" 205 _ = fmt.Sprintf("%[3]d x", 2) // ERROR "Sprintf format has invalid argument index \[3\]" 206 Printf("%[2]*.[1]*[3]d x", 2, "hi", 4) // ERROR "Printf format %\[2]\*\.\[1\]\*\[3\]d uses non-int \x22hi\x22 as argument of \*" 207 Printf("%[0]s x", "arg1") // ERROR "Printf format has invalid argument index \[0\]" 208 Printf("%[0]d x", 1) // ERROR "Printf format has invalid argument index \[0\]" 209 // Something that satisfies the error interface. 210 var e error 211 fmt.Println(e.Error()) // ok 212 // Something that looks like an error interface but isn't, such as the (*T).Error method 213 // in the testing package. 214 var et1 *testing.T 215 et1.Error() // ok 216 et1.Error("hi") // ok 217 et1.Error("%d", 3) // ERROR "Error call has possible formatting directive %d" 218 var et3 errorTest3 219 et3.Error() // ok, not an error method. 220 var et4 errorTest4 221 et4.Error() // ok, not an error method. 222 var et5 errorTest5 223 et5.error() // ok, not an error method. 224 // Interfaces can be used with any verb. 225 var iface interface { 226 ToTheMadness() bool // Method ToTheMadness usually returns false 227 } 228 fmt.Printf("%f", iface) // ok: fmt treats interfaces as transparent and iface may well have a float concrete type 229 // Can't print a function. 230 Printf("%d", someFunction) // ERROR "Printf format %d arg someFunction is a func value, not called" 231 Printf("%v", someFunction) // ERROR "Printf format %v arg someFunction is a func value, not called" 232 Println(someFunction) // ERROR "Println arg someFunction is a func value, not called" 233 Printf("%p", someFunction) // ok: maybe someone wants to see the pointer 234 Printf("%T", someFunction) // ok: maybe someone wants to see the type 235 // Bug: used to recur forever. 236 Printf("%p %x", recursiveStructV, recursiveStructV.next) 237 Printf("%p %x", recursiveStruct1V, recursiveStruct1V.next) // ERROR "Printf format %x has arg recursiveStruct1V\.next of wrong type \*print\.RecursiveStruct2" 238 Printf("%p %x", recursiveSliceV, recursiveSliceV) 239 Printf("%p %x", recursiveMapV, recursiveMapV) 240 // Special handling for Log. 241 math.Log(3) // OK 242 var t *testing.T 243 t.Log("%d", 3) // ERROR "Log call has possible formatting directive %d" 244 t.Logf("%d", 3) 245 t.Logf("%d", "hi") // ERROR "Logf format %d has arg \x22hi\x22 of wrong type string" 246 247 Errorf(1, "%d", 3) // OK 248 Errorf(1, "%d", "hi") // ERROR "Errorf format %d has arg \x22hi\x22 of wrong type string" 249 250 // Multiple string arguments before variadic args 251 errorf("WARNING", "foobar") // OK 252 errorf("INFO", "s=%s, n=%d", "foo", 1) // OK 253 errorf("ERROR", "%d") // ERROR "errorf format %d reads arg #1, but call has 0 args" 254 255 // Printf from external package 256 // externalprintf.Printf("%d", 42) // OK 257 // externalprintf.Printf("foobar") // OK 258 // level := 123 259 // externalprintf.Logf(level, "%d", 42) // OK 260 // externalprintf.Errorf(level, level, "foo %q bar", "foobar") // OK 261 // externalprintf.Logf(level, "%d") // no error "Logf format %d reads arg #1, but call has 0 args" 262 // var formatStr = "%s %s" 263 // externalprintf.Sprintf(formatStr, "a", "b") // OK 264 // externalprintf.Logf(level, formatStr, "a", "b") // OK 265 266 // user-defined Println-like functions 267 ss := &someStruct{} 268 ss.Log(someFunction, "foo") // OK 269 ss.Error(someFunction, someFunction) // OK 270 ss.Println() // OK 271 ss.Println(1.234, "foo") // OK 272 ss.Println(1, someFunction) // no error "Println arg someFunction is a func value, not called" 273 ss.log(someFunction) // OK 274 ss.log(someFunction, "bar", 1.33) // OK 275 ss.log(someFunction, someFunction) // no error "log arg someFunction is a func value, not called" 276 277 // indexed arguments 278 Printf("%d %[3]d %d %[2]d x", 1, 2, 3, 4) // OK 279 Printf("%d %[0]d %d %[2]d x", 1, 2, 3, 4) // ERROR "Printf format has invalid argument index \[0\]" 280 Printf("%d %[3]d %d %[-2]d x", 1, 2, 3, 4) // ERROR "Printf format has invalid argument index \[-2\]" 281 Printf("%d %[3]d %d %[2234234234234]d x", 1, 2, 3, 4) // ERROR "Printf format has invalid argument index \[2234234234234\]" 282 Printf("%d %[3]d %-10d %[2]d x", 1, 2, 3) // ERROR "Printf format %-10d reads arg #4, but call has 3 args" 283 Printf("%[1][3]d x", 1, 2) // ERROR "Printf format %\[1\]\[ has unknown verb \[" 284 Printf("%[1]d x", 1, 2) // OK 285 Printf("%d %[3]d %d %[2]d x", 1, 2, 3, 4, 5) // OK 286 287 // wrote Println but meant Fprintln 288 Printf("%p\n", os.Stdout) // OK 289 Println(os.Stdout, "hello") // ERROR "Println does not take io.Writer but has first arg os.Stdout" 290 291 Printf(someString(), "hello") // OK 292 293 // Printf wrappers in package log should be detected automatically 294 logpkg.Fatal("%d", 1) // ERROR "Fatal call has possible formatting directive %d" 295 logpkg.Fatalf("%d", "x") // ERROR "Fatalf format %d has arg \x22x\x22 of wrong type string" 296 logpkg.Fatalln("%d", 1) // ERROR "Fatalln call has possible formatting directive %d" 297 logpkg.Panic("%d", 1) // ERROR "Panic call has possible formatting directive %d" 298 logpkg.Panicf("%d", "x") // ERROR "Panicf format %d has arg \x22x\x22 of wrong type string" 299 logpkg.Panicln("%d", 1) // ERROR "Panicln call has possible formatting directive %d" 300 logpkg.Print("%d", 1) // ERROR "Print call has possible formatting directive %d" 301 logpkg.Printf("%d", "x") // ERROR "Printf format %d has arg \x22x\x22 of wrong type string" 302 logpkg.Println("%d", 1) // ERROR "Println call has possible formatting directive %d" 303 304 // Methods too. 305 var l *logpkg.Logger 306 l.Fatal("%d", 1) // ERROR "Fatal call has possible formatting directive %d" 307 l.Fatalf("%d", "x") // ERROR "Fatalf format %d has arg \x22x\x22 of wrong type string" 308 l.Fatalln("%d", 1) // ERROR "Fatalln call has possible formatting directive %d" 309 l.Panic("%d", 1) // ERROR "Panic call has possible formatting directive %d" 310 l.Panicf("%d", "x") // ERROR "Panicf format %d has arg \x22x\x22 of wrong type string" 311 l.Panicln("%d", 1) // ERROR "Panicln call has possible formatting directive %d" 312 l.Print("%d", 1) // ERROR "Print call has possible formatting directive %d" 313 l.Printf("%d", "x") // ERROR "Printf format %d has arg \x22x\x22 of wrong type string" 314 l.Println("%d", 1) // ERROR "Println call has possible formatting directive %d" 315 316 // Issue 26486 317 dbg("", 1) // no error "call has arguments but no formatting directive" 318 } 319 320 func someString() string { return "X" } 321 322 type someStruct struct{} 323 324 // Log is non-variadic user-define Println-like function. 325 // Calls to this func must be skipped when checking 326 // for Println-like arguments. 327 func (ss *someStruct) Log(f func(), s string) {} 328 329 // Error is variadic user-define Println-like function. 330 // Calls to this func mustn't be checked for Println-like arguments, 331 // since variadic arguments type isn't interface{}. 332 func (ss *someStruct) Error(args ...func()) {} 333 334 // Println is variadic user-defined Println-like function. 335 // Calls to this func must be checked for Println-like arguments. 336 func (ss *someStruct) Println(args ...interface{}) {} 337 338 // log is variadic user-defined Println-like function. 339 // Calls to this func must be checked for Println-like arguments. 340 func (ss *someStruct) log(f func(), args ...interface{}) {} 341 342 // A function we use as a function value; it has no other purpose. 343 func someFunction() {} 344 345 // Printf is used by the test so we must declare it. 346 func Printf(format string, args ...interface{}) { 347 fmt.Printf(format, args...) 348 } 349 350 // Println is used by the test so we must declare it. 351 func Println(args ...interface{}) { 352 fmt.Println(args...) 353 } 354 355 // printf is used by the test so we must declare it. 356 func printf(format string, args ...interface{}) { 357 fmt.Printf(format, args...) 358 } 359 360 // Errorf is used by the test for a case in which the first parameter 361 // is not a format string. 362 func Errorf(i int, format string, args ...interface{}) { 363 _ = fmt.Errorf(format, args...) 364 } 365 366 // errorf is used by the test for a case in which the function accepts multiple 367 // string parameters before variadic arguments 368 func errorf(level, format string, args ...interface{}) { 369 _ = fmt.Errorf(format, args...) 370 } 371 372 // multi is used by the test. 373 func multi() []interface{} { 374 panic("don't call - testing only") 375 } 376 377 type stringer int 378 379 func (stringer) String() string { return "string" } 380 381 type ptrStringer float64 382 383 var stringerv ptrStringer 384 385 func (*ptrStringer) String() string { 386 return "string" 387 } 388 389 func (p *ptrStringer) Warn2(x int, args ...interface{}) string { 390 return p.Warn(x, args...) 391 } 392 393 func (p *ptrStringer) Warnf2(x int, format string, args ...interface{}) string { 394 return p.Warnf(x, format, args...) 395 } 396 397 func (*ptrStringer) Warn(x int, args ...interface{}) string { 398 return "warn" 399 } 400 401 func (*ptrStringer) Warnf(x int, format string, args ...interface{}) string { 402 return "warnf" 403 } 404 405 func (p *ptrStringer) Wrap2(x int, args ...interface{}) string { 406 return p.Wrap(x, args...) 407 } 408 409 func (p *ptrStringer) Wrapf2(x int, format string, args ...interface{}) string { 410 return p.Wrapf(x, format, args...) 411 } 412 413 func (*ptrStringer) Wrap(x int, args ...interface{}) string { 414 return fmt.Sprint(args...) 415 } 416 417 func (*ptrStringer) Wrapf(x int, format string, args ...interface{}) string { 418 return fmt.Sprintf(format, args...) 419 } 420 421 func (*ptrStringer) BadWrap(x int, args ...interface{}) string { 422 return fmt.Sprint(args) // ERROR "missing ... in args forwarded to print-like function" 423 } 424 425 func (*ptrStringer) BadWrapf(x int, format string, args ...interface{}) string { 426 return fmt.Sprintf(format, args) // ERROR "missing ... in args forwarded to printf-like function" 427 } 428 429 func (*ptrStringer) WrapfFalsePositive(x int, arg1 string, arg2 ...interface{}) string { 430 return fmt.Sprintf("%s %v", arg1, arg2) 431 } 432 433 type embeddedStringer struct { 434 foo string 435 ptrStringer 436 bar int 437 } 438 439 var embeddedStringerv embeddedStringer 440 441 type notstringer struct { 442 f float64 443 } 444 445 var notstringerv notstringer 446 447 type stringerarray [4]float64 448 449 func (stringerarray) String() string { 450 return "string" 451 } 452 453 var stringerarrayv stringerarray 454 455 type notstringerarray [4]float64 456 457 var notstringerarrayv notstringerarray 458 459 var nonemptyinterface = interface { 460 f() 461 }(nil) 462 463 // A data type we can print with "%d". 464 type percentDStruct struct { 465 a int 466 b []byte 467 c *float64 468 } 469 470 var percentDV percentDStruct 471 472 // A data type we cannot print correctly with "%d". 473 type notPercentDStruct struct { 474 a int 475 b []byte 476 c bool 477 } 478 479 var notPercentDV notPercentDStruct 480 481 // A data type we can print with "%s". 482 type percentSStruct struct { 483 a string 484 b []byte 485 C stringerarray 486 } 487 488 var percentSV percentSStruct 489 490 type recursiveStringer int 491 492 func (s recursiveStringer) String() string { 493 _ = fmt.Sprintf("%d", s) 494 _ = fmt.Sprintf("%#v", s) 495 _ = fmt.Sprintf("%v", s) // ERROR "Sprintf format %v with arg s causes recursive String method call" 496 _ = fmt.Sprintf("%v", &s) // ERROR "Sprintf format %v with arg &s causes recursive String method call" 497 _ = fmt.Sprintf("%T", s) // ok; does not recursively call String 498 return fmt.Sprintln(s) // ERROR "Sprintln arg s causes recursive call to String method" 499 } 500 501 type recursivePtrStringer int 502 503 func (p *recursivePtrStringer) String() string { 504 _ = fmt.Sprintf("%v", *p) 505 _ = fmt.Sprint(&p) // ok; prints address 506 return fmt.Sprintln(p) // ERROR "Sprintln arg p causes recursive call to String method" 507 } 508 509 type BoolFormatter bool 510 511 func (*BoolFormatter) Format(fmt.State, rune) { 512 } 513 514 // Formatter with value receiver 515 type FormatterVal bool 516 517 func (FormatterVal) Format(fmt.State, rune) { 518 } 519 520 type RecursiveSlice []RecursiveSlice 521 522 var recursiveSliceV = &RecursiveSlice{} 523 524 type RecursiveMap map[int]RecursiveMap 525 526 var recursiveMapV = make(RecursiveMap) 527 528 type RecursiveStruct struct { 529 next *RecursiveStruct 530 } 531 532 var recursiveStructV = &RecursiveStruct{} 533 534 type RecursiveStruct1 struct { 535 next *RecursiveStruct2 536 } 537 538 type RecursiveStruct2 struct { 539 next *RecursiveStruct1 540 } 541 542 var recursiveStruct1V = &RecursiveStruct1{} 543 544 type unexportedInterface struct { 545 f interface{} 546 } 547 548 // Issue 17798: unexported ptrStringer cannot be formatted. 549 type unexportedStringer struct { 550 t ptrStringer 551 } 552 type unexportedStringerOtherFields struct { 553 s string 554 t ptrStringer 555 S string 556 } 557 558 // Issue 17798: unexported error cannot be formatted. 559 type unexportedError struct { 560 e error 561 } 562 type unexportedErrorOtherFields struct { 563 s string 564 e error 565 S string 566 } 567 568 type errorer struct{} 569 570 func (e errorer) Error() string { return "errorer" } 571 572 type unexportedCustomError struct { 573 e errorer 574 } 575 576 type errorInterface interface { 577 error 578 ExtraMethod() 579 } 580 581 type unexportedErrorInterface struct { 582 e errorInterface 583 } 584 585 func UnexportedStringerOrError() { 586 fmt.Printf("%s", unexportedInterface{"foo"}) // ok; prints {foo} 587 fmt.Printf("%s", unexportedInterface{3}) // ok; we can't see the problem 588 589 us := unexportedStringer{} 590 fmt.Printf("%s", us) // ERROR "Printf format %s has arg us of wrong type print.unexportedStringer" 591 fmt.Printf("%s", &us) // ERROR "Printf format %s has arg &us of wrong type [*]print.unexportedStringer" 592 593 usf := unexportedStringerOtherFields{ 594 s: "foo", 595 S: "bar", 596 } 597 fmt.Printf("%s", usf) // ERROR "Printf format %s has arg usf of wrong type print.unexportedStringerOtherFields" 598 fmt.Printf("%s", &usf) // ERROR "Printf format %s has arg &usf of wrong type [*]print.unexportedStringerOtherFields" 599 600 ue := unexportedError{ 601 e: &errorer{}, 602 } 603 fmt.Printf("%s", ue) // ERROR "Printf format %s has arg ue of wrong type print.unexportedError" 604 fmt.Printf("%s", &ue) // ERROR "Printf format %s has arg &ue of wrong type [*]print.unexportedError" 605 606 uef := unexportedErrorOtherFields{ 607 s: "foo", 608 e: &errorer{}, 609 S: "bar", 610 } 611 fmt.Printf("%s", uef) // ERROR "Printf format %s has arg uef of wrong type print.unexportedErrorOtherFields" 612 fmt.Printf("%s", &uef) // ERROR "Printf format %s has arg &uef of wrong type [*]print.unexportedErrorOtherFields" 613 614 uce := unexportedCustomError{ 615 e: errorer{}, 616 } 617 fmt.Printf("%s", uce) // ERROR "Printf format %s has arg uce of wrong type print.unexportedCustomError" 618 619 uei := unexportedErrorInterface{} 620 fmt.Printf("%s", uei) // ERROR "Printf format %s has arg uei of wrong type print.unexportedErrorInterface" 621 fmt.Println("foo\n", "bar") // not an error 622 623 fmt.Println("foo\n") // ERROR "Println arg list ends with redundant newline" 624 fmt.Println("foo\\n") // not an error 625 fmt.Println(`foo\n`) // not an error 626 627 intSlice := []int{3, 4} 628 fmt.Printf("%s", intSlice) // ERROR "Printf format %s has arg intSlice of wrong type \[\]int" 629 nonStringerArray := [1]unexportedStringer{{}} 630 fmt.Printf("%s", nonStringerArray) // ERROR "Printf format %s has arg nonStringerArray of wrong type \[1\]print.unexportedStringer" 631 fmt.Printf("%s", []stringer{3, 4}) // not an error 632 fmt.Printf("%s", [2]stringer{3, 4}) // not an error 633 } 634 635 // TODO: Disable complaint about '0' for Go 1.10. To be fixed properly in 1.11. 636 // See issues 23598 and 23605. 637 func DisableErrorForFlag0() { 638 fmt.Printf("%0t", true) 639 } 640 641 // Issue 26486. 642 func dbg(format string, args ...interface{}) { 643 if format == "" { 644 format = "%v" 645 } 646 fmt.Printf(format, args...) 647 } 648 649 func PointersToCompoundTypes() { 650 stringSlice := []string{"a", "b"} 651 fmt.Printf("%s", &stringSlice) // not an error 652 653 intSlice := []int{3, 4} 654 fmt.Printf("%s", &intSlice) // ERROR "Printf format %s has arg &intSlice of wrong type \*\[\]int" 655 656 stringArray := [2]string{"a", "b"} 657 fmt.Printf("%s", &stringArray) // not an error 658 659 intArray := [2]int{3, 4} 660 fmt.Printf("%s", &intArray) // ERROR "Printf format %s has arg &intArray of wrong type \*\[2\]int" 661 662 stringStruct := struct{ F string }{"foo"} 663 fmt.Printf("%s", &stringStruct) // not an error 664 665 intStruct := struct{ F int }{3} 666 fmt.Printf("%s", &intStruct) // ERROR "Printf format %s has arg &intStruct of wrong type \*struct{F int}" 667 668 stringMap := map[string]string{"foo": "bar"} 669 fmt.Printf("%s", &stringMap) // not an error 670 671 intMap := map[int]int{3: 4} 672 fmt.Printf("%s", &intMap) // ERROR "Printf format %s has arg &intMap of wrong type \*map\[int\]int" 673 674 type T2 struct { 675 X string 676 } 677 type T1 struct { 678 X *T2 679 } 680 fmt.Printf("%s\n", T1{&T2{"x"}}) // ERROR "Printf format %s has arg T1{&T2{.x.}} of wrong type print\.T1" 681 }