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
|
package main
import (
"go/ast"
"golang.org/x/tools/go/analysis"
)
func checkNotLog(pass *analysis.Pass, node *ast.ExprStmt) {
prevCall, ok := node.X.(*ast.CallExpr)
if !ok {
return
}
selector, ok := prevCall.Fun.(*ast.SelectorExpr)
if !ok {
return
}
switch {
case isIdent(selector.X, "slog"):
case isIdent(selector.X, "logger"):
pass.Reportf(node.Pos(), "no log before panic")
}
}
func checkLogBeforePanic(pass *analysis.Pass, node *ast.BlockStmt) {
for i, stmt := range node.List {
expr, ok := stmt.(*ast.ExprStmt)
if !ok {
continue
}
call, ok := expr.X.(*ast.CallExpr)
if !ok {
continue
}
if i > 0 && isIdent(call.Fun, "panic") {
if prev, ok := node.List[i-1].(*ast.ExprStmt); ok {
checkNotLog(pass, prev)
}
}
}
}
func checkLogWithArgs(node *ast.CallExpr, pass *analysis.Pass) {
for _, arg := range node.Args {
call, ok := arg.(*ast.CallExpr)
if !ok {
continue
}
if selector, ok := call.Fun.(*ast.SelectorExpr); ok {
ident, ok := selector.X.(*ast.Ident)
if ok && ident.Name != "slog" {
pass.Reportf(node.Pos(), "slog.With and logger.With must be called with a slog arg")
}
} else {
pass.Reportf(node.Pos(), "slog.With and logger.With must be called with a slog arg")
}
}
}
func checkLogUsage(pass *analysis.Pass, node *ast.CallExpr) {
if selector, ok := node.Fun.(*ast.SelectorExpr); ok {
if ident, ok := selector.X.(*ast.Ident); ok {
if ident.Name == "log" {
pass.Reportf(node.Pos(), "found old log usage")
}
}
}
if isSelector(node.Fun, "slog", "With") || isSelector(node.Fun, "logger", "With") {
checkLogWithArgs(node, pass)
}
}
|