-
-
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.
feat: add contextcheck linter (#2216)
- Loading branch information
1 parent
2fb6563
commit 7fc2fe8
Showing
5 changed files
with
90 additions
and
2 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 the function whether use a non-inherited context", | ||
[]*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,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) | ||
} |