aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/vet/main.go
diff options
context:
space:
mode:
authorDaniel Martí <mvdan@mvdan.cc>2018-05-04 11:22:53 +0700
committerDaniel Martí <mvdan@mvdan.cc>2018-05-29 17:01:58 +0000
commit07d384b9de6b58bfe8cf9c6543179654353944b4 (patch)
treeea10a07a8a6062d7fcab7cc63fe46f8e25963d31 /src/cmd/vet/main.go
parentd15d0550544fc6392ff58a99939eeb907a823737 (diff)
downloadgo-07d384b9de6b58bfe8cf9c6543179654353944b4.tar.gz
go-07d384b9de6b58bfe8cf9c6543179654353944b4.zip
cmd/vet: avoid false positives with non-comments
vet's buildtag check looks for malformed build tag comments. Since these can appear in Go files as well as non-Go files (such as assembly files), it must read the file line by line instead of using go/token or go/ast directly. However, this method runs into false positives if there are any lines in the code that look like comments, but are not. For example: $ cat f.go package main const foo = ` //+build ignore ` $ go vet f.go ./f.go:3: +build comment must appear before package clause and be followed by a blank line This bug has been popping up more frequently since vet started being run with go test, so it is important to make the check as precise as possible. To avoid the false positive, when checking a Go file, cross-check that a line that looks like a comment actually corresponds to a comment in the go/ast syntax tree. Since vet already obtains the syntax trees for all the Go files, it checks, this change means very little extra work for the check. While at it, add a badf helper function to simplify the code that reports warnings in the buildtag check. Fixes #13533. Change-Id: I484a16da01363b409ec418c313634171bf85250b Reviewed-on: https://go-review.googlesource.com/111415 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/cmd/vet/main.go')
-rw-r--r--src/cmd/vet/main.go9
1 files changed, 5 insertions, 4 deletions
diff --git a/src/cmd/vet/main.go b/src/cmd/vet/main.go
index 4422add72f..50af846c59 100644
--- a/src/cmd/vet/main.go
+++ b/src/cmd/vet/main.go
@@ -415,23 +415,24 @@ func doPackage(names []string, basePkg *Package) *Package {
warnf("%s: %s", name, err)
return nil
}
- checkBuildTag(name, data)
var parsedFile *ast.File
if strings.HasSuffix(name, ".go") {
- parsedFile, err = parser.ParseFile(fs, name, data, 0)
+ parsedFile, err = parser.ParseFile(fs, name, data, parser.ParseComments)
if err != nil {
warnf("%s: %s", name, err)
return nil
}
astFiles = append(astFiles, parsedFile)
}
- files = append(files, &File{
+ file := &File{
fset: fs,
content: data,
name: name,
file: parsedFile,
dead: make(map[ast.Node]bool),
- })
+ }
+ checkBuildTag(file)
+ files = append(files, file)
}
if len(astFiles) == 0 {
return nil