Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not check static-msg for With functions #45

Merged
merged 1 commit into from
May 27, 2024
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
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
Loading