Skip to content

Commit

Permalink
feat: add foreshadow linter
Browse files Browse the repository at this point in the history
  • Loading branch information
Crocmagnon committed Mar 27, 2024
1 parent c037920 commit c9f847d
Show file tree
Hide file tree
Showing 7 changed files with 63 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:
- exportloopref
- forbidigo
- forcetypeassert
- foreshadow
- funlen
- gci
- ginkgolinter
Expand Down Expand Up @@ -2647,6 +2648,7 @@ linters:
- exportloopref
- forbidigo
- forcetypeassert
- foreshadow
- funlen
- gci
- ginkgolinter
Expand Down
2 changes: 2 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2534,6 +2534,7 @@ linters:
- exportloopref
- forbidigo
- forcetypeassert
- foreshadow
- funlen
- gci
- ginkgolinter
Expand Down Expand Up @@ -2647,6 +2648,7 @@ linters:
- exportloopref
- forbidigo
- forcetypeassert
- foreshadow
- funlen
- gci
- ginkgolinter
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/foreshadow v0.1.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.

17 changes: 17 additions & 0 deletions pkg/golinters/foreshadow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package golinters

import (
"github.com/Crocmagnon/foreshadow/pkg/analyzer"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/goanalysis"
)

func NewForeshadow() *goanalysis.Linter {
return goanalysis.NewLinter(
"foreshadow",
"Enforces context shadowing inside loops",
[]*analysis.Analyzer{analyzer.Analyzer},
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.NewForeshadow()).
WithSince("1.58.0").
WithPresets(linter.PresetPerformance).
WithLoadForGoAnalysis().
WithURL("https://github.com/Crocmagnon/foreshadow"),

linter.NewConfig(golinters.NewFunlen(&cfg.LintersSettings.Funlen)).
WithSince("v1.18.0").
WithPresets(linter.PresetComplexity).
Expand Down
33 changes: 33 additions & 0 deletions test/testdata/foreshadow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//golangcitest:args -Eforeshadow
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 "context not shadowed in loop"
ctx = context.WithValue(ctx, "other", "val")
}

for item := range []string{"one", "two", "three"} {
ctx = wrapContext(ctx) // want "context not shadowed in loop"
ctx := context.WithValue(ctx, "key", item)
ctx = wrapContext(ctx)
}

for {
ctx = wrapContext(ctx) // want "context not shadowed in loop"
break
}
}

func wrapContext(ctx context.Context) context.Context {
return context.WithoutCancel(ctx)
}

0 comments on commit c9f847d

Please sign in to comment.