1
0
mirror of https://github.com/mgechev/revive.git synced 2025-06-02 22:57:22 +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 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

View File

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