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