Skip to content

Commit c397610

Browse files
authored
fix: ignore non-static slog.Attr calls (#36)
1 parent c2aa167 commit c397610

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

sloglint.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,18 @@ func badKeyNames(info *types.Info, caseFn func(string) string, keys, attrs []ast
315315

316316
for _, attr := range attrs {
317317
var expr ast.Expr
318+
318319
switch attr := attr.(type) {
319320
case *ast.CallExpr: // e.g. slog.Int()
320321
fn := typeutil.StaticCallee(info, attr)
321-
if _, ok := attrFuncs[fn.FullName()]; ok {
322-
expr = attr.Args[0]
322+
if fn == nil {
323+
continue
324+
}
325+
if _, ok := attrFuncs[fn.FullName()]; !ok {
326+
continue
323327
}
328+
expr = attr.Args[0]
329+
324330
case *ast.CompositeLit: // slog.Attr{}
325331
switch len(attr.Elts) {
326332
case 1: // slog.Attr{Key: ...} | slog.Attr{Value: ...}
@@ -337,6 +343,7 @@ func badKeyNames(info *types.Info, caseFn func(string) string, keys, attrs []ast
337343
}
338344
}
339345
}
346+
340347
if name, ok := getKeyName(expr); ok && name != caseFn(name) {
341348
return true
342349
}

testdata/src/key_naming_case/key_naming_case.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,8 @@ func tests() {
3636
slog.Info("msg", slog.Attr{Value: slog.IntValue(1), Key: "foo-bar"}) // want `keys should be written in snake_case`
3737
slog.Info("msg", slog.Attr{Value: slog.IntValue(1), Key: kebabKey}) // want `keys should be written in snake_case`
3838
}
39+
40+
func issue35() {
41+
intAttr := slog.Int
42+
slog.Info("msg", intAttr("foo_bar", 1))
43+
}

0 commit comments

Comments
 (0)