Skip to content
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

contextcheck: bump to v1.0.5 && add noCache flag #2525

Closed
wants to merge 1 commit into from
Closed
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: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ require (
github.com/spf13/viper v1.10.1
github.com/ssgreg/nlreturn/v2 v2.2.1
github.com/stretchr/testify v1.7.0
github.com/sylvia7788/contextcheck v1.0.4
github.com/sylvia7788/contextcheck v1.0.5
github.com/tdakkota/asciicheck v0.1.1
github.com/tetafro/godot v1.4.11
github.com/timakin/bodyclose v0.0.0-20210704033933-f49887972144
Expand Down
4 changes: 2 additions & 2 deletions go.sum

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

6 changes: 5 additions & 1 deletion pkg/golinters/contextcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"golang.org/x/tools/go/analysis"

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

func NewContextCheck() *goanalysis.Linter {
Expand All @@ -14,5 +15,8 @@ func NewContextCheck() *goanalysis.Linter {
"check the function whether use a non-inherited context",
[]*analysis.Analyzer{analyzer},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
).WithLoadMode(goanalysis.LoadModeTypesInfo).WithNoCache().
WithContextSetter(func(lintCtx *linter.Context) {
analyzer.Run = contextcheck.NewRun(lintCtx.Packages)
})
}
10 changes: 10 additions & 0 deletions pkg/golinters/goanalysis/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Linter struct {
contextSetter func(*linter.Context)
loadMode LoadMode
needUseOriginalPackages bool
noCache bool
}

func NewLinter(name, desc string, analyzers []*analysis.Analyzer, cfg map[string]map[string]interface{}) *Linter {
Expand Down Expand Up @@ -86,6 +87,11 @@ func (lnt *Linter) WithContextSetter(cs func(*linter.Context)) *Linter {
return lnt
}

func (lnt *Linter) WithNoCache() *Linter {
lnt.noCache = true
return lnt
}

func (lnt *Linter) Name() string {
return lnt.name
}
Expand Down Expand Up @@ -187,6 +193,10 @@ func (lnt *Linter) getLoadMode() LoadMode {
return lnt.loadMode
}

func (lnt *Linter) withoutCache() bool {
return lnt.noCache
}

func allFlagNames(fs *flag.FlagSet) []string {
var ret []string
fs.VisitAll(func(f *flag.Flag) {
Expand Down
9 changes: 9 additions & 0 deletions pkg/golinters/goanalysis/metalinter.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,12 @@ func (ml MetaLinter) getAnalyzerToLinterNameMapping() map[*analysis.Analyzer]str
}
return analyzerToLinterName
}

func (ml MetaLinter) withoutCache() bool {
for _, l := range ml.linters {
if l.withoutCache() {
return true
}
}
return false
}
9 changes: 7 additions & 2 deletions pkg/golinters/goanalysis/runners.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type runAnalyzersConfig interface {
useOriginalPackages() bool
reportIssues(*linter.Context) []Issue
getLoadMode() LoadMode
withoutCache() bool
}

func runAnalyzers(cfg runAnalyzersConfig, lintCtx *linter.Context) ([]result.Issue, error) {
Expand All @@ -41,7 +42,11 @@ func runAnalyzers(cfg runAnalyzersConfig, lintCtx *linter.Context) ([]result.Iss
pkgs = lintCtx.OriginalPackages
}

issues, pkgsFromCache := loadIssuesFromCache(pkgs, lintCtx, cfg.getAnalyzers())
var issues []result.Issue
var pkgsFromCache map[*packages.Package]bool
if !cfg.withoutCache() {
issues, pkgsFromCache = loadIssuesFromCache(pkgs, lintCtx, cfg.getAnalyzers())
}
var pkgsToAnalyze []*packages.Package
for _, pkg := range pkgs {
if !pkgsFromCache[pkg] {
Expand All @@ -52,7 +57,7 @@ func runAnalyzers(cfg runAnalyzersConfig, lintCtx *linter.Context) ([]result.Iss
diags, errs, passToPkg := runner.run(cfg.getAnalyzers(), pkgsToAnalyze)

defer func() {
if len(errs) == 0 {
if len(errs) == 0 && !cfg.withoutCache() {
// If we try to save to cache even if we have compilation errors
// we won't see them on repeated runs.
saveIssuesToCache(pkgs, pkgsFromCache, issues, lintCtx, cfg.getAnalyzers())
Expand Down