Skip to content

Commit

Permalink
feat: add contextcheck linter
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvia7788 committed Sep 3, 2021
1 parent 4ab17bd commit 30b02e6
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ require (
github.com/spf13/viper v1.8.1
github.com/ssgreg/nlreturn/v2 v2.1.0
github.com/stretchr/testify v1.7.0
github.com/sylvia7788/contextcheck v1.0.3 // indirect
github.com/tdakkota/asciicheck v0.0.0-20200416200610-e657995f937b
github.com/tetafro/godot v1.4.9
github.com/timakin/bodyclose v0.0.0-20200424151742-cb6215831a94
Expand Down
17 changes: 17 additions & 0 deletions go.sum

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

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

import (
"github.com/sylvia7788/contextcheck"
"golang.org/x/tools/go/analysis"

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

func NewContextCheck() *goanalysis.Linter {
analyzer := contextcheck.NewAnalyzer()
return goanalysis.NewLinter(
"contextcheck",
"check for using context.Background() and context.TODO() directly",
[]*analysis.Analyzer{analyzer},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
5 changes: 5 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithLoadForGoAnalysis().
WithURL("https://github.com/Antonboom/errname").
WithSince("v1.42.0"),
linter.NewConfig(golinters.NewContextCheck()).
WithPresets(linter.PresetBugs).
WithLoadForGoAnalysis().
WithURL("https://github.com/sylvia7788/contextcheck").
WithSince("v1.0.0"),

// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
linter.NewConfig(golinters.NewNoLintLint()).
Expand Down
40 changes: 40 additions & 0 deletions test/testdata/contextcheck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//args: -Econtextcheck
package testdata

import "context"

type MyString string

func contextcheckCase1(ctx context.Context) {
funcWithoutCtx() // ERROR "Function `funcWithoutCtx` should pass the context parameter"
}

func contextcheckCase2(ctx context.Context) {
ctx = context.WithValue(ctx, MyString("aaa"), "aaaaaa")
funcWithCtx(ctx)

defer func() {
funcWithCtx(ctx)
}()

func(ctx context.Context) {
funcWithCtx(ctx)
}(ctx)

funcWithCtx(context.Background()) // ERROR `The context param may be context.TODO\(\) or context.Background\(\), please replace it with another way, such as context.WithValue\(ctx, key, val\)`
}

func contextcheckCase3(ctx context.Context) {
func() {
funcWithCtx(ctx)
}()

ctx = context.Background() // ERROR `Invalid call to get new context, please replace it with another way, such as context.WithValue\(ctx, key, val\)`
funcWithCtx(ctx)
}

func funcWithCtx(ctx context.Context) {}

func funcWithoutCtx() {
funcWithCtx(context.TODO())
}

0 comments on commit 30b02e6

Please sign in to comment.