Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions sloglint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...}
Expand All @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions testdata/src/key_naming_case/key_naming_case.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}