From f9a76f2ff148966b8dac8291c482abc3133df285 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Wed, 27 Mar 2024 19:48:37 +0100 Subject: [PATCH 1/2] feat: add fatcontext linter --- .golangci.next.reference.yml | 2 ++ go.mod | 1 + go.sum | 2 ++ jsonschema/golangci.next.jsonschema.json | 1 + pkg/golinters/fatcontext.go | 17 ++++++++++++ pkg/lint/lintersdb/builder_linter.go | 6 +++++ test/testdata/fatcontext.go | 33 ++++++++++++++++++++++++ 7 files changed, 62 insertions(+) create mode 100644 pkg/golinters/fatcontext.go create mode 100644 test/testdata/fatcontext.go diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index c4e23bb0cbd6..b4cfe946b9e8 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -2532,6 +2532,7 @@ linters: - exhaustive - exhaustruct - exportloopref + - fatcontext - forbidigo - forcetypeassert - funlen @@ -2645,6 +2646,7 @@ linters: - exhaustive - exhaustruct - exportloopref + - fatcontext - forbidigo - forcetypeassert - funlen diff --git a/go.mod b/go.mod index 09e91b469e67..9bd350c2fbe0 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/Antonboom/nilnil v0.1.7 github.com/Antonboom/testifylint v1.2.0 github.com/BurntSushi/toml v1.3.2 + github.com/Crocmagnon/fatcontext v0.2.2 github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24 github.com/GaijinEntertainment/go-exhaustruct/v3 v3.2.0 github.com/OpenPeeDeeP/depguard/v2 v2.2.0 diff --git a/go.sum b/go.sum index 805c22e514d6..95605d6d2ca7 100644 --- a/go.sum +++ b/go.sum @@ -49,6 +49,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03 github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= +github.com/Crocmagnon/fatcontext v0.2.2 h1:OrFlsDdOj9hW/oBEJBNSuH7QWf+E9WPVHw+x52bXVbk= +github.com/Crocmagnon/fatcontext v0.2.2/go.mod h1:WSn/c/+MMNiD8Pri0ahRj0o9jVpeowzavOQplBJw6u0= 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.2.0 h1:sATXp1x6/axKxz2Gjxv8MALP0bXaNRfQinEwyfMcx8c= diff --git a/jsonschema/golangci.next.jsonschema.json b/jsonschema/golangci.next.jsonschema.json index f411de6cbe3a..6e09d29ec6bb 100644 --- a/jsonschema/golangci.next.jsonschema.json +++ b/jsonschema/golangci.next.jsonschema.json @@ -235,6 +235,7 @@ "exhaustivestruct", "exhaustruct", "exportloopref", + "fatcontext", "forbidigo", "forcetypeassert", "funlen", diff --git a/pkg/golinters/fatcontext.go b/pkg/golinters/fatcontext.go new file mode 100644 index 000000000000..58830b6b66b6 --- /dev/null +++ b/pkg/golinters/fatcontext.go @@ -0,0 +1,17 @@ +package golinters + +import ( + "github.com/Crocmagnon/fatcontext/pkg/analyzer" + "golang.org/x/tools/go/analysis" + + "github.com/golangci/golangci-lint/pkg/goanalysis" +) + +func NewFatContext() *goanalysis.Linter { + return goanalysis.NewLinter( + "fatcontext", + "Detects potential fat contexts in loops", + []*analysis.Analyzer{analyzer.Analyzer}, + nil, + ).WithLoadMode(goanalysis.LoadModeTypesInfo) +} diff --git a/pkg/lint/lintersdb/builder_linter.go b/pkg/lint/lintersdb/builder_linter.go index 649a82fd7545..b73552f3f037 100644 --- a/pkg/lint/lintersdb/builder_linter.go +++ b/pkg/lint/lintersdb/builder_linter.go @@ -181,6 +181,12 @@ func (b LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) { WithPresets(linter.PresetStyle). WithURL("https://github.com/gostaticanalysis/forcetypeassert"), + linter.NewConfig(golinters.NewFatContext()). + WithSince("1.58.0"). + WithPresets(linter.PresetPerformance). + WithLoadForGoAnalysis(). + WithURL("https://github.com/Crocmagnon/fatcontext"), + linter.NewConfig(golinters.NewFunlen(&cfg.LintersSettings.Funlen)). WithSince("v1.18.0"). WithPresets(linter.PresetComplexity). diff --git a/test/testdata/fatcontext.go b/test/testdata/fatcontext.go new file mode 100644 index 000000000000..f504f4d7ccd7 --- /dev/null +++ b/test/testdata/fatcontext.go @@ -0,0 +1,33 @@ +//golangcitest:args -Efatcontext +package testdata + +import "context" + +func example() { + ctx := context.Background() + + for i := 0; i < 10; i++ { + ctx := context.WithValue(ctx, "key", i) + ctx = context.WithValue(ctx, "other", "val") + } + + for i := 0; i < 10; i++ { + ctx = context.WithValue(ctx, "key", i) // want "nested context in loop" + ctx = context.WithValue(ctx, "other", "val") + } + + for item := range []string{"one", "two", "three"} { + ctx = wrapContext(ctx) // want "nested context in loop" + ctx := context.WithValue(ctx, "key", item) + ctx = wrapContext(ctx) + } + + for { + ctx = wrapContext(ctx) // want "nested context in loop" + break + } +} + +func wrapContext(ctx context.Context) context.Context { + return context.WithoutCancel(ctx) +} From 224e5189603d731c0d28ce9d36ae6aa76abe00ae Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Mon, 1 Apr 2024 16:15:08 +0200 Subject: [PATCH 2/2] review --- pkg/golinters/fatcontext.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/golinters/fatcontext.go b/pkg/golinters/fatcontext.go index 58830b6b66b6..8ea3102920f1 100644 --- a/pkg/golinters/fatcontext.go +++ b/pkg/golinters/fatcontext.go @@ -8,10 +8,12 @@ import ( ) func NewFatContext() *goanalysis.Linter { + a := analyzer.Analyzer + return goanalysis.NewLinter( - "fatcontext", - "Detects potential fat contexts in loops", - []*analysis.Analyzer{analyzer.Analyzer}, + a.Name, + a.Doc, + []*analysis.Analyzer{a}, nil, ).WithLoadMode(goanalysis.LoadModeTypesInfo) }