diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index 361b92c2faf9..da278de696cc 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -1997,12 +1997,15 @@ linters-settings: sloglint: # Enforce not mixing key-value pairs and attributes. + # https://github.com/go-simpler/sloglint?tab=readme-ov-file#no-mixed-arguments # Default: true no-mixed-args: false # Enforce using key-value pairs only (overrides no-mixed-args, incompatible with attr-only). + # https://github.com/go-simpler/sloglint?tab=readme-ov-file#key-value-pairs-only # Default: false kv-only: true # Enforce using attributes only (overrides no-mixed-args, incompatible with kv-only). + # https://github.com/go-simpler/sloglint?tab=readme-ov-file#attributes-only # Default: false attr-only: true # Enforce not using global loggers. @@ -2010,6 +2013,7 @@ linters-settings: # - "": disabled # - "all": report all global loggers # - "default": report only the default slog logger + # https://github.com/go-simpler/sloglint?tab=readme-ov-file#no-global # Default: "" no-global: "all" # Enforce using methods that accept a context. @@ -2017,19 +2021,32 @@ linters-settings: # - "": disabled # - "all": report all contextless calls # - "scope": report only if a context exists in the scope of the outermost function + # https://github.com/go-simpler/sloglint?tab=readme-ov-file#context-only # Default: "" context: "all" # Enforce using static values for log messages. + # https://github.com/go-simpler/sloglint?tab=readme-ov-file#static-messages # Default: false static-msg: true # Enforce using constants instead of raw keys. + # https://github.com/go-simpler/sloglint?tab=readme-ov-file#no-raw-keys # Default: false no-raw-keys: true # Enforce a single key naming convention. # Values: snake, kebab, camel, pascal + # https://github.com/go-simpler/sloglint?tab=readme-ov-file#key-naming-convention # Default: "" key-naming-case: snake + # Enforce not using specific keys. + # Default: [] + forbidden-keys: + - time + - level + - msg + - source + - foo # Enforce putting arguments on separate lines. + # https://github.com/go-simpler/sloglint?tab=readme-ov-file#forbidden-keys # Default: false args-on-sep-lines: true diff --git a/go.mod b/go.mod index 3413a9b1eab2..52524fe7755e 100644 --- a/go.mod +++ b/go.mod @@ -122,7 +122,7 @@ require ( github.com/ykadowak/zerologlint v0.1.5 gitlab.com/bosi/decorder v0.4.2 go-simpler.org/musttag v0.12.2 - go-simpler.org/sloglint v0.6.0 + go-simpler.org/sloglint v0.7.0 go.uber.org/automaxprocs v1.5.3 golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc golang.org/x/tools v0.21.0 diff --git a/go.sum b/go.sum index a92c5a824b97..52b613e66e39 100644 --- a/go.sum +++ b/go.sum @@ -594,8 +594,8 @@ go-simpler.org/assert v0.9.0 h1:PfpmcSvL7yAnWyChSjOz6Sp6m9j5lyK8Ok9pEL31YkQ= go-simpler.org/assert v0.9.0/go.mod h1:74Eqh5eI6vCK6Y5l3PI8ZYFXG4Sa+tkr70OIPJAUr28= go-simpler.org/musttag v0.12.2 h1:J7lRc2ysXOq7eM8rwaTYnNrHd5JwjppzB6mScysB2Cs= go-simpler.org/musttag v0.12.2/go.mod h1:uN1DVIasMTQKk6XSik7yrJoEysGtR2GRqvWnI9S7TYM= -go-simpler.org/sloglint v0.6.0 h1:0YcqSVG7LI9EVBfRPhgPec79BH6X6mwjFuUR5Mr7j1M= -go-simpler.org/sloglint v0.6.0/go.mod h1:+kJJtebtPePWyG5boFwY46COydAggADDOHM22zOvzBk= +go-simpler.org/sloglint v0.7.0 h1:rMZRxD9MbaGoRFobIOicMxZzum7AXNFDlez6xxJs5V4= +go-simpler.org/sloglint v0.7.0/go.mod h1:g9SXiSWY0JJh4LS39/Q0GxzP/QX2cVcbTOYhDpXrJEs= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= diff --git a/jsonschema/golangci.next.jsonschema.json b/jsonschema/golangci.next.jsonschema.json index 5b0082759294..77edb3359731 100644 --- a/jsonschema/golangci.next.jsonschema.json +++ b/jsonschema/golangci.next.jsonschema.json @@ -2468,6 +2468,13 @@ "type": "boolean", "default": false }, + "forbidden-keys": { + "description": "Enforce not using specific keys.", + "type": "array", + "items": { + "type": "string" + } + }, "args-on-sep-lines": { "description": "Enforce putting arguments on separate lines.", "type": "boolean", diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index e204328421ce..876b40cfbf83 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -146,10 +146,10 @@ var defaultLintersSettings = LintersSettings{ AttrOnly: false, NoGlobal: "", Context: "", - ContextOnly: false, StaticMsg: false, NoRawKeys: false, KeyNamingCase: "", + ForbiddenKeys: nil, ArgsOnSepLines: false, }, TagAlign: TagAlignSettings{ @@ -819,16 +819,19 @@ type RowsErrCheckSettings struct { } type SlogLintSettings struct { - NoMixedArgs bool `mapstructure:"no-mixed-args"` - KVOnly bool `mapstructure:"kv-only"` - AttrOnly bool `mapstructure:"attr-only"` - NoGlobal string `mapstructure:"no-global"` - Context string `mapstructure:"context"` - ContextOnly bool `mapstructure:"context-only"` // Deprecated: use Context instead. - StaticMsg bool `mapstructure:"static-msg"` - NoRawKeys bool `mapstructure:"no-raw-keys"` - KeyNamingCase string `mapstructure:"key-naming-case"` - ArgsOnSepLines bool `mapstructure:"args-on-sep-lines"` + NoMixedArgs bool `mapstructure:"no-mixed-args"` + KVOnly bool `mapstructure:"kv-only"` + AttrOnly bool `mapstructure:"attr-only"` + NoGlobal string `mapstructure:"no-global"` + Context string `mapstructure:"context"` + StaticMsg bool `mapstructure:"static-msg"` + NoRawKeys bool `mapstructure:"no-raw-keys"` + KeyNamingCase string `mapstructure:"key-naming-case"` + ForbiddenKeys []string `mapstructure:"forbidden-keys"` + ArgsOnSepLines bool `mapstructure:"args-on-sep-lines"` + + // Deprecated: use Context instead. + ContextOnly bool `mapstructure:"context-only"` } type SpancheckSettings struct { diff --git a/pkg/golinters/sloglint/sloglint.go b/pkg/golinters/sloglint/sloglint.go index d3f567e9c7f7..486662577935 100644 --- a/pkg/golinters/sloglint/sloglint.go +++ b/pkg/golinters/sloglint/sloglint.go @@ -20,6 +20,7 @@ func New(settings *config.SlogLintSettings) *goanalysis.Linter { StaticMsg: settings.StaticMsg, NoRawKeys: settings.NoRawKeys, KeyNamingCase: settings.KeyNamingCase, + ForbiddenKeys: settings.ForbiddenKeys, ArgsOnSepLines: settings.ArgsOnSepLines, } } diff --git a/pkg/golinters/sloglint/testdata/sloglint_forbidden_keys.go b/pkg/golinters/sloglint/testdata/sloglint_forbidden_keys.go new file mode 100644 index 000000000000..b7ef720d110c --- /dev/null +++ b/pkg/golinters/sloglint/testdata/sloglint_forbidden_keys.go @@ -0,0 +1,28 @@ +//golangcitest:args -Esloglint +//golangcitest:config_path testdata/sloglint_forbidden_keys.yml +package testdata + +import "log/slog" + +const ( + snakeKey = "foo_bar" +) + +func tests() { + slog.Info("msg") + slog.Info("msg", "foo-bar", 1) + slog.Info("msg", "foo_bar", 1) // want `"foo_bar" key is forbidden and should not be used` + slog.Info("msg", snakeKey, 1) // want `"foo_bar" key is forbidden and should not be used` + slog.Info("msg", slog.Int("foo_bar", 1)) // want `"foo_bar" key is forbidden and should not be used` + slog.Info("msg", slog.Int(snakeKey, 1)) // want `"foo_bar" key is forbidden and should not be used` + slog.Info("msg", slog.Attr{}) + slog.Info("msg", slog.Attr{"foo_bar", slog.IntValue(1)}) // want `"foo_bar" key is forbidden and should not be used` + slog.Info("msg", slog.Attr{snakeKey, slog.IntValue(1)}) // want `"foo_bar" key is forbidden and should not be used` + slog.Info("msg", slog.Attr{Key: "foo_bar"}) // want `"foo_bar" key is forbidden and should not be used` + slog.Info("msg", slog.Attr{Key: snakeKey}) // want `"foo_bar" key is forbidden and should not be used` + slog.Info("msg", slog.Attr{Key: "foo_bar", Value: slog.IntValue(1)}) // want `"foo_bar" key is forbidden and should not be used` + slog.Info("msg", slog.Attr{Key: snakeKey, Value: slog.IntValue(1)}) // want `"foo_bar" key is forbidden and should not be used` + slog.Info("msg", slog.Attr{Value: slog.IntValue(1), Key: "foo_bar"}) // want `"foo_bar" key is forbidden and should not be used` + slog.Info("msg", slog.Attr{Value: slog.IntValue(1), Key: snakeKey}) // want `"foo_bar" key is forbidden and should not be used` + slog.Info("msg", slog.Attr{Value: slog.IntValue(1), Key: `foo_bar`}) // want `"foo_bar" key is forbidden and should not be used` +} diff --git a/pkg/golinters/sloglint/testdata/sloglint_forbidden_keys.yml b/pkg/golinters/sloglint/testdata/sloglint_forbidden_keys.yml new file mode 100644 index 000000000000..2b0d969cd63c --- /dev/null +++ b/pkg/golinters/sloglint/testdata/sloglint_forbidden_keys.yml @@ -0,0 +1,4 @@ +linters-settings: + sloglint: + forbidden-keys: + - foo_bar