Skip to content

Commit c66b63f

Browse files
authored
dev: isolate checkDiscardHandler (#83)
1 parent 6b7f937 commit c66b63f

File tree

1 file changed

+37
-19
lines changed

1 file changed

+37
-19
lines changed

sloglint.go

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -214,25 +214,7 @@ func visit(pass *analysis.Pass, opts *Options, node ast.Node, stack []ast.Node)
214214

215215
name := fn.FullName()
216216

217-
if opts.go124 && (name == "log/slog.NewTextHandler" || name == "log/slog.NewJSONHandler") {
218-
if sel, ok := call.Args[0].(*ast.SelectorExpr); ok {
219-
if obj := pass.TypesInfo.ObjectOf(sel.Sel); obj != nil {
220-
if obj.Pkg().Name() == "io" && obj.Name() == "Discard" {
221-
pass.Report(analysis.Diagnostic{
222-
Pos: call.Pos(),
223-
Message: "use slog.DiscardHandler instead",
224-
SuggestedFixes: []analysis.SuggestedFix{{
225-
TextEdits: []analysis.TextEdit{{
226-
Pos: call.Pos(),
227-
End: call.End(),
228-
NewText: []byte("slog.DiscardHandler"),
229-
}},
230-
}},
231-
})
232-
}
233-
}
234-
}
235-
}
217+
checkDiscardHandler(opts, pass, name, call)
236218

237219
funcInfo, ok := slogFuncs[name]
238220
if !ok {
@@ -370,6 +352,42 @@ func visit(pass *analysis.Pass, opts *Options, node ast.Node, stack []ast.Node)
370352
}
371353
}
372354

355+
func checkDiscardHandler(opts *Options, pass *analysis.Pass, name string, call *ast.CallExpr) {
356+
if !opts.go124 {
357+
return
358+
}
359+
360+
if name != "log/slog.NewTextHandler" && name != "log/slog.NewJSONHandler" {
361+
return
362+
}
363+
364+
sel, ok := call.Args[0].(*ast.SelectorExpr)
365+
if !ok {
366+
return
367+
}
368+
369+
obj := pass.TypesInfo.ObjectOf(sel.Sel)
370+
if obj == nil {
371+
return
372+
}
373+
374+
if obj.Pkg().Name() != "io" || obj.Name() != "Discard" {
375+
return
376+
}
377+
378+
pass.Report(analysis.Diagnostic{
379+
Pos: call.Pos(),
380+
Message: "use slog.DiscardHandler instead",
381+
SuggestedFixes: []analysis.SuggestedFix{{
382+
TextEdits: []analysis.TextEdit{{
383+
Pos: call.Pos(),
384+
End: call.End(),
385+
NewText: []byte("slog.DiscardHandler"),
386+
}},
387+
}},
388+
})
389+
}
390+
373391
func isGlobalLoggerUsed(info *types.Info, call ast.Expr) bool {
374392
sel, ok := call.(*ast.SelectorExpr)
375393
if !ok {

0 commit comments

Comments
 (0)