Fix lint check for ValueSpec docs.
For standalone package-global var/consts, there is still a GenDecl
with no parens, and the var/const's doc comment may be attached to
that instead of to the ValueSpec.
Fixes #166.
diff --git a/lint.go b/lint.go
index 672a067..060ea5c 100644
--- a/lint.go
+++ b/lint.go
@@ -564,12 +564,13 @@
return true
}
- check(v.Name, "func")
-
thing := "func"
if v.Recv != nil {
thing = "method"
}
+
+ check(v.Name, thing)
+
checkList(v.Type.Params, thing+" parameter")
checkList(v.Type.Results, thing+" result")
case *ast.GenDecl:
@@ -835,20 +836,31 @@
return
}
- if vs.Doc == nil {
- if gd.Doc == nil && !genDeclMissingComments[gd] {
- block := ""
- if kind == "const" && gd.Lparen.IsValid() {
- block = " (or a comment on this block)"
- }
- f.errorf(vs, 1, link(docCommentsLink), category("comments"), "exported %s %s should have comment%s or be unexported", kind, name, block)
- genDeclMissingComments[gd] = true
+ if vs.Doc == nil && gd.Doc == nil {
+ if genDeclMissingComments[gd] {
+ return
}
+ block := ""
+ if kind == "const" && gd.Lparen.IsValid() {
+ block = " (or a comment on this block)"
+ }
+ f.errorf(vs, 1, link(docCommentsLink), category("comments"), "exported %s %s should have comment%s or be unexported", kind, name, block)
+ genDeclMissingComments[gd] = true
return
}
+ // If this GenDecl has parens and a comment, we don't check its comment form.
+ if gd.Lparen.IsValid() && gd.Doc != nil {
+ return
+ }
+ // The relevant text to check will be on either vs.Doc or gd.Doc.
+ // Use vs.Doc preferentially.
+ doc := vs.Doc
+ if doc == nil {
+ doc = gd.Doc
+ }
prefix := name + " "
- if !strings.HasPrefix(vs.Doc.Text(), prefix) {
- f.errorf(vs.Doc, 1, link(docCommentsLink), category("comments"), `comment on exported %s %s should be of the form "%s..."`, kind, name, prefix)
+ if !strings.HasPrefix(doc.Text(), prefix) {
+ f.errorf(doc, 1, link(docCommentsLink), category("comments"), `comment on exported %s %s should be of the form "%s..."`, kind, name, prefix)
}
}
diff --git a/testdata/4.go b/testdata/4.go
index 2303a9a..c9a6e89 100644
--- a/testdata/4.go
+++ b/testdata/4.go
@@ -32,3 +32,7 @@
// Location should be okay, since the other var name is an underscore.
var Location, _ = time.LoadLocation("Europe/Istanbul") // not Constantinople
+
+// this is improperly documented
+// MATCH /comment.*const.*Thing.*form.*"Thing ..."/
+const Thing = "wonderful"