summaryrefslogtreecommitdiff
path: root/libgo/go/cmd/vet/testdata/print.go
diff options
context:
space:
mode:
Diffstat (limited to 'libgo/go/cmd/vet/testdata/print.go')
-rw-r--r--libgo/go/cmd/vet/testdata/print.go176
1 files changed, 154 insertions, 22 deletions
diff --git a/libgo/go/cmd/vet/testdata/print.go b/libgo/go/cmd/vet/testdata/print.go
index 55ab84fae76..ecafed5fa2f 100644
--- a/libgo/go/cmd/vet/testdata/print.go
+++ b/libgo/go/cmd/vet/testdata/print.go
@@ -14,6 +14,7 @@ package testdata
import (
"fmt"
. "fmt"
+ logpkg "log" // renamed to make it harder to see
"math"
"os"
"testing"
@@ -79,7 +80,7 @@ func PrintfTests() {
fmt.Printf("%G %G %G %G", 3e9, x, fslice, c)
fmt.Printf("%b %b %b %b", 3e9, x, fslice, c)
fmt.Printf("%o %o", 3, i)
- fmt.Printf("%p %p", p, nil)
+ fmt.Printf("%p", p)
fmt.Printf("%q %q %q %q", 3, i, 'x', r)
fmt.Printf("%s %s %s", "hi", s, []byte{65})
fmt.Printf("%t %t", true, b)
@@ -122,6 +123,7 @@ func PrintfTests() {
fmt.Printf("%g", imap) // ERROR "Printf format %g has arg imap of wrong type map\[int\]int"
fmt.Printf("%G", i) // ERROR "Printf format %G has arg i of wrong type int"
fmt.Printf("%o", x) // ERROR "Printf format %o has arg x of wrong type float64"
+ fmt.Printf("%p", nil) // ERROR "Printf format %p has arg nil of wrong type untyped nil"
fmt.Printf("%p", 23) // ERROR "Printf format %p has arg 23 of wrong type int"
fmt.Printf("%q", x) // ERROR "Printf format %q has arg x of wrong type float64"
fmt.Printf("%s", b) // ERROR "Printf format %s has arg b of wrong type bool"
@@ -130,8 +132,8 @@ func PrintfTests() {
fmt.Printf("%U", x) // ERROR "Printf format %U has arg x of wrong type float64"
fmt.Printf("%x", nil) // ERROR "Printf format %x has arg nil of wrong type untyped nil"
fmt.Printf("%X", 2.3) // ERROR "Printf format %X has arg 2.3 of wrong type float64"
- fmt.Printf("%s", stringerv) // ERROR "Printf format %s has arg stringerv of wrong type testdata.stringer"
- fmt.Printf("%t", stringerv) // ERROR "Printf format %t has arg stringerv of wrong type testdata.stringer"
+ fmt.Printf("%s", stringerv) // ERROR "Printf format %s has arg stringerv of wrong type testdata.ptrStringer"
+ fmt.Printf("%t", stringerv) // ERROR "Printf format %t has arg stringerv of wrong type testdata.ptrStringer"
fmt.Printf("%s", embeddedStringerv) // ERROR "Printf format %s has arg embeddedStringerv of wrong type testdata.embeddedStringer"
fmt.Printf("%t", embeddedStringerv) // ERROR "Printf format %t has arg embeddedStringerv of wrong type testdata.embeddedStringer"
fmt.Printf("%q", notstringerv) // ERROR "Printf format %q has arg notstringerv of wrong type testdata.notstringer"
@@ -147,6 +149,7 @@ func PrintfTests() {
fmt.Println() // not an error
fmt.Println("%s", "hi") // ERROR "Println call has possible formatting directive %s"
fmt.Println("%v", "hi") // ERROR "Println call has possible formatting directive %v"
+ fmt.Println("%T", "hi") // ERROR "Println call has possible formatting directive %T"
fmt.Println("0.0%") // correct (trailing % couldn't be a formatting directive)
fmt.Printf("%s", "hi", 3) // ERROR "Printf call needs 1 arg but has 2 args"
_ = fmt.Sprintf("%"+("s"), "hi", 3) // ERROR "Sprintf call needs 1 arg but has 2 args"
@@ -166,13 +169,25 @@ func PrintfTests() {
Printf("hi") // ok
const format = "%s %s\n"
Printf(format, "hi", "there")
- Printf(format, "hi") // ERROR "Printf format %s reads arg #2, but call has only 1 arg$"
- Printf("%s %d %.3v %q", "str", 4) // ERROR "Printf format %.3v reads arg #3, but call has only 2 args"
- f := new(stringer)
+ Printf(format, "hi") // ERROR "Printf format %s reads arg #2, but call has 1 arg$"
+ Printf("%s %d %.3v %q", "str", 4) // ERROR "Printf format %.3v reads arg #3, but call has 2 args"
+ f := new(ptrStringer)
f.Warn(0, "%s", "hello", 3) // ERROR "Warn call has possible formatting directive %s"
f.Warnf(0, "%s", "hello", 3) // ERROR "Warnf call needs 1 arg but has 2 args"
f.Warnf(0, "%r", "hello") // ERROR "Warnf format %r has unknown verb r"
f.Warnf(0, "%#s", "hello") // ERROR "Warnf format %#s has unrecognized flag #"
+ f.Warn2(0, "%s", "hello", 3) // ERROR "Warn2 call has possible formatting directive %s"
+ f.Warnf2(0, "%s", "hello", 3) // ERROR "Warnf2 call needs 1 arg but has 2 args"
+ f.Warnf2(0, "%r", "hello") // ERROR "Warnf2 format %r has unknown verb r"
+ f.Warnf2(0, "%#s", "hello") // ERROR "Warnf2 format %#s has unrecognized flag #"
+ f.Wrap(0, "%s", "hello", 3) // ERROR "Wrap call has possible formatting directive %s"
+ f.Wrapf(0, "%s", "hello", 3) // ERROR "Wrapf call needs 1 arg but has 2 args"
+ f.Wrapf(0, "%r", "hello") // ERROR "Wrapf format %r has unknown verb r"
+ f.Wrapf(0, "%#s", "hello") // ERROR "Wrapf format %#s has unrecognized flag #"
+ f.Wrap2(0, "%s", "hello", 3) // ERROR "Wrap2 call has possible formatting directive %s"
+ f.Wrapf2(0, "%s", "hello", 3) // ERROR "Wrapf2 call needs 1 arg but has 2 args"
+ f.Wrapf2(0, "%r", "hello") // ERROR "Wrapf2 format %r has unknown verb r"
+ f.Wrapf2(0, "%#s", "hello") // ERROR "Wrapf2 format %#s has unrecognized flag #"
fmt.Printf("%#s", FormatterVal(true)) // correct (the type is responsible for formatting)
Printf("d%", 2) // ERROR "Printf format % is missing verb at end of string"
Printf("%d", percentDV)
@@ -180,6 +195,7 @@ func PrintfTests() {
Printf("%d", notPercentDV) // ERROR "Printf format %d has arg notPercentDV of wrong type testdata.notPercentDStruct"
Printf("%d", &notPercentDV) // ERROR "Printf format %d has arg &notPercentDV of wrong type \*testdata.notPercentDStruct"
Printf("%p", &notPercentDV) // Works regardless: we print it as a pointer.
+ Printf("%q", &percentDV) // ERROR "Printf format %q has arg &percentDV of wrong type \*testdata.percentDStruct"
Printf("%s", percentSV)
Printf("%s", &percentSV)
// Good argument reorderings.
@@ -240,7 +256,7 @@ func PrintfTests() {
// Multiple string arguments before variadic args
// errorf("WARNING", "foobar") // OK
// errorf("INFO", "s=%s, n=%d", "foo", 1) // OK
- // errorf("ERROR", "%d") // no error "errorf format %d reads arg #1, but call has only 0 args"
+ // errorf("ERROR", "%d") // no error "errorf format %d reads arg #1, but call has 0 args"
// Printf from external package
// externalprintf.Printf("%d", 42) // OK
@@ -248,7 +264,7 @@ func PrintfTests() {
// level := 123
// externalprintf.Logf(level, "%d", 42) // OK
// externalprintf.Errorf(level, level, "foo %q bar", "foobar") // OK
- // externalprintf.Logf(level, "%d") // no error "Logf format %d reads arg #1, but call has only 0 args"
+ // externalprintf.Logf(level, "%d") // no error "Logf format %d reads arg #1, but call has 0 args"
// var formatStr = "%s %s"
// externalprintf.Sprintf(formatStr, "a", "b") // OK
// externalprintf.Logf(level, formatStr, "a", "b") // OK
@@ -269,9 +285,10 @@ func PrintfTests() {
Printf("%d %[0]d %d %[2]d x", 1, 2, 3, 4) // ERROR "Printf format has invalid argument index \[0\]"
Printf("%d %[3]d %d %[-2]d x", 1, 2, 3, 4) // ERROR "Printf format has invalid argument index \[-2\]"
Printf("%d %[3]d %d %[2234234234234]d x", 1, 2, 3, 4) // ERROR "Printf format has invalid argument index \[2234234234234\]"
- Printf("%d %[3]d %-10d %[2]d x", 1, 2, 3) // ERROR "Printf format %-10d reads arg #4, but call has only 3 args"
- Printf("%d %[3]d %d %[2]d x", 1, 2, 3, 4, 5) // ERROR "Printf call needs 4 args but has 5 args"
+ Printf("%d %[3]d %-10d %[2]d x", 1, 2, 3) // ERROR "Printf format %-10d reads arg #4, but call has 3 args"
Printf("%[1][3]d x", 1, 2) // ERROR "Printf format %\[1\]\[ has unknown verb \["
+ Printf("%[1]d x", 1, 2) // OK
+ Printf("%d %[3]d %d %[2]d x", 1, 2, 3, 4, 5) // OK
// wrote Println but meant Fprintln
Printf("%p\n", os.Stdout) // OK
@@ -279,6 +296,31 @@ func PrintfTests() {
Printf(someString(), "hello") // OK
+ // Printf wrappers in package log should be detected automatically
+ logpkg.Fatal("%d", 1) // ERROR "Fatal call has possible formatting directive %d"
+ logpkg.Fatalf("%d", "x") // ERROR "Fatalf format %d has arg \x22x\x22 of wrong type string"
+ logpkg.Fatalln("%d", 1) // ERROR "Fatalln call has possible formatting directive %d"
+ logpkg.Panic("%d", 1) // ERROR "Panic call has possible formatting directive %d"
+ logpkg.Panicf("%d", "x") // ERROR "Panicf format %d has arg \x22x\x22 of wrong type string"
+ logpkg.Panicln("%d", 1) // ERROR "Panicln call has possible formatting directive %d"
+ logpkg.Print("%d", 1) // ERROR "Print call has possible formatting directive %d"
+ logpkg.Printf("%d", "x") // ERROR "Printf format %d has arg \x22x\x22 of wrong type string"
+ logpkg.Println("%d", 1) // ERROR "Println call has possible formatting directive %d"
+
+ // Methods too.
+ var l *logpkg.Logger
+ l.Fatal("%d", 1) // ERROR "Fatal call has possible formatting directive %d"
+ l.Fatalf("%d", "x") // ERROR "Fatalf format %d has arg \x22x\x22 of wrong type string"
+ l.Fatalln("%d", 1) // ERROR "Fatalln call has possible formatting directive %d"
+ l.Panic("%d", 1) // ERROR "Panic call has possible formatting directive %d"
+ l.Panicf("%d", "x") // ERROR "Panicf format %d has arg \x22x\x22 of wrong type string"
+ l.Panicln("%d", 1) // ERROR "Panicln call has possible formatting directive %d"
+ l.Print("%d", 1) // ERROR "Print call has possible formatting directive %d"
+ l.Printf("%d", "x") // ERROR "Printf format %d has arg \x22x\x22 of wrong type string"
+ l.Println("%d", 1) // ERROR "Println call has possible formatting directive %d"
+
+ // Issue 26486
+ dbg("", 1) // no error "call has arguments but no formatting directive"
}
func someString() string { return "X" }
@@ -352,25 +394,65 @@ func multi() []interface{} {
panic("don't call - testing only")
}
-type stringer float64
+type stringer int
-var stringerv stringer
+func (stringer) String() string { return "string" }
-func (*stringer) String() string {
+type ptrStringer float64
+
+var stringerv ptrStringer
+
+func (*ptrStringer) String() string {
return "string"
}
-func (*stringer) Warn(int, ...interface{}) string {
+func (p *ptrStringer) Warn2(x int, args ...interface{}) string {
+ return p.Warn(x, args...)
+}
+
+func (p *ptrStringer) Warnf2(x int, format string, args ...interface{}) string {
+ return p.Warnf(x, format, args...)
+}
+
+func (*ptrStringer) Warn(x int, args ...interface{}) string {
return "warn"
}
-func (*stringer) Warnf(int, string, ...interface{}) string {
+func (*ptrStringer) Warnf(x int, format string, args ...interface{}) string {
return "warnf"
}
+func (p *ptrStringer) Wrap2(x int, args ...interface{}) string {
+ return p.Wrap(x, args...)
+}
+
+func (p *ptrStringer) Wrapf2(x int, format string, args ...interface{}) string {
+ return p.Wrapf(x, format, args...)
+}
+
+func (*ptrStringer) Wrap(x int, args ...interface{}) string {
+ return fmt.Sprint(args...)
+}
+
+func (*ptrStringer) Wrapf(x int, format string, args ...interface{}) string {
+ return fmt.Sprintf(format, args...)
+}
+
+func (*ptrStringer) BadWrap(x int, args ...interface{}) string {
+ return fmt.Sprint(args) // ERROR "missing ... in args forwarded to print-like function"
+}
+
+func (*ptrStringer) BadWrapf(x int, format string, args ...interface{}) string {
+ return fmt.Sprintf(format, args) // ERROR "missing ... in args forwarded to printf-like function"
+}
+
+func (*ptrStringer) WrapfFalsePositive(x int, arg1 string, arg2 ...interface{}) string {
+ return fmt.Sprintf("%s %v", arg1, arg2)
+}
+
type embeddedStringer struct {
foo string
- stringer
+ ptrStringer
bar int
}
@@ -440,6 +522,7 @@ type recursivePtrStringer int
func (p *recursivePtrStringer) String() string {
_ = fmt.Sprintf("%v", *p)
+ _ = fmt.Sprint(&p) // ok; prints address
return fmt.Sprintln(p) // ERROR "Sprintln arg p causes recursive call to String method"
}
@@ -478,13 +561,17 @@ type RecursiveStruct2 struct {
var recursiveStruct1V = &RecursiveStruct1{}
-// Issue 17798: unexported stringer cannot be formatted.
+type unexportedInterface struct {
+ f interface{}
+}
+
+// Issue 17798: unexported ptrStringer cannot be formatted.
type unexportedStringer struct {
- t stringer
+ t ptrStringer
}
type unexportedStringerOtherFields struct {
s string
- t stringer
+ t ptrStringer
S string
}
@@ -502,7 +589,23 @@ type errorer struct{}
func (e errorer) Error() string { return "errorer" }
+type unexportedCustomError struct {
+ e errorer
+}
+
+type errorInterface interface {
+ error
+ ExtraMethod()
+}
+
+type unexportedErrorInterface struct {
+ e errorInterface
+}
+
func UnexportedStringerOrError() {
+ fmt.Printf("%s", unexportedInterface{"foo"}) // ok; prints {foo}
+ fmt.Printf("%s", unexportedInterface{3}) // ok; we can't see the problem
+
us := unexportedStringer{}
fmt.Printf("%s", us) // ERROR "Printf format %s has arg us of wrong type testdata.unexportedStringer"
fmt.Printf("%s", &us) // ERROR "Printf format %s has arg &us of wrong type [*]testdata.unexportedStringer"
@@ -528,8 +631,37 @@ func UnexportedStringerOrError() {
fmt.Printf("%s", uef) // ERROR "Printf format %s has arg uef of wrong type testdata.unexportedErrorOtherFields"
fmt.Printf("%s", &uef) // ERROR "Printf format %s has arg &uef of wrong type [*]testdata.unexportedErrorOtherFields"
+ uce := unexportedCustomError{
+ e: errorer{},
+ }
+ fmt.Printf("%s", uce) // ERROR "Printf format %s has arg uce of wrong type testdata.unexportedCustomError"
+
+ uei := unexportedErrorInterface{}
+ fmt.Printf("%s", uei) // ERROR "Printf format %s has arg uei of wrong type testdata.unexportedErrorInterface"
fmt.Println("foo\n", "bar") // not an error
- fmt.Println("foo\n") // ERROR "Println arg list ends with redundant newline"
- fmt.Println("foo\\n") // not an error
- fmt.Println(`foo\n`) // not an error
+
+ fmt.Println("foo\n") // ERROR "Println arg list ends with redundant newline"
+ fmt.Println("foo\\n") // not an error
+ fmt.Println(`foo\n`) // not an error
+
+ intSlice := []int{3, 4}
+ fmt.Printf("%s", intSlice) // ERROR "Printf format %s has arg intSlice of wrong type \[\]int"
+ nonStringerArray := [1]unexportedStringer{{}}
+ fmt.Printf("%s", nonStringerArray) // ERROR "Printf format %s has arg nonStringerArray of wrong type \[1\]testdata.unexportedStringer"
+ fmt.Printf("%s", []stringer{3, 4}) // not an error
+ fmt.Printf("%s", [2]stringer{3, 4}) // not an error
+}
+
+// TODO: Disable complaint about '0' for Go 1.10. To be fixed properly in 1.11.
+// See issues 23598 and 23605.
+func DisableErrorForFlag0() {
+ fmt.Printf("%0t", true)
+}
+
+// Issue 26486.
+func dbg(format string, args ...interface{}) {
+ if format == "" {
+ format = "%v"
+ }
+ fmt.Printf(format, args...)
}