1
0
mirror of https://github.com/mgechev/revive.git synced 2025-05-31 22:49:41 +02:00
Closes #534
This commit is contained in:
SalvadorC 2021-06-29 21:40:40 +02:00 committed by GitHub
parent 1d91f74b14
commit cfac8b0013
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 4 deletions

View File

@ -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

View File

@ -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/