Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion sloglint.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ func visit(pass *analysis.Pass, opts *Options, node ast.Node, stack []ast.Node)
}

msgPos := funcInfo.argsPos - 1

// 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 Expand Up @@ -267,7 +268,6 @@ func visit(pass *analysis.Pass, opts *Options, node ast.Node, stack []ast.Node)
if typ == nil {
continue
}

switch typ.String() {
case "string":
keys = append(keys, args[i])
Expand Down Expand Up @@ -376,6 +376,8 @@ func isStaticMsg(msg ast.Expr) bool {
return msg.Kind == token.STRING
case *ast.Ident: // e.g. const msg = "msg"; slog.Info(msg)
return msg.Obj != nil && msg.Obj.Kind == ast.Con
case *ast.BinaryExpr: // e.g. slog.Info("x" + "y")
return isStaticMsg(msg.X) && isStaticMsg(msg.Y)
default:
return false
}
Expand Down
15 changes: 11 additions & 4 deletions testdata/src/static_msg/static_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,20 @@ func tests() {
slog.Log(ctx, slog.LevelInfo, constMsg)
slog.With("key", "value").Info(constMsg)

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`
slog.Log(ctx, slog.LevelInfo, varMsg) // want `message should be a string literal or a constant`
slog.With("key", "value").Info(varMsg) // 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`
slog.Log(ctx, slog.LevelInfo, varMsg) // want `message should be a string literal or a constant`
slog.With("key", "value").Info(varMsg) // want `message should be a string literal or a constant`
// binary expressions:
slog.Info("msg" + "msg")
slog.Info("msg" + constMsg)
slog.Info("msg" + varMsg) // want `message should be a string literal or a constant`
slog.Info("msg" + fmt.Sprintf("msg")) // want `message should be a string literal or a constant`
slog.Info("msg" + constMsg + varMsg + fmt.Sprintf("msg")) // want `message should be a string literal or a constant`
}
Loading