diff options
| author | Taras Madan <tarasmadan@google.com> | 2024-11-11 11:41:38 +0100 |
|---|---|---|
| committer | Taras Madan <tarasmadan@google.com> | 2024-11-11 11:10:48 +0000 |
| commit | 27e76fae2ee2d84dc7db63af1d9ed7358ba35b7a (patch) | |
| tree | ed19c0e35e272b3c4cc5a2f2c595e035b2428337 /vendor/github.com/raeperd | |
| parent | 621e84e063b0e15b23e17780338627c509e1b9e8 (diff) | |
vendor: update
Diffstat (limited to 'vendor/github.com/raeperd')
| -rw-r--r-- | vendor/github.com/raeperd/recvcheck/.gitignore | 2 | ||||
| -rw-r--r-- | vendor/github.com/raeperd/recvcheck/LICENSE | 21 | ||||
| -rw-r--r-- | vendor/github.com/raeperd/recvcheck/Makefile | 14 | ||||
| -rw-r--r-- | vendor/github.com/raeperd/recvcheck/README.md | 52 | ||||
| -rw-r--r-- | vendor/github.com/raeperd/recvcheck/analyzer.go | 69 |
5 files changed, 158 insertions, 0 deletions
diff --git a/vendor/github.com/raeperd/recvcheck/.gitignore b/vendor/github.com/raeperd/recvcheck/.gitignore new file mode 100644 index 000000000..035dc07e3 --- /dev/null +++ b/vendor/github.com/raeperd/recvcheck/.gitignore @@ -0,0 +1,2 @@ +coverage.txt +cmd/recvcheck/recvcheck diff --git a/vendor/github.com/raeperd/recvcheck/LICENSE b/vendor/github.com/raeperd/recvcheck/LICENSE new file mode 100644 index 000000000..a46db59be --- /dev/null +++ b/vendor/github.com/raeperd/recvcheck/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 raeperd + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/vendor/github.com/raeperd/recvcheck/Makefile b/vendor/github.com/raeperd/recvcheck/Makefile new file mode 100644 index 000000000..45ca47d9b --- /dev/null +++ b/vendor/github.com/raeperd/recvcheck/Makefile @@ -0,0 +1,14 @@ +all: build test lint + +download: + go mod download + +build: download + go build -C cmd/recvcheck + +test: + go test -race -coverprofile=coverage.txt . + +lint: + golangci-lint run + diff --git a/vendor/github.com/raeperd/recvcheck/README.md b/vendor/github.com/raeperd/recvcheck/README.md new file mode 100644 index 000000000..db84fe38e --- /dev/null +++ b/vendor/github.com/raeperd/recvcheck/README.md @@ -0,0 +1,52 @@ +# recvcheck +[](https://github.com/raeperd/recvcheck/actions/workflows/build.yaml) [](https://goreportcard.com/report/github.com/raeperd/recvcheck) [](https://codecov.io/gh/raeperd/recvcheck) +Golang linter for check receiver type in method + +## Motivtation +From [Go Wiki: Go Code Review Comments - The Go Programming Language](https://go.dev/wiki/CodeReviewComments#receiver-type) +> Don’t mix receiver types. Choose either pointers or struct types for all available method + +Following code from [Dave Chenney](https://dave.cheney.net/2015/11/18/wednesday-pop-quiz-spot-the-race) causes data race. Could you find it? +This linter does it for you. + +```go +package main + +import ( + "fmt" + "time" +) + +type RPC struct { + result int + done chan struct{} +} + +func (rpc *RPC) compute() { + time.Sleep(time.Second) // strenuous computation intensifies + rpc.result = 42 + close(rpc.done) +} + +func (RPC) version() int { + return 1 // never going to need to change this +} + +func main() { + rpc := &RPC{done: make(chan struct{})} + + go rpc.compute() // kick off computation in the background + version := rpc.version() // grab some other information while we're waiting + <-rpc.done // wait for computation to finish + result := rpc.result + + fmt.Printf("RPC computation complete, result: %d, version: %d\n", result, version) +} +``` + +## References +- [Is there a way to detect following data race code using golangci-lint or other linter?? · golangci/golangci-lint · Discussion #5006](https://github.com/golangci/golangci-lint/discussions/5006) + - [Wednesday pop quiz: spot the race | Dave Cheney](https://dave.cheney.net/2015/11/18/wednesday-pop-quiz-spot-the-race) + + + diff --git a/vendor/github.com/raeperd/recvcheck/analyzer.go b/vendor/github.com/raeperd/recvcheck/analyzer.go new file mode 100644 index 000000000..e80dfc577 --- /dev/null +++ b/vendor/github.com/raeperd/recvcheck/analyzer.go @@ -0,0 +1,69 @@ +package recvcheck + +import ( + "go/ast" + + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/analysis/passes/inspect" + "golang.org/x/tools/go/ast/inspector" +) + +var Analyzer = &analysis.Analyzer{ + Name: "recvcheck", + Doc: "checks for receiver type consistency", + Run: run, + Requires: []*analysis.Analyzer{inspect.Analyzer}, +} + +func run(pass *analysis.Pass) (any, error) { + inspector := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) + + structs := map[string]*structType{} + inspector.Preorder([]ast.Node{(*ast.FuncDecl)(nil)}, func(n ast.Node) { + funcDecl, ok := n.(*ast.FuncDecl) + if !ok || funcDecl.Recv == nil || len(funcDecl.Recv.List) != 1 { + return + } + + var recv *ast.Ident + var isStar bool + switch recvType := funcDecl.Recv.List[0].Type.(type) { + case *ast.StarExpr: + isStar = true + if recv, ok = recvType.X.(*ast.Ident); !ok { + return + } + case *ast.Ident: + recv = recvType + default: + return + } + + var st *structType + st, ok = structs[recv.Name] + if !ok { + structs[recv.Name] = &structType{recv: recv.Name} + st = structs[recv.Name] + } + + if isStar { + st.numStarMethod++ + } else { + st.numTypeMethod++ + } + }) + + for _, st := range structs { + if st.numStarMethod > 0 && st.numTypeMethod > 0 { + pass.Reportf(pass.Pkg.Scope().Lookup(st.recv).Pos(), "the methods of %q use pointer receiver and non-pointer receiver.", st.recv) + } + } + + return nil, nil +} + +type structType struct { + recv string + numStarMethod int + numTypeMethod int +} |
