From 16871ed55f51d0dca10bbf8894cb53b432a56e22 Mon Sep 17 00:00:00 2001 From: chavacava Date: Sat, 12 Aug 2023 10:56:53 +0200 Subject: [PATCH] fix #864: confusing-naming FP on methods of generic types (#869) --- rule/confusing-naming.go | 11 +++++++---- testdata/confusing-naming1.go | 12 +++++++++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/rule/confusing-naming.go b/rule/confusing-naming.go index 34cdb90..8b1c3ea 100644 --- a/rule/confusing-naming.go +++ b/rule/confusing-naming.go @@ -27,10 +27,10 @@ type packages struct { func (ps *packages) methodNames(lp *lint.Package) pkgMethods { ps.mu.Lock() + defer ps.mu.Unlock() for _, pkg := range ps.pkgs { if pkg.pkg == lp { - ps.mu.Unlock() return pkg } } @@ -38,7 +38,6 @@ func (ps *packages) methodNames(lp *lint.Package) pkgMethods { pkgm := pkgMethods{pkg: lp, methods: make(map[string]map[string]*referenceMethod), mu: &sync.Mutex{}} ps.pkgs = append(ps.pkgs, pkgm) - ps.mu.Unlock() return pkgm } @@ -72,6 +71,7 @@ func (*ConfusingNamingRule) Name() string { // checkMethodName checks if a given method/function name is similar (just case differences) to other method/function of the same struct/file. func checkMethodName(holder string, id *ast.Ident, w *lintConfusingNames) { + if id.Name == "init" && holder == defaultStructName { // ignore init functions return @@ -137,8 +137,11 @@ func getStructName(r *ast.FieldList) string { t := r.List[0].Type - if p, _ := t.(*ast.StarExpr); p != nil { // if a pointer receiver => dereference pointer receiver types - t = p.X + switch v := t.(type) { + case *ast.StarExpr: + t = v.X + case *ast.IndexExpr: + t = v.X } if p, _ := t.(*ast.Ident); p != nil { diff --git a/testdata/confusing-naming1.go b/testdata/confusing-naming1.go index ba12b07..5b06bac 100644 --- a/testdata/confusing-naming1.go +++ b/testdata/confusing-naming1.go @@ -1,5 +1,4 @@ // Test of confusing-naming rule. - // Package pkg ... package pkg @@ -60,3 +59,14 @@ type tBar struct { qwe bool zxc float32 } + +// issue #864 +type x[T any] struct{} + +func (x[T]) method() { +} + +type y[T any] struct{} + +func (y[T]) method() { +}