diff --git a/rule/use-any.go b/rule/use-any.go index bdf3c936d..9c6d5ccf8 100644 --- a/rule/use-any.go +++ b/rule/use-any.go @@ -1,9 +1,8 @@ package rule import ( - "go/ast" - "github.com/mgechev/revive/lint" + "go/ast" ) // UseAnyRule lints given else constructs. @@ -14,6 +13,7 @@ func (*UseAnyRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { var failures []lint.Failure walker := lintUseAny{ + commentPositions: getCommentsPositions(file.AST.Comments), onFailure: func(failure lint.Failure) { failures = append(failures, failure) }, @@ -30,7 +30,8 @@ func (*UseAnyRule) Name() string { } type lintUseAny struct { - onFailure func(lint.Failure) + commentPositions []int + onFailure func(lint.Failure) } func (w lintUseAny) Visit(n ast.Node) ast.Visitor { @@ -40,7 +41,13 @@ func (w lintUseAny) Visit(n ast.Node) ast.Visitor { } if len(it.Methods.List) != 0 { - return w // it is not and empty interface + return w // it is not an empty interface + } + + for _, pos := range w.commentPositions { + if pos > int(it.Pos()) && pos < int(it.End()) { + return w // it is a comment inside the interface + } } w.onFailure(lint.Failure{ @@ -52,3 +59,14 @@ func (w lintUseAny) Visit(n ast.Node) ast.Visitor { return w } + +func getCommentsPositions(commentGroups []*ast.CommentGroup) []int { + result := []int{} + for _, commentGroup := range commentGroups { + for _, comment := range commentGroup.List { + result = append(result, int(comment.Pos())) + } + } + + return result +} diff --git a/testdata/use-any.go b/testdata/use-any.go index bc6e64759..a624d5ed2 100644 --- a/testdata/use-any.go +++ b/testdata/use-any.go @@ -17,6 +17,10 @@ func any2(a int) interface{} {} // MATCH /since GO 1.18 'interface{}' can be rep var ni interface{ Close() } +var ni interface { + // Close() +} + type nt interface{ Close() } type na = interface{ Close() }