Skip to content

Commit

Permalink
feat: add contextcheck linter (#2216)
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvia7788 committed Oct 4, 2021
1 parent 2fb6563 commit 7fc2fe8
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -77,6 +77,7 @@ require (
github.com/spf13/viper v1.9.0
github.com/ssgreg/nlreturn/v2 v2.2.1
github.com/stretchr/testify v1.7.0
github.com/sylvia7788/contextcheck v1.0.4
github.com/tdakkota/asciicheck v0.0.0-20200416200610-e657995f937b
github.com/tetafro/godot v1.4.11
github.com/timakin/bodyclose v0.0.0-20200424151742-cb6215831a94
Expand Down
18 changes: 16 additions & 2 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
@@ -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 the function whether use a non-inherited context",
[]*analysis.Analyzer{analyzer},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
5 changes: 5 additions & 0 deletions pkg/lint/lintersdb/manager.go
Expand Up @@ -529,6 +529,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetStyle).
WithLoadForGoAnalysis().
WithURL("https://github.com/sivchari/tenv"),
linter.NewConfig(golinters.NewContextCheck()).
WithPresets(linter.PresetBugs).
WithLoadForGoAnalysis().
WithURL("https://github.com/sylvia7788/contextcheck").
WithSince("v1.43.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
50 changes: 50 additions & 0 deletions test/testdata/contextcheck.go
@@ -0,0 +1,50 @@
//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 "Non-inherited new context, use function like `context.WithXXX` instead"
}

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

ctx = context.Background() // ERROR "Non-inherited new context, use function like `context.WithXXX` instead"
funcWithCtx(ctx)
}

func contextcheckCase4(ctx context.Context) {
ctx, cancel := getNewCtx(ctx)
defer cancel()
funcWithCtx(ctx)
}

func funcWithCtx(ctx context.Context) {}

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

func getNewCtx(ctx context.Context) (newCtx context.Context, cancel context.CancelFunc) {
return context.WithCancel(ctx)
}

0 comments on commit 7fc2fe8

Please sign in to comment.