Skip to content

Commit ac69f13

Browse files
authored
Merge pull request #16 from gostaticanalysis/more-parse
omment: fix hasIgnoreCheck to more pares lines
2 parents de8b809 + 6483ecf commit ac69f13

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

comment.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -123,25 +123,30 @@ func (maps Maps) IgnoreLine(fset *token.FileSet, line int, check string) bool {
123123

124124
// hasIgnoreCheck returns true if the provided CommentGroup starts with a comment
125125
// of the form "//lint:ignore Check1[,Check2,...,CheckN] reason" and one of the
126-
// checks matches the provided check. The *ast.CommentGroup is checked directly
127-
// rather than using "cg.Text()" because, starting in Go 1.15, the "cg.Text()" call
128-
// no longer returns directive-style comments (see https://github.com/golang/go/issues/37974).
126+
// checks matches the provided check.
127+
//
128+
// The *ast.CommentGroup is checked directly rather than using "cg.Text()" because,
129+
// starting in Go 1.15, the "cg.Text()" call no longer returns directive-style
130+
// comments (see https://github.com/golang/go/issues/37974).
129131
func hasIgnoreCheck(cg *ast.CommentGroup, check string) bool {
130-
if !strings.HasPrefix(cg.List[0].Text, "//") {
131-
return false
132-
}
132+
for _, list := range cg.List {
133+
if !strings.HasPrefix(list.Text, "//") {
134+
continue
135+
}
133136

134-
s := strings.TrimSpace(cg.List[0].Text[2:])
135-
txt := strings.Split(s, " ")
136-
if len(txt) < 3 || txt[0] != "lint:ignore" {
137-
return false
138-
}
137+
s := strings.TrimSpace(list.Text[2:]) // list.Text[2:]: trim "//"
138+
txt := strings.Split(s, " ")
139+
if len(txt) < 3 || txt[0] != "lint:ignore" {
140+
continue
141+
}
139142

140-
checks := strings.Split(txt[1], ",")
141-
for i := range checks {
142-
if check == checks[i] {
143-
return true
143+
checks := strings.Split(txt[1], ",") // txt[1]: trim "lint:ignore"
144+
for i := range checks {
145+
if check == checks[i] {
146+
return true
147+
}
144148
}
145149
}
150+
146151
return false
147152
}

passes/commentmap/commentmap_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ func Test_Maps_Ignore(t *testing.T) {
3434
path: "notignore",
3535
found: false,
3636
},
37+
"havecomment": {
38+
path: "havecomment",
39+
found: true,
40+
},
3741
}
3842
for name, tt := range tests {
3943
name := name
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-- a.go --
2+
package havecomment
3+
4+
func _() {
5+
// var is no-op
6+
//lint:ignore check havecomment
7+
var _ = ""
8+
}
9+
10+
-- b.go --
11+
package havecomment
12+
13+
func _() {
14+
//lint:ignore check havecomment
15+
var _ = "" // var is no-op
16+
}
17+
18+
-- c.go --
19+
package havecomment
20+
21+
func _() {
22+
// var is no-op
23+
var _ = "" //lint:ignore check havecomment
24+
}

0 commit comments

Comments
 (0)