Skip to content

Commit dcc42c7

Browse files
valyalarobpike
authored andcommitted
cmd/vet: do not check print-like functions with unknown type
Fixes #15787 Change-Id: I559ba886527b474dbdd44fe884c78973b3012377 Reviewed-on: https://go-review.googlesource.com/23351 Run-TryBot: Rob Pike <[email protected]> Reviewed-by: Rob Pike <[email protected]>
1 parent 524956f commit dcc42c7

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

src/cmd/vet/print.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -587,22 +587,24 @@ func (f *File) argCanBeChecked(call *ast.CallExpr, formatArg int, isStar bool, s
587587
func (f *File) checkPrint(call *ast.CallExpr, name string) {
588588
firstArg := 0
589589
typ := f.pkg.types[call.Fun].Type
590-
if typ != nil {
591-
if sig, ok := typ.(*types.Signature); ok {
592-
if !sig.Variadic() {
593-
// Skip checking non-variadic functions.
594-
return
595-
}
596-
params := sig.Params()
597-
firstArg = params.Len() - 1
598-
599-
typ := params.At(firstArg).Type()
600-
typ = typ.(*types.Slice).Elem()
601-
it, ok := typ.(*types.Interface)
602-
if !ok || !it.Empty() {
603-
// Skip variadic functions accepting non-interface{} args.
604-
return
605-
}
590+
if typ == nil {
591+
// Skip checking functions with unknown type.
592+
return
593+
}
594+
if sig, ok := typ.(*types.Signature); ok {
595+
if !sig.Variadic() {
596+
// Skip checking non-variadic functions.
597+
return
598+
}
599+
params := sig.Params()
600+
firstArg = params.Len() - 1
601+
602+
typ := params.At(firstArg).Type()
603+
typ = typ.(*types.Slice).Elem()
604+
it, ok := typ.(*types.Interface)
605+
if !ok || !it.Empty() {
606+
// Skip variadic functions accepting non-interface{} args.
607+
return
606608
}
607609
}
608610
args := call.Args

src/cmd/vet/testdata/print.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package testdata
88

99
import (
1010
"fmt"
11+
"io"
1112
"math"
1213
"os"
1314
"unsafe" // just for test case printing unsafe.Pointer
@@ -272,11 +273,21 @@ func Printf(format string, args ...interface{}) {
272273
panic("don't call - testing only")
273274
}
274275

276+
// Println is used by the test so we must declare it.
277+
func Println(args ...interface{}) {
278+
panic("don't call - testing only")
279+
}
280+
275281
// Logf is used by the test so we must declare it.
276282
func Logf(format string, args ...interface{}) {
277283
panic("don't call - testing only")
278284
}
279285

286+
// Log is used by the test so we must declare it.
287+
func Log(args ...interface{}) {
288+
panic("don't call - testing only")
289+
}
290+
280291
// printf is used by the test so we must declare it.
281292
func printf(format string, args ...interface{}) {
282293
panic("don't call - testing only")
@@ -415,3 +426,10 @@ var recursiveStruct1V = &RecursiveStruct1{}
415426
func (int) String() {
416427
return ""
417428
}
429+
430+
func (s *unknownStruct) Fprintln(w io.Writer, s string) {}
431+
432+
func UnknownStructFprintln() {
433+
s := unknownStruct{}
434+
s.Fprintln(os.Stdout, "hello, world!") // OK
435+
}

0 commit comments

Comments
 (0)