diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index 546270df..860e4fc2 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -2014,8 +2014,12 @@ linters-settings: # Default: "" no-global: "all" # Enforce using methods that accept a context. - # Default: false - context-only: true + # Values: + # - "": disabled + # - "all": report all contextless calls + # - "scope": report only if a context exists in the scope of the outermost function + # Default: "" + context: "all" # Enforce using static values for log messages. # Default: false static-msg: true diff --git a/go.mod b/go.mod index cc9f1863..72ed95cb 100644 --- a/go.mod +++ b/go.mod @@ -121,7 +121,7 @@ require ( github.com/ykadowak/zerologlint v0.1.5 gitlab.com/bosi/decorder v0.4.1 go-simpler.org/musttag v0.12.1 - go-simpler.org/sloglint v0.5.1 + go-simpler.org/sloglint v0.6.0 go.uber.org/automaxprocs v1.5.3 golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc golang.org/x/tools v0.20.0 diff --git a/go.sum b/go.sum index fcce20d0..2242a7d0 100644 --- a/go.sum +++ b/go.sum @@ -592,8 +592,8 @@ go-simpler.org/assert v0.7.0 h1:OzWWZqfNxt8cLS+MlUp6Tgk1HjPkmgdKBq9qvy8lZsA= go-simpler.org/assert v0.7.0/go.mod h1:74Eqh5eI6vCK6Y5l3PI8ZYFXG4Sa+tkr70OIPJAUr28= go-simpler.org/musttag v0.12.1 h1:yaMcjl/uyVnd1z6GqIhBiFH/PoqNN9f2IgtU7bp7W/0= go-simpler.org/musttag v0.12.1/go.mod h1:46HKu04A3Am9Lne5kKP0ssgwY3AeIlqsDzz3UxKROpY= -go-simpler.org/sloglint v0.5.1 h1:v4b9ave8MidsW28AkkRnF6Syg109zDgsYLiTlB5Ldew= -go-simpler.org/sloglint v0.5.1/go.mod h1:+kJJtebtPePWyG5boFwY46COydAggADDOHM22zOvzBk= +go-simpler.org/sloglint v0.6.0 h1:0YcqSVG7LI9EVBfRPhgPec79BH6X6mwjFuUR5Mr7j1M= +go-simpler.org/sloglint v0.6.0/go.mod h1:+kJJtebtPePWyG5boFwY46COydAggADDOHM22zOvzBk= 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 ed1c51cf..2d2673b8 100644 --- a/jsonschema/golangci.next.jsonschema.json +++ b/jsonschema/golangci.next.jsonschema.json @@ -2289,10 +2289,10 @@ "type": "boolean", "default": true }, - "context-only": { + "context": { "description": "Enforce using methods that accept a context.", - "type": "boolean", - "default": false + "enum": ["", "all", "scope"], + "default": "" }, "static-msg": { "description": "Enforce using static values for log messages.", diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index b368f4f2..eccf368b 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -144,6 +144,8 @@ var defaultLintersSettings = LintersSettings{ NoMixedArgs: true, KVOnly: false, AttrOnly: false, + NoGlobal: "", + Context: "", ContextOnly: false, StaticMsg: false, NoRawKeys: false, @@ -810,9 +812,10 @@ type RowsErrCheckSettings struct { type SlogLintSettings struct { NoMixedArgs bool `mapstructure:"no-mixed-args"` KVOnly bool `mapstructure:"kv-only"` - NoGlobal string `mapstructure:"no-global"` AttrOnly bool `mapstructure:"attr-only"` - ContextOnly bool `mapstructure:"context-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"` diff --git a/pkg/config/loader.go b/pkg/config/loader.go index eb5044a7..57ec4d4e 100644 --- a/pkg/config/loader.go +++ b/pkg/config/loader.go @@ -408,6 +408,14 @@ func (l *Loader) handleLinterOptionDeprecations() { if l.cfg.LintersSettings.Stylecheck.GoVersion != "" { l.log.Warnf("The configuration option `linters.stylecheck.go` is deprecated, please use global `run.go`.") } + + // Deprecated since v1.58.0 + if l.cfg.LintersSettings.SlogLint.ContextOnly { + l.log.Warnf("The configuration option `linters.sloglint.context-only` is deprecated, please use `linters.sloglint.context`") + if l.cfg.LintersSettings.SlogLint.Context == "" { + l.cfg.LintersSettings.SlogLint.Context = "all" + } + } } func (l *Loader) handleEnableOnlyOption() error { diff --git a/pkg/golinters/sloglint/sloglint.go b/pkg/golinters/sloglint/sloglint.go index be7d4518..d3f567e9 100644 --- a/pkg/golinters/sloglint/sloglint.go +++ b/pkg/golinters/sloglint/sloglint.go @@ -14,9 +14,9 @@ func New(settings *config.SlogLintSettings) *goanalysis.Linter { opts = &sloglint.Options{ NoMixedArgs: settings.NoMixedArgs, KVOnly: settings.KVOnly, - NoGlobal: settings.NoGlobal, AttrOnly: settings.AttrOnly, - ContextOnly: settings.ContextOnly, + NoGlobal: settings.NoGlobal, + ContextOnly: settings.Context, StaticMsg: settings.StaticMsg, NoRawKeys: settings.NoRawKeys, KeyNamingCase: settings.KeyNamingCase, diff --git a/pkg/golinters/sloglint/testdata/sloglint_context_only.go b/pkg/golinters/sloglint/testdata/sloglint_context_only.go index a257a516..4eca68e1 100644 --- a/pkg/golinters/sloglint/testdata/sloglint_context_only.go +++ b/pkg/golinters/sloglint/testdata/sloglint_context_only.go @@ -12,5 +12,5 @@ import ( func test() { slog.InfoContext(context.Background(), "msg") - slog.Info("msg") // want `methods without a context should not be used` + slog.Info("msg") // want `InfoContext should be used instead` } diff --git a/pkg/golinters/sloglint/testdata/sloglint_context_only.yml b/pkg/golinters/sloglint/testdata/sloglint_context_only.yml index a15bbe49..2b0703ce 100644 --- a/pkg/golinters/sloglint/testdata/sloglint_context_only.yml +++ b/pkg/golinters/sloglint/testdata/sloglint_context_only.yml @@ -1,3 +1,3 @@ linters-settings: sloglint: - context-only: true + context: "all"