1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-31 05:04:23 +03:00
This commit is contained in:
ofekshenawa
2024-06-20 02:30:37 +03:00
commit 0b95fd7fa5
188 changed files with 45644 additions and 0 deletions

1
internal/customvet/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/customvet

View File

@ -0,0 +1,62 @@
package setval
import (
"go/ast"
"go/token"
"go/types"
"golang.org/x/tools/go/analysis"
)
var Analyzer = &analysis.Analyzer{
Name: "setval",
Doc: "find Cmder types that are missing a SetVal method",
Run: func(pass *analysis.Pass) (interface{}, error) {
cmderTypes := make(map[string]token.Pos)
typesWithSetValMethod := make(map[string]bool)
for _, file := range pass.Files {
for _, decl := range file.Decls {
funcName, receiverType := parseFuncDecl(decl, pass.TypesInfo)
switch funcName {
case "Result":
cmderTypes[receiverType] = decl.Pos()
case "SetVal":
typesWithSetValMethod[receiverType] = true
}
}
}
for cmder, pos := range cmderTypes {
if !typesWithSetValMethod[cmder] {
pass.Reportf(pos, "%s is missing a SetVal method", cmder)
}
}
return nil, nil
},
}
func parseFuncDecl(decl ast.Decl, typesInfo *types.Info) (funcName, receiverType string) {
funcDecl, ok := decl.(*ast.FuncDecl)
if !ok {
return "", "" // Not a function declaration.
}
if funcDecl.Recv == nil {
return "", "" // Not a method.
}
if len(funcDecl.Recv.List) != 1 {
return "", "" // Unexpected number of receiver arguments. (Can this happen?)
}
receiverTypeObj := typesInfo.TypeOf(funcDecl.Recv.List[0].Type)
if receiverTypeObj == nil {
return "", "" // Unable to determine the receiver type.
}
return funcDecl.Name.Name, receiverTypeObj.String()
}

View File

@ -0,0 +1,14 @@
package setval_test
import (
"testing"
"golang.org/x/tools/go/analysis/analysistest"
"github.com/redis/go-redis/internal/customvet/checks/setval"
)
func Test(t *testing.T) {
testdata := analysistest.TestData()
analysistest.Run(t, testdata, setval.Analyzer, "a")
}

View File

@ -0,0 +1,29 @@
package a
type GoodCmd struct {
val int
}
func (c *GoodCmd) SetVal(val int) {
c.val = val
}
func (c *GoodCmd) Result() (int, error) {
return c.val, nil
}
type BadCmd struct {
val int
}
func (c *BadCmd) Result() (int, error) { // want "\\*a.BadCmd is missing a SetVal method"
return c.val, nil
}
type NotACmd struct {
val int
}
func (c *NotACmd) Val() int {
return c.val
}

10
internal/customvet/go.mod Normal file
View File

@ -0,0 +1,10 @@
module github.com/redis/go-redis/internal/customvet
go 1.17
require golang.org/x/tools v0.5.0
require (
golang.org/x/mod v0.7.0 // indirect
golang.org/x/sys v0.4.0 // indirect
)

View File

@ -0,0 +1,7 @@
golang.org/x/mod v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA=
golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.5.0 h1:+bSpV5HIeWkuvgaMfI3UmKRThoTA5ODJTUd8T17NO+4=
golang.org/x/tools v0.5.0/go.mod h1:N+Kgy78s5I24c24dU8OfWNEotWjutIs8SnJvn5IDq+k=

View File

@ -0,0 +1,13 @@
package main
import (
"golang.org/x/tools/go/analysis/multichecker"
"github.com/redis/go-redis/internal/customvet/checks/setval"
)
func main() {
multichecker.Main(
setval.Analyzer,
)
}