| 1 | // Copyright 2015 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package testsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/ |
| 6 | |
| 7 | import ( |
| 8 | _ "embedscip-go gomod github.com/golang/go/src go1.25.0 embed/" |
| 9 | "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 10 | "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/" |
| 11 | "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/" |
| 12 | "regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/" |
| 13 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 14 | "unicodescip-go gomod github.com/golang/go/src go1.25.0 unicode/" |
| 15 | "unicode/utf8scip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/" |
| 16 | |
| 17 | "golang.org/x/tools/go/analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/" |
| 18 | "golang.org/x/tools/internal/analysis/analyzerutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/" |
| 19 | "golang.org/x/tools/internal/astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/" |
| 20 | "golang.org/x/tools/internal/typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/" |
| 21 | ) |
| 22 | |
| 23 | //go:embed doc.go |
| 24 | var docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/doc. string |
| 25 | |
| 26 | var Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/Analyzer. = &analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{ |
| 27 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "tests", |
| 28 | Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Doc.: analyzerutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/.MustExtractDocscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/MustExtractDoc().(docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/doc., "tests"), |
| 29 | URLscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#URL.: "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/tests", |
| 30 | Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Run.: runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/run()., |
| 31 | } |
| 32 | |
| 33 | var acceptedFuzzTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/acceptedFuzzTypes. = []typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#{ |
| 34 | typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Typ.[typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Stringscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/String.], |
| 35 | typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Typ.[typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Boolscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Bool.], |
| 36 | typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Typ.[typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Float32scip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Float32.], |
| 37 | typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Typ.[typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Float64scip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Float64.], |
| 38 | typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Typ.[typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Intscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Int.], |
| 39 | typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Typ.[typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Int8scip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Int8.], |
| 40 | typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Typ.[typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Int16scip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Int16.], |
| 41 | typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Typ.[typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Int32scip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Int32.], |
| 42 | typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Typ.[typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Int64scip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Int64.], |
| 43 | typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Typ.[typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Uintscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Uint.], |
| 44 | typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Typ.[typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Uint8scip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Uint8.], |
| 45 | typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Typ.[typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Uint16scip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Uint16.], |
| 46 | typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Typ.[typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Uint32scip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Uint32.], |
| 47 | typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Typ.[typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Uint64scip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Uint64.], |
| 48 | typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.NewSlicescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/NewSlice().(typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Universescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Universe..Lookupscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Scope#Lookup().("byte").Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Type().()), |
| 49 | } |
| 50 | |
| 51 | func runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/run().(pass *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#) (any, error) { |
| 52 | for _, f := range pass.Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Files. { |
| 53 | if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(pass.Fsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Fset..Filescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#File().(f.FileStartscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#FileStart.).Namescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/File#Name().(), "_test.go") { |
| 54 | continue |
| 55 | } |
| 56 | for _, decl := range f.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls. { |
| 57 | fn, ok := decl.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#) |
| 58 | if !ok || fn.Recvscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Recv. != nil { |
| 59 | // Ignore non-functions or functions with receivers. |
| 60 | continue |
| 61 | } |
| 62 | switch { |
| 63 | case stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(fn.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Name..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name., "Example"): |
| 64 | checkExampleNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/checkExampleName().(pass, fn) |
| 65 | checkExampleOutputscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/checkExampleOutput().(pass, fn, f.Commentsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Comments.) |
| 66 | case stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(fn.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Name..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name., "Test"): |
| 67 | checkTestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/checkTest().(pass, fn, "Test") |
| 68 | case stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(fn.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Name..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name., "Benchmark"): |
| 69 | checkTestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/checkTest().(pass, fn, "Benchmark") |
| 70 | case stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(fn.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Name..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name., "Fuzz"): |
| 71 | checkTestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/checkTest().(pass, fn, "Fuzz") |
| 72 | checkFuzzscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/checkFuzz().(pass, fn) |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | return nil, nil |
| 77 | } |
| 78 | |
| 79 | // checkFuzz checks the contents of a fuzz function. |
| 80 | func checkFuzzscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/checkFuzz().(pass *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#, fn *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#) { |
| 81 | params := checkFuzzCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/checkFuzzCall().(pass, fn) |
| 82 | if params != nil { |
| 83 | checkAddCallsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/checkAddCalls().(pass, fn, params) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // checkFuzzCall checks the arguments of f.Fuzz() calls: |
| 88 | // |
| 89 | // 1. f.Fuzz() should call a function and it should be of type (*testing.F).Fuzz(). |
| 90 | // 2. The called function in f.Fuzz(func(){}) should not return result. |
| 91 | // 3. First argument of func() should be of type *testing.T |
| 92 | // 4. Second argument onwards should be of type []byte, string, bool, byte, |
| 93 | // rune, float32, float64, int, int8, int16, int32, int64, uint, uint8, uint16, |
| 94 | // uint32, uint64 |
| 95 | // 5. func() must not call any *F methods, e.g. (*F).Log, (*F).Error, (*F).Skip |
| 96 | // The only *F methods that are allowed in the (*F).Fuzz function are (*F).Failed and (*F).Name. |
| 97 | // |
| 98 | // Returns the list of parameters to the fuzz function, if they are valid fuzz parameters. |
| 99 | func checkFuzzCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/checkFuzzCall().(pass *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#, fn *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#) (params *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Tuplescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Tuple#) { |
| 100 | astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Inspectscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Inspect().(fn, func(n astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#) bool { |
| 101 | call, ok := n.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#) |
| 102 | if ok { |
| 103 | if !isFuzzTargetDotFuzzscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/isFuzzTargetDotFuzz().(pass, call) { |
| 104 | return true |
| 105 | } |
| 106 | |
| 107 | // Only one argument (func) must be passed to (*testing.F).Fuzz. |
| 108 | if len(call.Argsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Args.) != 1 { |
| 109 | return true |
| 110 | } |
| 111 | expr := call.Argsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Args.[0] |
| 112 | if pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo..Typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Types.[expr].Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeAndValue#Type. == nil { |
| 113 | return true |
| 114 | } |
| 115 | t := pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo..Typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Types.[expr].Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeAndValue#Type..Underlyingscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#Underlying().() |
| 116 | tSign, argOk := t.(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Signaturescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#) |
| 117 | // Argument should be a function |
| 118 | if !argOk { |
| 119 | pass.ReportRangefscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ReportRangef().(expr, "argument to Fuzz must be a function") |
| 120 | return false |
| 121 | } |
| 122 | // ff Argument function should not return |
| 123 | if tSign.Resultsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#Results().().Lenscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Tuple#Len().() != 0 { |
| 124 | pass.ReportRangefscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ReportRangef().(expr, "fuzz target must not return any value") |
| 125 | } |
| 126 | // ff Argument function should have 1 or more argument |
| 127 | if tSign.Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#Params().().Lenscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Tuple#Len().() == 0 { |
| 128 | pass.ReportRangefscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ReportRangef().(expr, "fuzz target must have 1 or more argument") |
| 129 | return false |
| 130 | } |
| 131 | ok := validateFuzzArgsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/validateFuzzArgs().(pass, tSign.Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#Params().(), expr) |
| 132 | if ok && params == nil { |
| 133 | params = tSign.Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#Params().() |
| 134 | } |
| 135 | // Inspect the function that was passed as an argument to make sure that |
| 136 | // there are no calls to *F methods, except for Name and Failed. |
| 137 | astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Inspectscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Inspect().(expr, func(n astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#) bool { |
| 138 | if call, ok := n.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#); ok { |
| 139 | if !isFuzzTargetDotscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/isFuzzTargetDot().(pass, call, "") { |
| 140 | return true |
| 141 | } |
| 142 | if !isFuzzTargetDotscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/isFuzzTargetDot().(pass, call, "Name") && !isFuzzTargetDotscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/isFuzzTargetDot().(pass, call, "Failed") { |
| 143 | pass.ReportRangefscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ReportRangef().(call, "fuzz target must not call any *F methods") |
| 144 | } |
| 145 | } |
| 146 | return true |
| 147 | }) |
| 148 | // We do not need to look at any calls to f.Fuzz inside of a Fuzz call, |
| 149 | // since they are not allowed. |
| 150 | return false |
| 151 | } |
| 152 | return true |
| 153 | }) |
| 154 | return params |
| 155 | } |
| 156 | |
| 157 | // checkAddCalls checks that the arguments of f.Add calls have the same number and type of arguments as |
| 158 | // the signature of the function passed to (*testing.F).Fuzz |
| 159 | func checkAddCallsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/checkAddCalls().(pass *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#, fn *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#, params *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Tuplescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Tuple#) { |
| 160 | astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Inspectscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Inspect().(fn, func(n astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#) bool { |
| 161 | call, ok := n.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#) |
| 162 | if ok { |
| 163 | if !isFuzzTargetDotAddscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/isFuzzTargetDotAdd().(pass, call) { |
| 164 | return true |
| 165 | } |
| 166 | |
| 167 | // The first argument to function passed to (*testing.F).Fuzz is (*testing.T). |
| 168 | if len(call.Argsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Args.) != params.Lenscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Tuple#Len().()-1 { |
| 169 | pass.ReportRangefscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ReportRangef().(call, "wrong number of values in call to (*testing.F).Add: %d, fuzz target expects %d", len(call.Argsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Args.), params.Lenscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Tuple#Len().()-1) |
| 170 | return true |
| 171 | } |
| 172 | var mismatched []int |
| 173 | for i, expr := range call.Argsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Args. { |
| 174 | if pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo..Typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Types.[expr].Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeAndValue#Type. == nil { |
| 175 | return true |
| 176 | } |
| 177 | t := pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo..Typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Types.[expr].Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeAndValue#Type. |
| 178 | if !typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Identicalscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Identical().(t, params.Atscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Tuple#At().(i+1).Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Type().()) { |
| 179 | mismatched = append(mismatched, i) |
| 180 | } |
| 181 | } |
| 182 | // If just one of the types is mismatched report for that |
| 183 | // type only. Otherwise report for the whole call to (*testing.F).Add |
| 184 | if len(mismatched) == 1 { |
| 185 | i := mismatched[0] |
| 186 | expr := call.Argsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Args.[i] |
| 187 | t := pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo..Typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Types.[expr].Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeAndValue#Type. |
| 188 | pass.ReportRangefscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ReportRangef().(expr, "mismatched type in call to (*testing.F).Add: %v, fuzz target expects %v", t, params.Atscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Tuple#At().(i+1).Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Type().()) |
| 189 | } else if len(mismatched) > 1 { |
| 190 | var gotArgs, wantArgs []typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type# |
| 191 | for i := 0; i < len(call.Argsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Args.); i++ { |
| 192 | gotArgs, wantArgs = append(gotArgs, pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo..Typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Types.[call.Argsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Args.[i]].Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeAndValue#Type.), append(wantArgs, params.Atscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Tuple#At().(i+1).Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Type().()) |
| 193 | } |
| 194 | pass.ReportRangefscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ReportRangef().(call, "mismatched types in call to (*testing.F).Add: %v, fuzz target expects %v", gotArgs, wantArgs) |
| 195 | } |
| 196 | } |
| 197 | return true |
| 198 | }) |
| 199 | } |
| 200 | |
| 201 | // isFuzzTargetDotFuzz reports whether call is (*testing.F).Fuzz(). |
| 202 | func isFuzzTargetDotFuzzscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/isFuzzTargetDotFuzz().(pass *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#, call *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#) bool { |
| 203 | return isFuzzTargetDotscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/isFuzzTargetDot().(pass, call, "Fuzz") |
| 204 | } |
| 205 | |
| 206 | // isFuzzTargetDotAdd reports whether call is (*testing.F).Add(). |
| 207 | func isFuzzTargetDotAddscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/isFuzzTargetDotAdd().(pass *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#, call *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#) bool { |
| 208 | return isFuzzTargetDotscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/isFuzzTargetDot().(pass, call, "Add") |
| 209 | } |
| 210 | |
| 211 | // isFuzzTargetDot reports whether call is (*testing.F).<name>(). |
| 212 | func isFuzzTargetDotscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/isFuzzTargetDot().(pass *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#, call *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#, name string) bool { |
| 213 | if selExpr, ok := call.Funscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Fun..(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.SelectorExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#); ok { |
| 214 | if !isTestingTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/isTestingType().(pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo..Typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Types.[selExpr.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#X.].Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeAndValue#Type., "F") { |
| 215 | return false |
| 216 | } |
| 217 | if name == "" || selExpr.Selscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#Sel..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name. == name { |
| 218 | return true |
| 219 | } |
| 220 | } |
| 221 | return false |
| 222 | } |
| 223 | |
| 224 | // Validate the arguments of fuzz target. |
| 225 | func validateFuzzArgsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/validateFuzzArgs().(pass *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#, params *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Tuplescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Tuple#, expr astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Exprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Expr#) bool { |
| 226 | fLit, isFuncLit := expr.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncLitscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncLit#) |
| 227 | exprRange := expr |
| 228 | ok := true |
| 229 | if !isTestingTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/isTestingType().(params.Atscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Tuple#At().(0).Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Type().(), "T") { |
| 230 | if isFuncLit { |
| 231 | exprRange = fLit.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncLit#Type..Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Params..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List.[0].Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Field#Type. |
| 232 | } |
| 233 | pass.ReportRangefscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ReportRangef().(exprRange, "the first parameter of a fuzz target must be *testing.T") |
| 234 | ok = false |
| 235 | } |
| 236 | for i := 1; i < params.Lenscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Tuple#Len().(); i++ { |
| 237 | if !isAcceptedFuzzTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/isAcceptedFuzzType().(params.Atscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Tuple#At().(i).Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/object#Type().()) { |
| 238 | if isFuncLit { |
| 239 | curr := 0 |
| 240 | for _, field := range fLit.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncLit#Type..Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Params..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List. { |
| 241 | curr += len(field.Namesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Field#Names.) |
| 242 | if i < curr { |
| 243 | exprRange = field.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Field#Type. |
| 244 | break |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | pass.ReportRangefscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ReportRangef().(exprRange, "fuzzing arguments can only have the following types: %s", formatAcceptedFuzzTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/formatAcceptedFuzzType().()) |
| 249 | ok = false |
| 250 | } |
| 251 | } |
| 252 | return ok |
| 253 | } |
| 254 | |
| 255 | func isTestingTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/isTestingType().(typ typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#, testingType string) bool { |
| 256 | // No Unalias here: I doubt "go test" recognizes |
| 257 | // "type A = *testing.T; func Test(A) {}" as a test. |
| 258 | ptr, ok := typ.(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Pointerscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Pointer#) |
| 259 | if !ok { |
| 260 | return false |
| 261 | } |
| 262 | return typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/.IsTypeNamedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/IsTypeNamed().(ptr.Elemscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Pointer#Elem().(), "testing", testingType) |
| 263 | } |
| 264 | |
| 265 | // Validate that fuzz target function's arguments are of accepted types. |
| 266 | func isAcceptedFuzzTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/isAcceptedFuzzType().(paramType typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#) bool { |
| 267 | for _, typ := range acceptedFuzzTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/acceptedFuzzTypes. { |
| 268 | if typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Identicalscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Identical().(typ, paramType) { |
| 269 | return true |
| 270 | } |
| 271 | } |
| 272 | return false |
| 273 | } |
| 274 | |
| 275 | func formatAcceptedFuzzTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/formatAcceptedFuzzType().() string { |
| 276 | var acceptedFuzzTypesStrings []string |
| 277 | for _, typ := range acceptedFuzzTypesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/acceptedFuzzTypes. { |
| 278 | acceptedFuzzTypesStrings = append(acceptedFuzzTypesStrings, typ.Stringscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Type#String().()) |
| 279 | } |
| 280 | acceptedFuzzTypesMsg := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(acceptedFuzzTypesStrings, ", ") |
| 281 | return acceptedFuzzTypesMsg |
| 282 | } |
| 283 | |
| 284 | func isExampleSuffixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/isExampleSuffix().(s string) bool { |
| 285 | r, size := utf8scip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/.DecodeRuneInStringscip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/DecodeRuneInString().(s) |
| 286 | return size > 0 && unicodescip-go gomod github.com/golang/go/src go1.25.0 unicode/.IsLowerscip-go gomod github.com/golang/go/src go1.25.0 unicode/IsLower().(r) |
| 287 | } |
| 288 | |
| 289 | func isTestSuffixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/isTestSuffix().(name string) bool { |
| 290 | if len(name) == 0 { |
| 291 | // "Test" is ok. |
| 292 | return true |
| 293 | } |
| 294 | r, _ := utf8scip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/.DecodeRuneInStringscip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/DecodeRuneInString().(name) |
| 295 | return !unicodescip-go gomod github.com/golang/go/src go1.25.0 unicode/.IsLowerscip-go gomod github.com/golang/go/src go1.25.0 unicode/IsLower().(r) |
| 296 | } |
| 297 | |
| 298 | func isTestParamscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/isTestParam().(typ astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Exprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Expr#, wantType string) bool { |
| 299 | ptr, ok := typ.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.StarExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/StarExpr#) |
| 300 | if !ok { |
| 301 | // Not a pointer. |
| 302 | return false |
| 303 | } |
| 304 | // No easy way of making sure it's a *testing.T or *testing.B: |
| 305 | // ensure the name of the type matches. |
| 306 | if name, ok := ptr.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/StarExpr#X..(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#); ok { |
| 307 | return name.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name. == wantType |
| 308 | } |
| 309 | if sel, ok := ptr.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/StarExpr#X..(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.SelectorExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#); ok { |
| 310 | return sel.Selscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#Sel..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name. == wantType |
| 311 | } |
| 312 | return false |
| 313 | } |
| 314 | |
| 315 | func lookupscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/lookup().(pkg *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Packagescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#, name string) []typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Objectscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object# { |
| 316 | if o := pkg.Scopescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Scope().().Lookupscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Scope#Lookup().(name); o != nil { |
| 317 | return []typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Objectscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#{o} |
| 318 | } |
| 319 | |
| 320 | var ret []typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Objectscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object# |
| 321 | // Search through the imports to see if any of them define name. |
| 322 | // It's hard to tell in general which package is being tested, so |
| 323 | // for the purposes of the analysis, allow the object to appear |
| 324 | // in any of the imports. This guarantees there are no false positives |
| 325 | // because the example needs to use the object so it must be defined |
| 326 | // in the package or one if its imports. On the other hand, false |
| 327 | // negatives are possible, but should be rare. |
| 328 | for _, imp := range pkg.Importsscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Imports().() { |
| 329 | if obj := imp.Scopescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Scope().().Lookupscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Scope#Lookup().(name); obj != nil { |
| 330 | ret = append(ret, obj) |
| 331 | } |
| 332 | } |
| 333 | return ret |
| 334 | } |
| 335 | |
| 336 | // This pattern is taken from /go/src/go/doc/example.go |
| 337 | var outputRescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/outputRe. = regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.25.0 regexp/MustCompile().(`(?i)^[[:space:]]*(unordered )?output:`) |
| 338 | |
| 339 | type commentMetadatascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/commentMetadata# struct { |
| 340 | isOutputscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/commentMetadata#isOutput. bool |
| 341 | posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/commentMetadata#pos. tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Pos# |
| 342 | } |
| 343 | |
| 344 | func checkExampleOutputscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/checkExampleOutput().(pass *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#, fn *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#, fileComments []*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CommentGroupscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#) { |
| 345 | commentsInExample := []commentMetadatascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/commentMetadata#{} |
| 346 | numOutputs := 0 |
| 347 | |
| 348 | // Find the comment blocks that are in the example. These comments are |
| 349 | // guaranteed to be in order of appearance. |
| 350 | for _, cg := range fileComments { |
| 351 | if cg.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#Pos().() < fn.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Pos().() { |
| 352 | continue |
| 353 | } else if cg.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#End().() > fn.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#End().() { |
| 354 | break |
| 355 | } |
| 356 | |
| 357 | isOutput := outputRescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/outputRe..MatchStringscip-go gomod github.com/golang/go/src go1.25.0 regexp/Regexp#MatchString().(cg.Textscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#Text().()) |
| 358 | if isOutput { |
| 359 | numOutputs++ |
| 360 | } |
| 361 | |
| 362 | commentsInExample = append(commentsInExample, commentMetadatascip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/commentMetadata#{ |
| 363 | isOutputscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/commentMetadata#isOutput.: isOutput, |
| 364 | posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/commentMetadata#pos.: cg.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CommentGroup#Pos().(), |
| 365 | }) |
| 366 | } |
| 367 | |
| 368 | // Change message based on whether there are multiple output comment blocks. |
| 369 | msg := "output comment block must be the last comment block" |
| 370 | if numOutputs > 1 { |
| 371 | msg = "there can only be one output comment block per example" |
| 372 | } |
| 373 | |
| 374 | for i, cg := range commentsInExample { |
| 375 | // Check for output comments that are not the last comment in the example. |
| 376 | isLast := (i == len(commentsInExample)-1) |
| 377 | if cg.isOutputscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/commentMetadata#isOutput. && !isLast { |
| 378 | pass.Reportscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Report.( |
| 379 | analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Diagnosticscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#{ |
| 380 | Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#Pos.: cg.posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/commentMetadata#pos., |
| 381 | Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#Message.: msg, |
| 382 | }, |
| 383 | ) |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | func checkExampleNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/checkExampleName().(pass *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#, fn *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#) { |
| 389 | fnName := fn.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Name..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name. |
| 390 | if params := fn.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Params.; len(params.Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List.) != 0 { |
| 391 | pass.Reportfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Reportf().(fn.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Pos().(), "%s should be niladic", fnName) |
| 392 | } |
| 393 | if results := fn.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Resultsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Results.; results != nil && len(results.Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List.) != 0 { |
| 394 | pass.Reportfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Reportf().(fn.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Pos().(), "%s should return nothing", fnName) |
| 395 | } |
| 396 | if tparams := fn.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..TypeParamsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#TypeParams.; tparams != nil && len(tparams.Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List.) > 0 { |
| 397 | pass.Reportfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Reportf().(fn.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Pos().(), "%s should not have type params", fnName) |
| 398 | } |
| 399 | |
| 400 | if fnName == "Example" { |
| 401 | // Nothing more to do. |
| 402 | return |
| 403 | } |
| 404 | |
| 405 | var ( |
| 406 | exName = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/TrimPrefix().(fnName, "Example") |
| 407 | elems = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.SplitNscip-go gomod github.com/golang/go/src go1.25.0 strings/SplitN().(exName, "_", 3) |
| 408 | ident = elems[0] |
| 409 | objs = lookupscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/lookup().(pass.Pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Pkg., ident) |
| 410 | ) |
| 411 | if ident != "" && len(objs) == 0 { |
| 412 | // Check ExampleFoo and ExampleBadFoo. |
| 413 | pass.Reportfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Reportf().(fn.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Pos().(), "%s refers to unknown identifier: %s", fnName, ident) |
| 414 | // Abort since obj is absent and no subsequent checks can be performed. |
| 415 | return |
| 416 | } |
| 417 | if len(elems) < 2 { |
| 418 | // Nothing more to do. |
| 419 | return |
| 420 | } |
| 421 | |
| 422 | if ident == "" { |
| 423 | // Check Example_suffix and Example_BadSuffix. |
| 424 | if residual := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/TrimPrefix().(exName, "_"); !isExampleSuffixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/isExampleSuffix().(residual) { |
| 425 | pass.Reportfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Reportf().(fn.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Pos().(), "%s has malformed example suffix: %s", fnName, residual) |
| 426 | } |
| 427 | return |
| 428 | } |
| 429 | |
| 430 | mmbr := elems[1] |
| 431 | if !isExampleSuffixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/isExampleSuffix().(mmbr) { |
| 432 | // Check ExampleFoo_Method and ExampleFoo_BadMethod. |
| 433 | found := false |
| 434 | // Check if Foo.Method exists in this package or its imports. |
| 435 | for _, obj := range objs { |
| 436 | if obj, _, _ := typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.LookupFieldOrMethodscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/LookupFieldOrMethod().(obj.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Type().(), true, obj.Pkgscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Pkg().(), mmbr); obj != nil { |
| 437 | found = true |
| 438 | break |
| 439 | } |
| 440 | } |
| 441 | if !found { |
| 442 | pass.Reportfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Reportf().(fn.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Pos().(), "%s refers to unknown field or method: %s.%s", fnName, ident, mmbr) |
| 443 | } |
| 444 | } |
| 445 | if len(elems) == 3 && !isExampleSuffixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/isExampleSuffix().(elems[2]) { |
| 446 | // Check ExampleFoo_Method_suffix and ExampleFoo_Method_Badsuffix. |
| 447 | pass.Reportfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Reportf().(fn.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Pos().(), "%s has malformed example suffix: %s", fnName, elems[2]) |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | func checkTestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/checkTest().(pass *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#, fn *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#, prefix string) { |
| 452 | // Want functions with 0 results and 1 parameter. |
| 453 | if fn.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Resultsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Results. != nil && len(fn.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Resultsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Results..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List.) > 0 || |
| 454 | fn.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Params. == nil || |
| 455 | len(fn.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Params..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List.) != 1 || |
| 456 | len(fn.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Params..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List.[0].Namesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Field#Names.) > 1 { |
| 457 | return |
| 458 | } |
| 459 | |
| 460 | // The param must look like a *testing.T or *testing.B. |
| 461 | if !isTestParamscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/isTestParam().(fn.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..Paramsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#Params..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List.[0].Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Field#Type., prefix[:1]) { |
| 462 | return |
| 463 | } |
| 464 | |
| 465 | if tparams := fn.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Type..TypeParamsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#TypeParams.; tparams != nil && len(tparams.Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#List.) > 0 { |
| 466 | // Note: cmd/go/internal/load also errors about TestXXX and BenchmarkXXX functions with type parameters. |
| 467 | // We have currently decided to also warn before compilation/package loading. This can help users in IDEs. |
| 468 | pass.ReportRangefscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ReportRangef().(astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.RangeOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/RangeOf().(tparams.Openingscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#Opening., tparams.Closingscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FieldList#Closing.), |
| 469 | "%s has type parameters: it will not be run by go test as a %sXXX function", |
| 470 | fn.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Name..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name., prefix) |
| 471 | } |
| 472 | |
| 473 | if !isTestSuffixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/tests`/isTestSuffix().(fn.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Name..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name.[len(prefix):]) { |
| 474 | pass.ReportRangefscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ReportRangef().(fn.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Name., "%s has malformed name: first letter after '%s' must not be lowercase", fn.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncDecl#Name..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name., prefix) |
| 475 | } |
| 476 | } |