cmd/vet: avoid panic if no valid inputs are found.

I'd like to make vet work as a filter, but passing /dev/stdin as a
command line argument doesn't work. This at least makes it not panic.

R=r
CC=golang-dev
https://golang.org/cl/11521045
This commit is contained in:
David Symonds 2013-07-22 12:43:11 +10:00
parent d203f128e2
commit 75919c8eee
1 changed files with 12 additions and 5 deletions

View File

@ -150,7 +150,9 @@ func main() {
}
return
}
doPackage(".", flag.Args())
if !doPackage(".", flag.Args()) {
warnf("no files checked")
}
os.Exit(exitCode)
}
@ -201,7 +203,8 @@ type Package struct {
}
// doPackage analyzes the single package constructed from the named files.
func doPackage(directory string, names []string) {
// It returns whether any files were checked.
func doPackage(directory string, names []string) bool {
var files []*File
var astFiles []*ast.File
fs := token.NewFileSet()
@ -210,13 +213,13 @@ func doPackage(directory string, names []string) {
if err != nil {
// Warn but continue to next package.
warnf("%s: %s", name, err)
return
return false
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
warnf("%s: %s", name, err)
return
return false
}
checkBuildTag(name, data)
var parsedFile *ast.File
@ -224,12 +227,15 @@ func doPackage(directory string, names []string) {
parsedFile, err = parser.ParseFile(fs, name, bytes.NewReader(data), 0)
if err != nil {
warnf("%s: %s", name, err)
return
return false
}
astFiles = append(astFiles, parsedFile)
}
files = append(files, &File{fset: fs, content: data, name: name, file: parsedFile})
}
if len(astFiles) == 0 {
return false
}
pkg := new(Package)
pkg.path = astFiles[0].Name.Name
pkg.files = files
@ -245,6 +251,7 @@ func doPackage(directory string, names []string) {
}
}
asmCheck(pkg)
return true
}
func visit(path string, f os.FileInfo, err error) error {