-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ab17bd
commit 30b02e6
Showing
5 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |