From ee3eeefaa3ab42cfc301dcff2c06c8bb89c86f12 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Mon, 24 Feb 2014 11:39:42 -0800 Subject: [PATCH] cmd/vet: check for nil result set in isStringer Fixes golang/go#7149. LGTM=gri R=golang-codereviews, gri CC=golang-codereviews https://golang.org/cl/68260044 --- cmd/vet/doc.go | 2 +- cmd/vet/main.go | 5 +++-- cmd/vet/testdata/print.go | 5 +++++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/cmd/vet/doc.go b/cmd/vet/doc.go index 877aa884..834c5612 100644 --- a/cmd/vet/doc.go +++ b/cmd/vet/doc.go @@ -22,7 +22,7 @@ vets the files named, all of which must be in the same package. By directory: go tool vet source/directory recursively descends the directory, vetting each file in isolation. -Package-level type-checking disabled, so the vetting is weaker. +Package-level type-checking is disabled, so the vetting is weaker. Vet's exit code is 2 for erroneous invocation of the tool, 1 if a problem was reported, and 0 otherwise. Note that the tool does not diff --git a/cmd/vet/main.go b/cmd/vet/main.go index 2f3307c9..b9d80107 100644 --- a/cmd/vet/main.go +++ b/cmd/vet/main.go @@ -76,7 +76,8 @@ func Usage() { fmt.Fprintf(os.Stderr, "\tvet [flags] directory...\n") fmt.Fprintf(os.Stderr, "\tvet [flags] files... # Must be a single package\n") fmt.Fprintf(os.Stderr, "For more information run\n") - fmt.Fprintf(os.Stderr, "\tgodoc code.google.com/p/go.tools/cmd/vet\n\nFlags:\n") + fmt.Fprintf(os.Stderr, "\tgodoc code.google.com/p/go.tools/cmd/vet\n\n") + fmt.Fprintf(os.Stderr, "Flags:\n") flag.PrintDefaults() os.Exit(2) } @@ -457,7 +458,7 @@ func (f *File) prepStringerReceiver(d *ast.FuncDecl) { // isStringer returns true if the provided declaration is a "String() string" // method; an implementation of fmt.Stringer. func (f *File) isStringer(d *ast.FuncDecl) bool { - return d.Recv != nil && d.Name.Name == "String" && + return d.Recv != nil && d.Name.Name == "String" && d.Type.Results != nil && len(d.Type.Params.List) == 0 && len(d.Type.Results.List) == 1 && f.pkg.types[d.Type.Results.List[0].Type].Type == types.Typ[types.String] } diff --git a/cmd/vet/testdata/print.go b/cmd/vet/testdata/print.go index 232c7b3b..ee8d2203 100644 --- a/cmd/vet/testdata/print.go +++ b/cmd/vet/testdata/print.go @@ -323,3 +323,8 @@ type RecursiveStruct2 struct { } var recursiveStruct1V = &RecursiveStruct1{} + +// Fix for issue 7149: Missing return type on String method caused fault. +func (int) String() { + return "" +}