Skip to content

Commit eaecb28

Browse files
committed
feat: add foreshadow linter
1 parent c037920 commit eaecb28

File tree

7 files changed

+62
-0
lines changed

7 files changed

+62
-0
lines changed

.golangci.next.reference.yml

+2
Original file line numberDiff line numberDiff line change
@@ -2532,6 +2532,7 @@ linters:
25322532
- exhaustive
25332533
- exhaustruct
25342534
- exportloopref
2535+
- fatcontext
25352536
- forbidigo
25362537
- forcetypeassert
25372538
- funlen
@@ -2645,6 +2646,7 @@ linters:
26452646
- exhaustive
26462647
- exhaustruct
26472648
- exportloopref
2649+
- fatcontext
26482650
- forbidigo
26492651
- forcetypeassert
26502652
- funlen

go.mod

+1
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.0
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

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jsonschema/golangci.next.jsonschema.json

+1
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

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
return goanalysis.NewLinter(
12+
"fatcontext",
13+
"Detects potential fat contexts in loops",
14+
[]*analysis.Analyzer{analyzer.Analyzer},
15+
nil,
16+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
17+
}

pkg/lint/lintersdb/builder_linter.go

+6
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ func (b 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

+33
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)