Skip to content

Commit 878f379

Browse files
authored
fix: handle unpacked slice (#51)
1 parent 8307697 commit 878f379

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

sloglint.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -242,21 +242,24 @@ func visit(pass *analysis.Pass, opts *Options, node ast.Node, stack []ast.Node)
242242
if typ == nil {
243243
continue
244244
}
245+
245246
switch typ.String() {
246247
case "string":
247248
keys = append(keys, args[i])
248249
i++ // skip the value.
249250
case "log/slog.Attr":
250251
attrs = append(attrs, args[i])
252+
case "[]any", "[]log/slog.Attr":
253+
continue // the last argument may be an unpacked slice, skip it.
251254
}
252255
}
253256

254257
switch {
255258
case opts.KVOnly && len(attrs) > 0:
256259
pass.Reportf(call.Pos(), "attributes should not be used")
257-
case opts.AttrOnly && len(attrs) < len(args):
260+
case opts.AttrOnly && len(keys) > 0:
258261
pass.Reportf(call.Pos(), "key-value pairs should not be used")
259-
case opts.NoMixedArgs && 0 < len(attrs) && len(attrs) < len(args):
262+
case opts.NoMixedArgs && len(attrs) > 0 && len(keys) > 0:
260263
pass.Reportf(call.Pos(), "key-value pairs and attributes should not be mixed")
261264
}
262265

testdata/src/attr_only/attr_only.go

+3
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@ func tests() {
1212
slog.Info("msg", "foo", 1, "bar", 2) // want `key-value pairs should not be used`
1313
slog.Info("msg", "foo", 1, slog.Int("bar", 2)) // want `key-value pairs should not be used`
1414
slog.With("foo", 1, slog.Int("bar", 2)).Info("msg") // want `key-value pairs should not be used`
15+
16+
args := []slog.Attr{slog.Int("foo", 1), slog.Int("bar", 2)}
17+
slog.LogAttrs(nil, slog.LevelInfo, "msg", args...)
1518
}

testdata/src/kv_only/kv_only.go

+3
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ func tests() {
1515
slog.With(slog.Int("foo", 1)).Info("msg") // want `attributes should not be used`
1616
slog.With(slog.Int("foo", 1), slog.Int("bar", 2)).Info("msg") // want `attributes should not be used`
1717
slog.With("foo", 1, slog.Int("bar", 2)).Info("msg") // want `attributes should not be used`
18+
19+
args := []any{"foo", 1, "bar", 2}
20+
slog.Log(nil, slog.LevelInfo, "msg", args...)
1821
}

0 commit comments

Comments
 (0)