diff --git a/sloglint.go b/sloglint.go index 35cac14..ac4fe87 100644 --- a/sloglint.go +++ b/sloglint.go @@ -315,12 +315,18 @@ func badKeyNames(info *types.Info, caseFn func(string) string, keys, attrs []ast for _, attr := range attrs { var expr ast.Expr + switch attr := attr.(type) { case *ast.CallExpr: // e.g. slog.Int() fn := typeutil.StaticCallee(info, attr) - if _, ok := attrFuncs[fn.FullName()]; ok { - expr = attr.Args[0] + if fn == nil { + continue + } + if _, ok := attrFuncs[fn.FullName()]; !ok { + continue } + expr = attr.Args[0] + case *ast.CompositeLit: // slog.Attr{} switch len(attr.Elts) { case 1: // slog.Attr{Key: ...} | slog.Attr{Value: ...} @@ -337,6 +343,7 @@ func badKeyNames(info *types.Info, caseFn func(string) string, keys, attrs []ast } } } + if name, ok := getKeyName(expr); ok && name != caseFn(name) { return true } diff --git a/testdata/src/key_naming_case/key_naming_case.go b/testdata/src/key_naming_case/key_naming_case.go index 36b6374..4011565 100644 --- a/testdata/src/key_naming_case/key_naming_case.go +++ b/testdata/src/key_naming_case/key_naming_case.go @@ -36,3 +36,8 @@ func tests() { slog.Info("msg", slog.Attr{Value: slog.IntValue(1), Key: "foo-bar"}) // want `keys should be written in snake_case` slog.Info("msg", slog.Attr{Value: slog.IntValue(1), Key: kebabKey}) // want `keys should be written in snake_case` } + +func issue35() { + intAttr := slog.Int + slog.Info("msg", intAttr("foo_bar", 1)) +}