Skip to content

Commit 519ffbd

Browse files
authored
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{}
1 parent 19a95d9 commit 519ffbd

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

rule/use-any.go

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package rule
22

33
import (
4-
"go/ast"
5-
64
"github.com/mgechev/revive/lint"
5+
"go/ast"
76
)
87

98
// UseAnyRule lints given else constructs.
@@ -14,6 +13,7 @@ func (*UseAnyRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
1413
var failures []lint.Failure
1514

1615
walker := lintUseAny{
16+
commentPositions: getCommentsPositions(file.AST.Comments),
1717
onFailure: func(failure lint.Failure) {
1818
failures = append(failures, failure)
1919
},
@@ -30,7 +30,8 @@ func (*UseAnyRule) Name() string {
3030
}
3131

3232
type lintUseAny struct {
33-
onFailure func(lint.Failure)
33+
commentPositions []int
34+
onFailure func(lint.Failure)
3435
}
3536

3637
func (w lintUseAny) Visit(n ast.Node) ast.Visitor {
@@ -40,7 +41,13 @@ func (w lintUseAny) Visit(n ast.Node) ast.Visitor {
4041
}
4142

4243
if len(it.Methods.List) != 0 {
43-
return w // it is not and empty interface
44+
return w // it is not an empty interface
45+
}
46+
47+
for _, pos := range w.commentPositions {
48+
if pos > int(it.Pos()) && pos < int(it.End()) {
49+
return w // it is a comment inside the interface
50+
}
4451
}
4552

4653
w.onFailure(lint.Failure{
@@ -52,3 +59,14 @@ func (w lintUseAny) Visit(n ast.Node) ast.Visitor {
5259

5360
return w
5461
}
62+
63+
func getCommentsPositions(commentGroups []*ast.CommentGroup) []int {
64+
result := []int{}
65+
for _, commentGroup := range commentGroups {
66+
for _, comment := range commentGroup.List {
67+
result = append(result, int(comment.Pos()))
68+
}
69+
}
70+
71+
return result
72+
}

testdata/use-any.go

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ func any2(a int) interface{} {} // MATCH /since GO 1.18 'interface{}' can be rep
1717

1818
var ni interface{ Close() }
1919

20+
var ni interface {
21+
// Close()
22+
}
23+
2024
type nt interface{ Close() }
2125
type na = interface{ Close() }
2226

0 commit comments

Comments
 (0)