Skip to content

Commit 8c135a1

Browse files
authored
fix: do not check static-msg for With functions (#45)
1 parent b95891c commit 8c135a1

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

sloglint.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ func visit(pass *analysis.Pass, opts *Options, node ast.Node, stack []ast.Node)
223223
}
224224

225225
msgPos := funcInfo.argsPos - 1
226-
if opts.StaticMsg && !isStaticMsg(call.Args[msgPos]) {
226+
// NOTE: "With" functions have no message argument and must be skipped.
227+
if opts.StaticMsg && msgPos >= 0 && !isStaticMsg(call.Args[msgPos]) {
227228
pass.Reportf(call.Pos(), "message should be a string literal or a constant")
228229
}
229230

testdata/src/context_only_scope/context_only_scope.go

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ func tests(ctx context.Context) {
99
slog.Info("msg") // want `InfoContext should be used instead`
1010
slog.InfoContext(ctx, "msg")
1111

12+
slog.With("key", "value").Info("msg") // want `InfoContext should be used instead`
13+
slog.With("key", "value").InfoContext(ctx, "msg")
14+
1215
if true {
1316
slog.Info("msg") // want `InfoContext should be used instead`
1417
slog.InfoContext(ctx, "msg")

testdata/src/no_global_all/no_global_all.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import "log/slog"
55
var logger = slog.New(nil)
66

77
func tests() {
8-
slog.Info("msg") // want `global logger should not be used`
9-
logger.Info("msg") // want `global logger should not be used`
8+
slog.Info("msg") // want `global logger should not be used`
9+
logger.Info("msg") // want `global logger should not be used`
10+
slog.With("key", "value") // want `global logger should not be used`
11+
logger.With("key", "value") // want `global logger should not be used`
1012
}

testdata/src/static_msg/static_msg.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ func tests() {
2020
slog.Info(constMsg)
2121
slog.InfoContext(ctx, constMsg)
2222
slog.Log(ctx, slog.LevelInfo, constMsg)
23+
slog.With("key", "value").Info("msg")
2324

24-
slog.Info(fmt.Sprintf("msg")) // want `message should be a string literal or a constant`
25-
slog.InfoContext(ctx, fmt.Sprintf("msg")) // want `message should be a string literal or a constant`
26-
slog.Log(ctx, slog.LevelInfo, fmt.Sprintf("msg")) // want `message should be a string literal or a constant`
25+
slog.Info(fmt.Sprintf("msg")) // want `message should be a string literal or a constant`
26+
slog.InfoContext(ctx, fmt.Sprintf("msg")) // want `message should be a string literal or a constant`
27+
slog.Log(ctx, slog.LevelInfo, fmt.Sprintf("msg")) // want `message should be a string literal or a constant`
28+
slog.With("key", "value").Info(fmt.Sprintf("msg")) // want `message should be a string literal or a constant`
2729

2830
slog.Info(varMsg) // want `message should be a string literal or a constant`
2931
slog.InfoContext(ctx, varMsg) // want `message should be a string literal or a constant`

0 commit comments

Comments
 (0)