Skip to content

feat: add fatcontext linter #4583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2532,6 +2532,7 @@ linters:
- exhaustive
- exhaustruct
- exportloopref
- fatcontext
- forbidigo
- forcetypeassert
- funlen
Expand Down Expand Up @@ -2645,6 +2646,7 @@ linters:
- exhaustive
- exhaustruct
- exportloopref
- fatcontext
- forbidigo
- forcetypeassert
- funlen
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions jsonschema/golangci.next.jsonschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@
"exhaustivestruct",
"exhaustruct",
"exportloopref",
"fatcontext",
"forbidigo",
"forcetypeassert",
"funlen",
Expand Down
19 changes: 19 additions & 0 deletions pkg/golinters/fatcontext.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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 {
a := analyzer.Analyzer

return goanalysis.NewLinter(
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
6 changes: 6 additions & 0 deletions pkg/lint/lintersdb/builder_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
33 changes: 33 additions & 0 deletions test/testdata/fatcontext.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading