1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
package rule
import (
"fmt"
"go/ast"
"strings"
"sync"
"github.com/mgechev/revive/lint"
)
type referenceMethod struct {
fileName string
id *ast.Ident
}
type pkgMethods struct {
pkg *lint.Package
methods map[string]map[string]*referenceMethod
mu *sync.Mutex
}
type packages struct {
pkgs []pkgMethods
mu sync.Mutex
}
func (ps *packages) methodNames(lp *lint.Package) pkgMethods {
ps.mu.Lock()
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
}
var allPkgs = packages{pkgs: make([]pkgMethods, 1)}
// ConfusingNamingRule lints method names that differ only by capitalization
type ConfusingNamingRule struct{}
// Apply applies the rule to given file.
func (r *ConfusingNamingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
var failures []lint.Failure
fileAst := file.AST
pkgm := allPkgs.methodNames(file.Pkg)
walker := lintConfusingNames{
fileName: file.Name,
pkgm: pkgm,
onFailure: func(failure lint.Failure) {
failures = append(failures, failure)
},
}
ast.Walk(&walker, fileAst)
return failures
}
// Name returns the rule name.
func (r *ConfusingNamingRule) Name() string {
return "confusing-naming"
}
//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
}
pkgm := w.pkgm
name := strings.ToUpper(id.Name)
pkgm.mu.Lock()
defer pkgm.mu.Unlock()
if pkgm.methods[holder] != nil {
if pkgm.methods[holder][name] != nil {
refMethod := pkgm.methods[holder][name]
// confusing names
var kind string
if holder == defaultStructName {
kind = "function"
} else {
kind = "method"
}
var fileName string
if w.fileName == refMethod.fileName {
fileName = "the same source file"
} else {
fileName = refMethod.fileName
}
w.onFailure(lint.Failure{
Failure: fmt.Sprintf("Method '%s' differs only by capitalization to %s '%s' in %s", id.Name, kind, refMethod.id.Name, fileName),
Confidence: 1,
Node: id,
Category: "naming",
})
return
}
} else {
pkgm.methods[holder] = make(map[string]*referenceMethod, 1)
}
// update the black list
if pkgm.methods[holder] == nil {
println("no entry for '", holder, "'")
}
pkgm.methods[holder][name] = &referenceMethod{fileName: w.fileName, id: id}
}
type lintConfusingNames struct {
fileName string
pkgm pkgMethods
onFailure func(lint.Failure)
}
const defaultStructName = "_" // used to map functions
//getStructName of a function receiver. Defaults to defaultStructName
func getStructName(r *ast.FieldList) string {
result := defaultStructName
if r == nil || len(r.List) < 1 {
return result
}
t := r.List[0].Type
if p, _ := t.(*ast.StarExpr); p != nil { // if a pointer receiver => dereference pointer receiver types
t = p.X
}
if p, _ := t.(*ast.Ident); p != nil {
result = p.Name
}
return result
}
func checkStructFields(fields *ast.FieldList, structName string, w *lintConfusingNames) {
bl := make(map[string]bool, len(fields.List))
for _, f := range fields.List {
for _, id := range f.Names {
normName := strings.ToUpper(id.Name)
if bl[normName] {
w.onFailure(lint.Failure{
Failure: fmt.Sprintf("Field '%s' differs only by capitalization to other field in the struct type %s", id.Name, structName),
Confidence: 1,
Node: id,
Category: "naming",
})
} else {
bl[normName] = true
}
}
}
}
func (w *lintConfusingNames) Visit(n ast.Node) ast.Visitor {
switch v := n.(type) {
case *ast.FuncDecl:
// Exclude naming warnings for functions that are exported to C but
// not exported in the Go API.
// See https://github.com/golang/lint/issues/144.
if ast.IsExported(v.Name.Name) || !isCgoExported(v) {
checkMethodName(getStructName(v.Recv), v.Name, w)
}
case *ast.TypeSpec:
if s, ok := v.Type.(*ast.StructType); ok {
checkStructFields(s.Fields, v.Name.Name, w)
}
default:
// will add other checks like field names, struct names, etc.
}
return w
}
|