mirror of
https://github.com/mgechev/revive.git
synced 2025-07-17 01:12:27 +02:00
fix: unnecesary alert for use-any when comments inside interface{} (#873)
* feat: add rule for unused import alias * fix: rename rule * fix: rename rule * fix: use-any skip comments inside interface{}
This commit is contained in:
@ -1,9 +1,8 @@
|
|||||||
package rule
|
package rule
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"go/ast"
|
|
||||||
|
|
||||||
"github.com/mgechev/revive/lint"
|
"github.com/mgechev/revive/lint"
|
||||||
|
"go/ast"
|
||||||
)
|
)
|
||||||
|
|
||||||
// UseAnyRule lints given else constructs.
|
// UseAnyRule lints given else constructs.
|
||||||
@ -14,6 +13,7 @@ func (*UseAnyRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
|
|||||||
var failures []lint.Failure
|
var failures []lint.Failure
|
||||||
|
|
||||||
walker := lintUseAny{
|
walker := lintUseAny{
|
||||||
|
commentPositions: getCommentsPositions(file.AST.Comments),
|
||||||
onFailure: func(failure lint.Failure) {
|
onFailure: func(failure lint.Failure) {
|
||||||
failures = append(failures, failure)
|
failures = append(failures, failure)
|
||||||
},
|
},
|
||||||
@ -30,6 +30,7 @@ func (*UseAnyRule) Name() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type lintUseAny struct {
|
type lintUseAny struct {
|
||||||
|
commentPositions []int
|
||||||
onFailure func(lint.Failure)
|
onFailure func(lint.Failure)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,7 +41,13 @@ func (w lintUseAny) Visit(n ast.Node) ast.Visitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(it.Methods.List) != 0 {
|
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{
|
w.onFailure(lint.Failure{
|
||||||
@ -52,3 +59,14 @@ func (w lintUseAny) Visit(n ast.Node) ast.Visitor {
|
|||||||
|
|
||||||
return w
|
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
testdata/use-any.go
vendored
4
testdata/use-any.go
vendored
@ -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() }
|
||||||
|
|
||||||
|
var ni interface {
|
||||||
|
// Close()
|
||||||
|
}
|
||||||
|
|
||||||
type nt interface{ Close() }
|
type nt interface{ Close() }
|
||||||
type na = interface{ Close() }
|
type na = interface{ Close() }
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user