Skip to content
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
19 changes: 13 additions & 6 deletions sloglint.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,19 @@
}

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")
}

if opts.MsgStyle != "" && msgPos >= 0 {
if msg, ok := call.Args[msgPos].(*ast.BasicLit); ok && msg.Kind == token.STRING {
msg.Value = msg.Value[1 : len(msg.Value)-1] // trim quotes/backticks.
if ok := isValidMsgStyle(msg.Value, opts.MsgStyle); !ok {
if lit, ok := call.Args[msgPos].(*ast.BasicLit); ok && lit.Kind == token.STRING {
value, err := strconv.Unquote(lit.Value)
if err != nil {
panic("unreachable") // string literals are always quoted.

Check warning on line 252 in sloglint.go

View check run for this annotation

Codecov / codecov/patch

sloglint.go#L252

Added line #L252 was not covered by tests
}
if ok := isValidMsgStyle(value, opts.MsgStyle); !ok {
pass.Reportf(call.Pos(), "message should be %s", opts.MsgStyle)
}
}
Expand All @@ -267,7 +271,6 @@
if typ == nil {
continue
}

switch typ.String() {
case "string":
keys = append(keys, args[i])
Expand Down Expand Up @@ -376,6 +379,11 @@
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")
if msg.Op != token.ADD {
panic("unreachable") // only + can be applied to strings.

Check warning on line 384 in sloglint.go

View check run for this annotation

Codecov / codecov/patch

sloglint.go#L384

Added line #L384 was not covered by tests
}
return isStaticMsg(msg.X) && isStaticMsg(msg.Y)
default:
return false
}
Expand Down Expand Up @@ -455,10 +463,9 @@
}
}
if lit, ok := key.(*ast.BasicLit); ok && lit.Kind == token.STRING {
// string literals are always quoted.
value, err := strconv.Unquote(lit.Value)
if err != nil {
panic("unreachable")
panic("unreachable") // string literals are always quoted.

Check warning on line 468 in sloglint.go

View check run for this annotation

Codecov / codecov/patch

sloglint.go#L468

Added line #L468 was not covered by tests
}
return value, true
}
Expand Down
16 changes: 12 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,21 @@ 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" + "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