Skip to content

Commit c00c1a5

Browse files
authored
feat: add fatcontext linter (#4583)
1 parent 7e2229a commit c00c1a5

File tree

7 files changed

+64
-0
lines changed

7 files changed

+64
-0
lines changed

.golangci.next.reference.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2534,6 +2534,7 @@ linters:
25342534
- exhaustive
25352535
- exhaustruct
25362536
- exportloopref
2537+
- fatcontext
25372538
- forbidigo
25382539
- forcetypeassert
25392540
- funlen
@@ -2647,6 +2648,7 @@ linters:
26472648
- exhaustive
26482649
- exhaustruct
26492650
- exportloopref
2651+
- fatcontext
26502652
- forbidigo
26512653
- forcetypeassert
26522654
- funlen

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/Antonboom/nilnil v0.1.7
1212
github.com/Antonboom/testifylint v1.2.0
1313
github.com/BurntSushi/toml v1.3.2
14+
github.com/Crocmagnon/fatcontext v0.2.2
1415
github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24
1516
github.com/GaijinEntertainment/go-exhaustruct/v3 v3.2.0
1617
github.com/OpenPeeDeeP/depguard/v2 v2.2.0

go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jsonschema/golangci.next.jsonschema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@
235235
"exhaustivestruct",
236236
"exhaustruct",
237237
"exportloopref",
238+
"fatcontext",
238239
"forbidigo",
239240
"forcetypeassert",
240241
"funlen",

pkg/golinters/fatcontext.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package golinters
2+
3+
import (
4+
"github.com/Crocmagnon/fatcontext/pkg/analyzer"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/goanalysis"
8+
)
9+
10+
func NewFatContext() *goanalysis.Linter {
11+
a := analyzer.Analyzer
12+
13+
return goanalysis.NewLinter(
14+
a.Name,
15+
a.Doc,
16+
[]*analysis.Analyzer{a},
17+
nil,
18+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
19+
}

pkg/lint/lintersdb/builder_linter.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ func (LinterBuilder) Build(cfg *config.Config) ([]*linter.Config, error) {
181181
WithPresets(linter.PresetStyle).
182182
WithURL("https://github.com/gostaticanalysis/forcetypeassert"),
183183

184+
linter.NewConfig(golinters.NewFatContext()).
185+
WithSince("1.58.0").
186+
WithPresets(linter.PresetPerformance).
187+
WithLoadForGoAnalysis().
188+
WithURL("https://github.com/Crocmagnon/fatcontext"),
189+
184190
linter.NewConfig(golinters.NewFunlen(&cfg.LintersSettings.Funlen)).
185191
WithSince("v1.18.0").
186192
WithPresets(linter.PresetComplexity).

test/testdata/fatcontext.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//golangcitest:args -Efatcontext
2+
package testdata
3+
4+
import "context"
5+
6+
func example() {
7+
ctx := context.Background()
8+
9+
for i := 0; i < 10; i++ {
10+
ctx := context.WithValue(ctx, "key", i)
11+
ctx = context.WithValue(ctx, "other", "val")
12+
}
13+
14+
for i := 0; i < 10; i++ {
15+
ctx = context.WithValue(ctx, "key", i) // want "nested context in loop"
16+
ctx = context.WithValue(ctx, "other", "val")
17+
}
18+
19+
for item := range []string{"one", "two", "three"} {
20+
ctx = wrapContext(ctx) // want "nested context in loop"
21+
ctx := context.WithValue(ctx, "key", item)
22+
ctx = wrapContext(ctx)
23+
}
24+
25+
for {
26+
ctx = wrapContext(ctx) // want "nested context in loop"
27+
break
28+
}
29+
}
30+
31+
func wrapContext(ctx context.Context) context.Context {
32+
return context.WithoutCancel(ctx)
33+
}

0 commit comments

Comments
 (0)