Skip to content

Commit

Permalink
fix: unnecesary alert for use-any when comments inside interface{} (#873
Browse files Browse the repository at this point in the history
)

* feat: add rule for unused import alias

* fix: rename rule

* fix: rename rule

* fix: use-any skip comments inside interface{}
  • Loading branch information
damif94 committed Aug 18, 2023
1 parent 19a95d9 commit 519ffbd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
26 changes: 22 additions & 4 deletions 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.
Expand All @@ -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)
},
Expand All @@ -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 {
Expand All @@ -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{
Expand All @@ -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
}
4 changes: 4 additions & 0 deletions testdata/use-any.go
Expand Up @@ -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() }

Expand Down

0 comments on commit 519ffbd

Please sign in to comment.