Skip to content

feat: add contextcheck linter #2216

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

Merged
merged 3 commits into from
Oct 4, 2021
Merged
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
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
19 changes: 16 additions & 3 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 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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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)
}