mirror of
https://github.com/mgechev/revive.git
synced 2025-06-02 22:57:22 +02:00
parent
1d91f74b14
commit
cfac8b0013
@ -23,7 +23,8 @@ func (r *ImportShadowingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Fa
|
|||||||
|
|
||||||
fileAst := file.AST
|
fileAst := file.AST
|
||||||
walker := importShadowing{
|
walker := importShadowing{
|
||||||
importNames: importNames,
|
packageNameIdent: fileAst.Name,
|
||||||
|
importNames: importNames,
|
||||||
onFailure: func(failure lint.Failure) {
|
onFailure: func(failure lint.Failure) {
|
||||||
failures = append(failures, failure)
|
failures = append(failures, failure)
|
||||||
},
|
},
|
||||||
@ -57,9 +58,10 @@ func getName(imp *ast.ImportSpec) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type importShadowing struct {
|
type importShadowing struct {
|
||||||
importNames map[string]struct{}
|
packageNameIdent *ast.Ident
|
||||||
onFailure func(lint.Failure)
|
importNames map[string]struct{}
|
||||||
alreadySeen map[*ast.Object]struct{}
|
onFailure func(lint.Failure)
|
||||||
|
alreadySeen map[*ast.Object]struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Visit visits AST nodes and checks if id nodes (ast.Ident) shadow an import name
|
// 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 {
|
|||||||
*ast.StructType: // skip analysis of struct type because struct fields can not shadow an import name
|
*ast.StructType: // skip analysis of struct type because struct fields can not shadow an import name
|
||||||
return nil
|
return nil
|
||||||
case *ast.Ident:
|
case *ast.Ident:
|
||||||
|
if n == w.packageNameIdent {
|
||||||
|
return nil // skip the ident corresponding to the package name of this file
|
||||||
|
}
|
||||||
|
|
||||||
id := n.Name
|
id := n.Name
|
||||||
if id == "_" {
|
if id == "_" {
|
||||||
return w // skip _ id
|
return w // skip _ id
|
||||||
|
1
testdata/import-shadowing.go
vendored
1
testdata/import-shadowing.go
vendored
@ -8,6 +8,7 @@ import (
|
|||||||
_ "net/http"
|
_ "net/http"
|
||||||
"strings"
|
"strings"
|
||||||
str "strings"
|
str "strings"
|
||||||
|
"fixtures" // Test case for issue #534
|
||||||
)
|
)
|
||||||
|
|
||||||
const str = "" // MATCH /The name 'str' shadows an import name/
|
const str = "" // MATCH /The name 'str' shadows an import name/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user