diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index aeda8070e4ae..48b7c1593eda 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -548,6 +548,12 @@ linters-settings: exclude: - '.+/cobra\.Command$' + fatcontext: + # Check for potential fat contexts in struct pointers. + # May generate false positives. + # Default: false + check-struct-pointers: true + forbidigo: # Forbid the following identifiers (list of regexp). # Default: ["^(fmt\\.Print(|f|ln)|print|println)$"] diff --git a/go.mod b/go.mod index 9f91e090929e..feb3503511ab 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/Antonboom/nilnil v1.0.1 github.com/Antonboom/testifylint v1.5.2 github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c - github.com/Crocmagnon/fatcontext v0.6.0 + github.com/Crocmagnon/fatcontext v0.7.1 github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24 github.com/GaijinEntertainment/go-exhaustruct/v3 v3.3.0 github.com/OpenPeeDeeP/depguard/v2 v2.2.0 diff --git a/go.sum b/go.sum index 5f5c064cd02b..373abad47063 100644 --- a/go.sum +++ b/go.sum @@ -49,8 +49,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03 github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c h1:pxW6RcqyfI9/kWtOwnv/G+AzdKuy2ZrqINhenH4HyNs= github.com/BurntSushi/toml v1.4.1-0.20240526193622-a339e1f7089c/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= -github.com/Crocmagnon/fatcontext v0.6.0 h1:DxGYfrUrJBOtvldiWpMvntvkTDgj6c1zLCTKelMqAtw= -github.com/Crocmagnon/fatcontext v0.6.0/go.mod h1:1wMvv3NXEBJucFGfwOJBxSVWcoIO6emV215SMkW9MFU= +github.com/Crocmagnon/fatcontext v0.7.1 h1:SC/VIbRRZQeQWj/TcQBS6JmrXcfA+BU4OGSVUt54PjM= +github.com/Crocmagnon/fatcontext v0.7.1/go.mod h1:1wMvv3NXEBJucFGfwOJBxSVWcoIO6emV215SMkW9MFU= github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24 h1:sHglBQTwgx+rWPdisA5ynNEsoARbiCBOyGcJM4/OzsM= github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24/go.mod h1:4UJr5HIiMZrwgkSPdsjy2uOQExX/WEILpIrO9UPGuXs= github.com/GaijinEntertainment/go-exhaustruct/v3 v3.3.0 h1:/fTUt5vmbkAcMBt4YQiuC23cV0kEsN1MVMNqeOW43cU= diff --git a/jsonschema/golangci.next.jsonschema.json b/jsonschema/golangci.next.jsonschema.json index 4d94d0674f65..79c5974a1bef 100644 --- a/jsonschema/golangci.next.jsonschema.json +++ b/jsonschema/golangci.next.jsonschema.json @@ -1019,6 +1019,17 @@ } } }, + "fatcontext": { + "type": "object", + "additionalProperties": false, + "properties": { + "check-struct-pointers": { + "description": "Check for potential fat contexts in struct pointers.", + "type": "boolean", + "default": false + } + } + }, "forbidigo": { "type": "object", "additionalProperties": false, diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index 8e6c184ca4d3..9968f3a0760b 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -223,6 +223,7 @@ type LintersSettings struct { ErrorLint ErrorLintSettings Exhaustive ExhaustiveSettings Exhaustruct ExhaustructSettings + Fatcontext FatcontextSettings Forbidigo ForbidigoSettings Funlen FunlenSettings Gci GciSettings @@ -430,6 +431,10 @@ type ExhaustructSettings struct { Exclude []string `mapstructure:"exclude"` } +type FatcontextSettings struct { + CheckStructPointers bool `mapstructure:"check-struct-pointers"` +} + type ForbidigoSettings struct { Forbid []ForbidigoPattern `mapstructure:"forbid"` ExcludeGodocExamples bool `mapstructure:"exclude-godoc-examples"` diff --git a/pkg/golinters/fatcontext/fatcontext.go b/pkg/golinters/fatcontext/fatcontext.go index 378025a8cc5e..2ffacacd39a7 100644 --- a/pkg/golinters/fatcontext/fatcontext.go +++ b/pkg/golinters/fatcontext/fatcontext.go @@ -4,16 +4,25 @@ import ( "github.com/Crocmagnon/fatcontext/pkg/analyzer" "golang.org/x/tools/go/analysis" + "github.com/golangci/golangci-lint/pkg/config" "github.com/golangci/golangci-lint/pkg/goanalysis" ) -func New() *goanalysis.Linter { - a := analyzer.Analyzer +func New(settings *config.FatcontextSettings) *goanalysis.Linter { + a := analyzer.NewAnalyzer() + + cfg := map[string]map[string]any{} + + if settings != nil { + cfg[a.Name] = map[string]any{ + analyzer.FlagCheckStructPointers: settings.CheckStructPointers, + } + } return goanalysis.NewLinter( a.Name, a.Doc, []*analysis.Analyzer{a}, - nil, + cfg, ).WithLoadMode(goanalysis.LoadModeTypesInfo) } diff --git a/pkg/golinters/fatcontext/testdata/fatcontext_structpointer.go b/pkg/golinters/fatcontext/testdata/fatcontext_structpointer.go new file mode 100644 index 000000000000..73968843d5b1 --- /dev/null +++ b/pkg/golinters/fatcontext/testdata/fatcontext_structpointer.go @@ -0,0 +1,25 @@ +//golangcitest:args -Efatcontext +//golangcitest:config_path testdata/fatcontext_structpointer.yml +package testdata + +import ( + "context" +) + +type Container struct { + Ctx context.Context +} + +func something() func(*Container) { + return func(r *Container) { + ctx := r.Ctx + ctx = context.WithValue(ctx, "key", "val") + r.Ctx = ctx // want "potential nested context in struct pointer" + } +} + +func blah(r *Container) { + ctx := r.Ctx + ctx = context.WithValue(ctx, "key", "val") + r.Ctx = ctx // want "potential nested context in struct pointer" +} diff --git a/pkg/golinters/fatcontext/testdata/fatcontext_structpointer.yml b/pkg/golinters/fatcontext/testdata/fatcontext_structpointer.yml new file mode 100644 index 000000000000..95924ee1d7d6 --- /dev/null +++ b/pkg/golinters/fatcontext/testdata/fatcontext_structpointer.yml @@ -0,0 +1,4 @@ +linters-settings: + fatcontext: + check-struct-pointers: true + diff --git a/pkg/lint/lintersdb/builder_linter.go b/pkg/lint/lintersdb/builder_linter.go index 116e46a9f4ee..beda9e3c76b6 100644 --- a/pkg/lint/lintersdb/builder_linter.go +++ b/pkg/lint/lintersdb/builder_linter.go @@ -307,7 +307,7 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) { WithPresets(linter.PresetStyle). WithURL("https://github.com/gostaticanalysis/forcetypeassert"), - linter.NewConfig(fatcontext.New()). + linter.NewConfig(fatcontext.New(&cfg.LintersSettings.Fatcontext)). WithSince("v1.58.0"). WithPresets(linter.PresetPerformance). WithLoadForGoAnalysis().