From 2fd8de73a209a19a655e9af02136d7c26adddc85 Mon Sep 17 00:00:00 2001 From: Koichi Shiraishi Date: Tue, 9 Feb 2021 13:23:41 +0900 Subject: [PATCH 1/2] passes/commentmap: add havecomment testdata --- passes/commentmap/commentmap_test.go | 4 ++++ .../testdata/src/havecomment/havecomment.go | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 passes/commentmap/testdata/src/havecomment/havecomment.go diff --git a/passes/commentmap/commentmap_test.go b/passes/commentmap/commentmap_test.go index 6ec482e..7dc4517 100644 --- a/passes/commentmap/commentmap_test.go +++ b/passes/commentmap/commentmap_test.go @@ -34,6 +34,10 @@ func Test_Maps_Ignore(t *testing.T) { path: "notignore", found: false, }, + "havecomment": { + path: "havecomment", + found: true, + }, } for name, tt := range tests { name := name diff --git a/passes/commentmap/testdata/src/havecomment/havecomment.go b/passes/commentmap/testdata/src/havecomment/havecomment.go new file mode 100644 index 0000000..7a1dd6d --- /dev/null +++ b/passes/commentmap/testdata/src/havecomment/havecomment.go @@ -0,0 +1,24 @@ +-- a.go -- +package havecomment + +func _() { + // var is no-op + //lint:ignore check havecomment + var _ = "" +} + +-- b.go -- +package havecomment + +func _() { + //lint:ignore check havecomment + var _ = "" // var is no-op +} + +-- c.go -- +package havecomment + +func _() { + // var is no-op + var _ = "" //lint:ignore check havecomment +} From 6483ecf017e465d3ee2ef59928a02bd23945f0e6 Mon Sep 17 00:00:00 2001 From: Koichi Shiraishi Date: Wed, 10 Feb 2021 00:21:30 +0900 Subject: [PATCH 2/2] comment: fix hasIgnoreCheck to more pares lines --- comment.go | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/comment.go b/comment.go index 2fe67fa..79cb093 100644 --- a/comment.go +++ b/comment.go @@ -123,25 +123,30 @@ func (maps Maps) IgnoreLine(fset *token.FileSet, line int, check string) bool { // hasIgnoreCheck returns true if the provided CommentGroup starts with a comment // of the form "//lint:ignore Check1[,Check2,...,CheckN] reason" and one of the -// checks matches the provided check. The *ast.CommentGroup is checked directly -// rather than using "cg.Text()" because, starting in Go 1.15, the "cg.Text()" call -// no longer returns directive-style comments (see https://github.com/golang/go/issues/37974). +// checks matches the provided check. +// +// The *ast.CommentGroup is checked directly rather than using "cg.Text()" because, +// starting in Go 1.15, the "cg.Text()" call no longer returns directive-style +// comments (see https://github.com/golang/go/issues/37974). func hasIgnoreCheck(cg *ast.CommentGroup, check string) bool { - if !strings.HasPrefix(cg.List[0].Text, "//") { - return false - } + for _, list := range cg.List { + if !strings.HasPrefix(list.Text, "//") { + continue + } - s := strings.TrimSpace(cg.List[0].Text[2:]) - txt := strings.Split(s, " ") - if len(txt) < 3 || txt[0] != "lint:ignore" { - return false - } + s := strings.TrimSpace(list.Text[2:]) // list.Text[2:]: trim "//" + txt := strings.Split(s, " ") + if len(txt) < 3 || txt[0] != "lint:ignore" { + continue + } - checks := strings.Split(txt[1], ",") - for i := range checks { - if check == checks[i] { - return true + checks := strings.Split(txt[1], ",") // txt[1]: trim "lint:ignore" + for i := range checks { + if check == checks[i] { + return true + } } } + return false }