Skip to content

Commit

Permalink
fix: ignore non-static slog.Attr calls (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmzane committed Apr 12, 2024
1 parent c2aa167 commit c397610
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
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))
}

0 comments on commit c397610

Please sign in to comment.