sloglint: add static-msg option (#4213)

This commit is contained in:
Matthew Dowdell 2023-11-29 17:03:30 +00:00 committed by GitHub
parent 855d443351
commit f269abec96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 0 deletions

View File

@ -1864,6 +1864,9 @@ linters-settings:
# Enforce using methods that accept a context.
# Default: false
context-only: true
# Enforce using static values for log messages.
# Default: false
static-msg: true
# Enforce using constants instead of raw keys.
# Default: false
no-raw-keys: true

View File

@ -123,6 +123,7 @@ var defaultLintersSettings = LintersSettings{
KVOnly: false,
AttrOnly: false,
ContextOnly: false,
StaticMsg: false,
NoRawKeys: false,
KeyNamingCase: "",
ArgsOnSepLines: false,
@ -755,6 +756,7 @@ type SlogLintSettings struct {
KVOnly bool `mapstructure:"kv-only"`
AttrOnly bool `mapstructure:"attr-only"`
ContextOnly bool `mapstructure:"context-only"`
StaticMsg bool `mapstructure:"static-msg"`
NoRawKeys bool `mapstructure:"no-raw-keys"`
KeyNamingCase string `mapstructure:"key-naming-case"`
ArgsOnSepLines bool `mapstructure:"args-on-sep-lines"`

View File

@ -15,6 +15,7 @@ func NewSlogLint(settings *config.SlogLintSettings) *goanalysis.Linter {
KVOnly: settings.KVOnly,
AttrOnly: settings.AttrOnly,
ContextOnly: settings.ContextOnly,
StaticMsg: settings.StaticMsg,
NoRawKeys: settings.NoRawKeys,
KeyNamingCase: settings.KeyNamingCase,
ArgsOnSepLines: settings.ArgsOnSepLines,

View File

@ -0,0 +1,3 @@
linters-settings:
sloglint:
static-msg: true

19
test/testdata/sloglint_static_msg.go vendored Normal file
View File

@ -0,0 +1,19 @@
//go:build go1.21
//golangcitest:args -Esloglint
//golangcitest:config_path testdata/configs/sloglint_static_msg.yml
package testdata
import (
"log/slog"
)
func test() {
slog.Info("msg")
const msg1 = "msg"
slog.Info(msg1)
msg2 := "msg"
slog.Info(msg2) // want `message should be a string literal or a constant`
}