From 9e19337de9ab0344bcbd056064c70249e65d52ed Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Wed, 27 Feb 2013 15:43:33 -0800 Subject: [PATCH] cmd/vet: continue past first error Also delete bogus tests for f.pkg (does the file have a package) since all files have a package attached. The tests for pkg.types and pkg.values suffice. R=golang-dev, adg CC=golang-dev https://golang.org/cl/7418043 --- src/cmd/vet/main.go | 6 +++++- src/cmd/vet/print.go | 12 ++++-------- src/cmd/vet/taglit.go | 30 ++++++++++++++---------------- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/cmd/vet/main.go b/src/cmd/vet/main.go index 85eab788e96..8d575e20b25 100644 --- a/src/cmd/vet/main.go +++ b/src/cmd/vet/main.go @@ -160,6 +160,7 @@ func doPackageDir(directory string) { return } var names []string + names = append(names, pkg.GoFiles...) names = append(names, pkg.CgoFiles...) names = append(names, pkg.TestGoFiles...) // These are also in the "foo" package. prefixDirectory(directory, names) @@ -209,8 +210,11 @@ func doPackage(names []string) { pkg.values[x] = val } } + // By providing the Context with our own error function, it will continue + // past the first error. There is no need for that function to do anything. context := types.Context{ - Expr: exprFn, + Expr: exprFn, + Error: func(error) {}, } // Type check the package. _, err := context.Check(fs, astFiles) diff --git a/src/cmd/vet/print.go b/src/cmd/vet/print.go index 5b012027101..487ccb4149f 100644 --- a/src/cmd/vet/print.go +++ b/src/cmd/vet/print.go @@ -276,9 +276,6 @@ func (f *File) checkPrintfArg(call *ast.CallExpr, verb rune, flags []byte, argNu return } } - if f.pkg == nil { // Nothing more to do. - return - } // Verb is good. If nargs>1, we have something like %.*s and all but the final // arg must be integer. for i := 0; i < nargs-1; i++ { @@ -373,13 +370,13 @@ func (f *File) checkPrint(call *ast.CallExpr, name string, firstArg int) { // If we have a call to a method called Error that satisfies the Error interface, // then it's ok. Otherwise it's something like (*T).Error from the testing package // and we need to check it. - if name == "Error" && f.pkg != nil && f.isErrorMethodCall(call) { + if name == "Error" && f.isErrorMethodCall(call) { return } // If it's an Error call now, it's probably for printing errors. if !isLn { // Check the signature to be sure: there are niladic functions called "error". - if f.pkg == nil || firstArg != 0 || f.numArgsInSignature(call) != firstArg { + if firstArg != 0 || f.numArgsInSignature(call) != firstArg { f.Badf(call.Pos(), "no args in %s call", name) } } @@ -403,7 +400,7 @@ func (f *File) checkPrint(call *ast.CallExpr, name string, firstArg int) { } // numArgsInSignature tells how many formal arguments the function type -// being called has. Assumes type checking is on (f.pkg != nil). +// being called has. func (f *File) numArgsInSignature(call *ast.CallExpr) int { // Check the type of the function or method declaration typ := f.pkg.types[call.Fun] @@ -420,8 +417,7 @@ func (f *File) numArgsInSignature(call *ast.CallExpr) int { // isErrorMethodCall reports whether the call is of a method with signature // func Error() string -// where "string" is the universe's string type. We know the method is called "Error" -// and f.pkg is set. +// where "string" is the universe's string type. We know the method is called "Error". func (f *File) isErrorMethodCall(call *ast.CallExpr) bool { // Is it a selector expression? Otherwise it's a function call, not a method call. sel, ok := call.Fun.(*ast.SelectorExpr) diff --git a/src/cmd/vet/taglit.go b/src/cmd/vet/taglit.go index 2ae0b2ad445..0324e37b061 100644 --- a/src/cmd/vet/taglit.go +++ b/src/cmd/vet/taglit.go @@ -22,20 +22,18 @@ func (f *File) checkUntaggedLiteral(c *ast.CompositeLit) { return } - // Check that the CompositeLit's type is a slice or array (which need no tag), if possible. - if f.pkg != nil { - typ := f.pkg.types[c] - if typ != nil { - // If it's a named type, pull out the underlying type. - if namedType, ok := typ.(*types.NamedType); ok { - typ = namedType.Underlying - } - switch typ.(type) { - case *types.Slice: - return - case *types.Array: - return - } + // Check that the CompositeLit's type is a slice or array (which needs no tag), if possible. + typ := f.pkg.types[c] + if typ != nil { + // If it's a named type, pull out the underlying type. + if namedType, ok := typ.(*types.NamedType); ok { + typ = namedType.Underlying + } + switch typ.(type) { + case *types.Slice: + return + case *types.Array: + return } } @@ -69,8 +67,8 @@ func (f *File) checkUntaggedLiteral(c *ast.CompositeLit) { f.Warnf(c.Pos(), "unresolvable package for %s.%s literal", pkg.Name, s.Sel.Name) return } - typ := path + "." + s.Sel.Name - if *compositeWhiteList && untaggedLiteralWhitelist[typ] { + typeName := path + "." + s.Sel.Name + if *compositeWhiteList && untaggedLiteralWhitelist[typeName] { return }