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