Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: golangci/golangci-lint
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: sylvia7788/golangci-lint
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: contextcheck
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 1 commit
  • 6 files changed
  • 1 contributor

Commits on Jan 27, 2022

  1. Copy the full SHA
    e76d134 View commit details
Showing with 34 additions and 6 deletions.
  1. +1 −1 go.mod
  2. +2 −2 go.sum
  3. +5 −1 pkg/golinters/contextcheck.go
  4. +10 −0 pkg/golinters/goanalysis/linter.go
  5. +9 −0 pkg/golinters/goanalysis/metalinter.go
  6. +7 −2 pkg/golinters/goanalysis/runners.go
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
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
@@ -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 {
@@ -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
@@ -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 {
@@ -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
}
@@ -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) {
9 changes: 9 additions & 0 deletions pkg/golinters/goanalysis/metalinter.go
Original file line number Diff line number Diff line change
@@ -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
@@ -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) {
@@ -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] {
@@ -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())