Skip to content

Commit

Permalink
fix: do not check static-msg for With functions (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
tigrato authored May 27, 2024
1 parent b95891c commit 8c135a1
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
3 changes: 2 additions & 1 deletion sloglint.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ func visit(pass *analysis.Pass, opts *Options, node ast.Node, stack []ast.Node)
}

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

Expand Down
3 changes: 3 additions & 0 deletions testdata/src/context_only_scope/context_only_scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ func tests(ctx context.Context) {
slog.Info("msg") // want `InfoContext should be used instead`
slog.InfoContext(ctx, "msg")

slog.With("key", "value").Info("msg") // want `InfoContext should be used instead`
slog.With("key", "value").InfoContext(ctx, "msg")

if true {
slog.Info("msg") // want `InfoContext should be used instead`
slog.InfoContext(ctx, "msg")
Expand Down
6 changes: 4 additions & 2 deletions testdata/src/no_global_all/no_global_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import "log/slog"
var logger = slog.New(nil)

func tests() {
slog.Info("msg") // want `global logger should not be used`
logger.Info("msg") // want `global logger should not be used`
slog.Info("msg") // want `global logger should not be used`
logger.Info("msg") // want `global logger should not be used`
slog.With("key", "value") // want `global logger should not be used`
logger.With("key", "value") // want `global logger should not be used`
}
8 changes: 5 additions & 3 deletions testdata/src/static_msg/static_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ func tests() {
slog.Info(constMsg)
slog.InfoContext(ctx, constMsg)
slog.Log(ctx, slog.LevelInfo, constMsg)
slog.With("key", "value").Info("msg")

slog.Info(fmt.Sprintf("msg")) // want `message should be a string literal or a constant`
slog.InfoContext(ctx, fmt.Sprintf("msg")) // want `message should be a string literal or a constant`
slog.Log(ctx, slog.LevelInfo, fmt.Sprintf("msg")) // want `message should be a string literal or a constant`
slog.Info(fmt.Sprintf("msg")) // want `message should be a string literal or a constant`
slog.InfoContext(ctx, fmt.Sprintf("msg")) // want `message should be a string literal or a constant`
slog.Log(ctx, slog.LevelInfo, fmt.Sprintf("msg")) // want `message should be a string literal or a constant`
slog.With("key", "value").Info(fmt.Sprintf("msg")) // want `message should be a string literal or a constant`

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

0 comments on commit 8c135a1

Please sign in to comment.