Skip to content

Commit

Permalink
Merge pull request #16 from gostaticanalysis/more-parse
Browse files Browse the repository at this point in the history
omment: fix hasIgnoreCheck to more pares lines
  • Loading branch information
tenntenn authored Mar 3, 2021
2 parents de8b809 + 6483ecf commit ac69f13
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
35 changes: 20 additions & 15 deletions comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions passes/commentmap/commentmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions passes/commentmap/testdata/src/havecomment/havecomment.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit ac69f13

Please sign in to comment.