Skip to content

Commit cfac8b0

Browse files
authored
fix 534 (#536)
Closes #534
1 parent 1d91f74 commit cfac8b0

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

rule/import-shadowing.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ func (r *ImportShadowingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fa
2323

2424
fileAst := file.AST
2525
walker := importShadowing{
26-
importNames: importNames,
26+
packageNameIdent: fileAst.Name,
27+
importNames: importNames,
2728
onFailure: func(failure lint.Failure) {
2829
failures = append(failures, failure)
2930
},
@@ -57,9 +58,10 @@ func getName(imp *ast.ImportSpec) string {
5758
}
5859

5960
type importShadowing struct {
60-
importNames map[string]struct{}
61-
onFailure func(lint.Failure)
62-
alreadySeen map[*ast.Object]struct{}
61+
packageNameIdent *ast.Ident
62+
importNames map[string]struct{}
63+
onFailure func(lint.Failure)
64+
alreadySeen map[*ast.Object]struct{}
6365
}
6466

6567
// Visit visits AST nodes and checks if id nodes (ast.Ident) shadow an import name
@@ -79,6 +81,10 @@ func (w importShadowing) Visit(n ast.Node) ast.Visitor {
7981
*ast.StructType: // skip analysis of struct type because struct fields can not shadow an import name
8082
return nil
8183
case *ast.Ident:
84+
if n == w.packageNameIdent {
85+
return nil // skip the ident corresponding to the package name of this file
86+
}
87+
8288
id := n.Name
8389
if id == "_" {
8490
return w // skip _ id

testdata/import-shadowing.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
_ "net/http"
99
"strings"
1010
str "strings"
11+
"fixtures" // Test case for issue #534
1112
)
1213

1314
const str = "" // MATCH /The name 'str' shadows an import name/

0 commit comments

Comments
 (0)