Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false positive confusing-naming error on generics with pointer type receiver #985

Merged
merged 1 commit into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions rule/confusing-naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,32 @@ func getStructName(r *ast.FieldList) string {

switch v := t.(type) {
case *ast.StarExpr:
t = v.X
return extractFromStarExpr(v)
case *ast.IndexExpr:
t = v.X
return extractFromIndexExpr(v)
case *ast.Ident:
return v.Name
}

if p, _ := t.(*ast.Ident); p != nil {
result = p.Name
return defaultStructName
}

func extractFromStarExpr(expr *ast.StarExpr) string {
switch v := expr.X.(type) {
case *ast.IndexExpr:
return extractFromIndexExpr(v)
case *ast.Ident:
return v.Name
}
return defaultStructName
}

return result
func extractFromIndexExpr(expr *ast.IndexExpr) string {
switch v := expr.X.(type) {
case *ast.Ident:
return v.Name
}
return defaultStructName
}

func checkStructFields(fields *ast.FieldList, structName string, w *lintConfusingNames) {
Expand Down
11 changes: 11 additions & 0 deletions testdata/confusing-naming1.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,14 @@ type y[T any] struct{}

func (y[T]) method() {
}

// issue #982
type a[T any] struct{}

func (x *a[T]) method() {
}

type b[T any] struct{}

func (x *b[T]) method() {
}
Loading