Skip to content

Commit 18186f0

Browse files
committed
go/analysis/passes/slog: simplify function matching
Simplify the identification of functions and methods that need to be checked by making them more explicit. Change-Id: I81efcb763613cf10de87902c6c3bbc929d35bae5 Reviewed-on: https://go-review.googlesource.com/c/tools/+/494155 Run-TryBot: Jonathan Amsterdam <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Tim King <[email protected]> gopls-CI: kokoro <[email protected]>
1 parent 9aa9d13 commit 18186f0

File tree

1 file changed

+40
-35
lines changed

1 file changed

+40
-35
lines changed

go/analysis/passes/slog/slog.go

+40-35
Original file line numberDiff line numberDiff line change
@@ -172,50 +172,55 @@ func kvFuncSkipArgs(fn *types.Func) (int, bool) {
172172
if pkg := fn.Pkg(); pkg == nil || pkg.Path() != "log/slog" {
173173
return 0, false
174174
}
175+
var recvName string // by default a slog package function
175176
recv := fn.Type().(*types.Signature).Recv()
176-
if recv == nil {
177-
if fn.Name() == "Group" {
178-
return 0, true
177+
if recv != nil {
178+
t := recv.Type()
179+
if pt, ok := t.(*types.Pointer); ok {
180+
t = pt.Elem()
179181
}
180-
skip, ok := slogOutputFuncs[fn.Name()]
181-
return skip, ok
182-
}
183-
var recvName string
184-
if pt, ok := recv.Type().(*types.Pointer); ok {
185-
if nt, ok := pt.Elem().(*types.Named); ok {
182+
if nt, ok := t.(*types.Named); !ok {
183+
return 0, false
184+
} else {
186185
recvName = nt.Obj().Name()
187186
}
188187
}
189-
if recvName == "" {
190-
return 0, false
191-
}
192-
// The methods on *Logger include all the top-level output methods, as well as "With".
193-
if recvName == "Logger" {
194-
if fn.Name() == "With" {
195-
return 0, true
196-
}
197-
skip, ok := slogOutputFuncs[fn.Name()]
198-
return skip, ok
199-
}
200-
if recvName == "Record" && fn.Name() == "Add" {
201-
return 0, true
202-
}
203-
return 0, false
188+
skip, ok := kvFuncs[recvName][fn.Name()]
189+
return skip, ok
204190
}
205191

206-
// The names of top-level functions and *Logger methods in log/slog that take
192+
// The names of functions and methods in log/slog that take
207193
// ...any for key-value pairs, mapped to the number of initial args to skip in
208194
// order to get to the ones that match the ...any parameter.
209-
var slogOutputFuncs = map[string]int{
210-
"Debug": 1,
211-
"Info": 1,
212-
"Warn": 1,
213-
"Error": 1,
214-
"DebugCtx": 2,
215-
"InfoCtx": 2,
216-
"WarnCtx": 2,
217-
"ErrorCtx": 2,
218-
"Log": 3,
195+
// The first key is the dereferenced receiver type name, or "" for a function.
196+
var kvFuncs = map[string]map[string]int{
197+
"": map[string]int{
198+
"Debug": 1,
199+
"Info": 1,
200+
"Warn": 1,
201+
"Error": 1,
202+
"DebugCtx": 2,
203+
"InfoCtx": 2,
204+
"WarnCtx": 2,
205+
"ErrorCtx": 2,
206+
"Log": 3,
207+
"Group": 0,
208+
},
209+
"Logger": map[string]int{
210+
"Debug": 1,
211+
"Info": 1,
212+
"Warn": 1,
213+
"Error": 1,
214+
"DebugCtx": 2,
215+
"InfoCtx": 2,
216+
"WarnCtx": 2,
217+
"ErrorCtx": 2,
218+
"Log": 3,
219+
"With": 0,
220+
},
221+
"Record": map[string]int{
222+
"Add": 0,
223+
},
219224
}
220225

221226
// isMethodExpr reports whether a call is to a MethodExpr.

0 commit comments

Comments
 (0)