From 2aac974b5c7f9d6a5583498fd6585cb4571e7bf2 Mon Sep 17 00:00:00 2001 From: chavacava Date: Tue, 28 Jun 2022 17:14:26 +0200 Subject: [PATCH] fix issue #691 (#700) --- rule/exported.go | 12 +++++++++--- testdata/golint/const-block.go | 12 +++++++++--- testdata/golint/error-naming.go | 4 +++- testdata/golint/value-spec.go | 15 +++++++++++---- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/rule/exported.go b/rule/exported.go index cf2915d..b8663c4 100644 --- a/rule/exported.go +++ b/rule/exported.go @@ -251,7 +251,7 @@ func (w *lintExported) lintValueSpecDoc(vs *ast.ValueSpec, gd *ast.GenDecl, genD return } - if vs.Doc == nil && gd.Doc == nil { + if vs.Doc == nil && vs.Comment == nil && gd.Doc == nil { if genDeclMissingComments[gd] { return } @@ -274,10 +274,16 @@ func (w *lintExported) lintValueSpecDoc(vs *ast.ValueSpec, gd *ast.GenDecl, genD } // The relevant text to check will be on either vs.Doc or gd.Doc. // Use vs.Doc preferentially. - doc := vs.Doc - if doc == nil { + var doc *ast.CommentGroup + switch { + case vs.Doc != nil: + doc = vs.Doc + case vs.Comment != nil && gd.Doc == nil: + doc = vs.Comment + default: doc = gd.Doc } + prefix := name + " " s := normalizeText(doc.Text()) if !strings.HasPrefix(s, prefix) { diff --git a/testdata/golint/const-block.go b/testdata/golint/const-block.go index d4d6ec1..fcaf86f 100644 --- a/testdata/golint/const-block.go +++ b/testdata/golint/const-block.go @@ -4,11 +4,14 @@ package foo const ( + InlineComment = "ShouldBeOK" // InlineComment is a valid comment + // Prefix for something. // MATCH /comment on exported const InlineWhatever should be of the form "InlineWhatever ..."/ InlineWhatever = "blah" - Whatsit = "missing_comment" // MATCH /exported const Whatsit should have comment (or a comment on this block) or be unexported/ + Whatsit = "missing_comment" + // MATCH:13 /exported const Whatsit should have comment (or a comment on this block) or be unexported/ // We should only warn once per block for missing comments, // but always complain about malformed comments. @@ -29,8 +32,11 @@ const ( // The comment on the previous const block shouldn't flow through to here. -const UndocAgain = 6 // MATCH /exported const UndocAgain should have comment or be unexported/ +const UndocAgain = 6 + +// MATCH:35 /exported const UndocAgain should have comment or be unexported/ const ( - SomeUndocumented = 7 // MATCH /exported const SomeUndocumented should have comment (or a comment on this block) or be unexported/ + SomeUndocumented = 7 + // MATCH:40 /exported const SomeUndocumented should have comment (or a comment on this block) or be unexported/ ) diff --git a/testdata/golint/error-naming.go b/testdata/golint/error-naming.go index 11df352..e8c7bbf 100644 --- a/testdata/golint/error-naming.go +++ b/testdata/golint/error-naming.go @@ -11,7 +11,9 @@ import ( var unexp = errors.New("some unexported error") // MATCH /error var unexp should have name of the form errFoo/ // Exp ... -var Exp = errors.New("some exported error") // MATCH /error var Exp should have name of the form ErrFoo/ +var Exp = errors.New("some exported error") + +// MATCH:14 /error var Exp should have name of the form ErrFoo/ var ( e1 = fmt.Errorf("blah %d", 4) // MATCH /error var e1 should have name of the form errFoo/ diff --git a/testdata/golint/value-spec.go b/testdata/golint/value-spec.go index 4471539..ccf8274 100644 --- a/testdata/golint/value-spec.go +++ b/testdata/golint/value-spec.go @@ -24,15 +24,22 @@ type V string func (*V) H() {} // MATCH /exported method V.H should have comment or be unexported/ -var W = "foo" // MATCH /exported var W should have comment or be unexported/ +var W = "foo" -const X = "bar" // MATCH /exported const X should have comment or be unexported/ +// MATCH:27 /exported var W should have comment or be unexported/ -var Y, Z int // MATCH /exported var Z should have its own declaration/ +const X = "bar" + +// MATCH:31 /exported const X should have comment or be unexported/ + +var Y, Z int + +// MATCH:35 /exported var Z should have its own declaration/ // 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 on exported const Thing should be of the form "Thing ..."/ const Thing = "wonderful" + +// MATCH:42 /comment on exported const Thing should be of the form "Thing ..."/