Skip to content

Commit

Permalink
fix #864: confusing-naming FP on methods of generic types
Browse files Browse the repository at this point in the history
  • Loading branch information
chavacava committed Aug 12, 2023
1 parent 7cb4540 commit f4239b8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
11 changes: 7 additions & 4 deletions rule/confusing-naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,17 @@ 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
}
}

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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 11 additions & 1 deletion testdata/confusing-naming1.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Test of confusing-naming rule.

// Package pkg ...
package pkg

Expand Down Expand Up @@ -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() {
}

0 comments on commit f4239b8

Please sign in to comment.