Skip to content

Commit 4ee7542

Browse files
authored
fix: false positive in import-shadowing for method names (#876)
1 parent e758901 commit 4ee7542

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

rule/import-shadowing.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func (*ImportShadowingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fail
2929
failures = append(failures, failure)
3030
},
3131
alreadySeen: map[*ast.Object]struct{}{},
32+
skipIdents: map[*ast.Ident]struct{}{},
3233
}
3334

3435
ast.Walk(walker, fileAst)
@@ -62,6 +63,7 @@ type importShadowing struct {
6263
importNames map[string]struct{}
6364
onFailure func(lint.Failure)
6465
alreadySeen map[*ast.Object]struct{}
66+
skipIdents map[*ast.Ident]struct{}
6567
}
6668

6769
// Visit visits AST nodes and checks if id nodes (ast.Ident) shadow an import name
@@ -80,6 +82,10 @@ func (w importShadowing) Visit(n ast.Node) ast.Visitor {
8082
*ast.SelectorExpr, // skip analysis of selector expressions (anId.otherId): because if anId shadows an import name, it was already detected, and otherId does not shadows the import name
8183
*ast.StructType: // skip analysis of struct type because struct fields can not shadow an import name
8284
return nil
85+
case *ast.FuncDecl:
86+
if n.Recv != nil {
87+
w.skipIdents[n.Name] = struct{}{}
88+
}
8389
case *ast.Ident:
8490
if n == w.packageNameIdent {
8591
return nil // skip the ident corresponding to the package name of this file
@@ -92,11 +98,12 @@ func (w importShadowing) Visit(n ast.Node) ast.Visitor {
9298

9399
_, isImportName := w.importNames[id]
94100
_, alreadySeen := w.alreadySeen[n.Obj]
95-
if isImportName && !alreadySeen {
101+
_, skipIdent := w.skipIdents[n]
102+
if isImportName && !alreadySeen && !skipIdent {
96103
w.onFailure(lint.Failure{
97104
Confidence: 1,
98105
Node: n,
99-
Category: "namming",
106+
Category: "naming",
100107
Failure: fmt.Sprintf("The name '%s' shadows an import name", id),
101108
})
102109

testdata/import-shadowing.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ type fmt interface {} // MATCH /The name 'fmt' shadows an import name/
2323

2424
func (ast myAst) foo() {} // MATCH /The name 'ast' shadows an import name/
2525

26+
func (a myAst) fmt() { // this should be skipped (method, not a pkg func)
27+
var fmt string // MATCH /The name 'fmt' shadows an import name/
28+
}
29+
30+
func (a myAst) md5() { // this should be skipped (method, not a pkg func)
31+
strings := map[string]string{} // MATCH /The name 'strings' shadows an import name/
32+
}
33+
2634
func md5() {} // MATCH /The name 'md5' shadows an import name/
2735

2836
func bar(_ string) {}

0 commit comments

Comments
 (0)