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