forked from Onyx-Protocol/Onyx
/
logparity.go
44 lines (37 loc) · 983 Bytes
/
logparity.go
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
// This file contains the logparity checker.
package main
import (
"go/ast"
"go/types"
)
func init() {
register("logparity",
"check parity of key-value args in log invocations",
checkLogCall,
callExpr)
}
// checkCall triggers the print-specific checks if the call invokes a print function.
func checkLogCall(f *File, node ast.Node) {
call, ok := node.(*ast.CallExpr)
if !ok {
return
}
sel, ok := call.Fun.(*ast.SelectorExpr)
if !ok {
return
}
fun, ok := f.pkg.uses[sel.Sel].(*types.Func)
if !ok {
return
}
if fun.FullName() != "chain/log.Write" && fun.FullName() != "chain/log.Fatal" {
return
}
// Form of arguments is (ctx, key1, val1, key2, val2, ...)
narg := len(call.Args) - 1 // Subtract one for the context.
if narg%2 == 1 && call.Ellipsis == 0 {
f.Badf(call.Pos(), "odd number of arguments in call to %s.%s", sel.X, sel.Sel)
}
// TODO(kr): perhaps check the type of the keys
// and limit them to string or fmt.Stringer or similar?
}