Skip to content

Commit

Permalink
feat: add fatcontext linter (#4583)
Browse files Browse the repository at this point in the history
  • Loading branch information
Crocmagnon authored Apr 1, 2024
1 parent 7e2229a commit c00c1a5
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2534,6 +2534,7 @@ linters:
- exhaustive
- exhaustruct
- exportloopref
- fatcontext
- forbidigo
- forcetypeassert
- funlen
Expand Down Expand Up @@ -2647,6 +2648,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 (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)
}

0 comments on commit c00c1a5

Please sign in to comment.