Skip to content

Commit 499dc1c

Browse files
committed
go/doc: ignore example functions with arguments
An Example function with arguments is not a valid example to be run with go test. Don't return those functions from Examples. This means that some functions that were previously showing up in Examples will no longer show up. But those functions were not being tested properly so the fact that they were showing up is misleading. This fixes an issue where a confusing compiler error was showing up when running go test on a file with an invalid example. While that issue could have been fixed by returning an error, this is more consistent with the behavior of go/doc.Examples, and the tests checker in vet will catch this issue. Fixes #35284 Change-Id: I2101a7d19f38522ef9c2e50967f9cfb30d28c730 Reviewed-on: https://go-review.googlesource.com/c/go/+/211357 Run-TryBot: Michael Matloob <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent a3dc6da commit 499dc1c

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Tests that invalid examples are ignored.
2+
# Verifies golang.org/issue/35284
3+
go test x_test.go
4+
5+
-- x_test.go --
6+
package x
7+
8+
import "fmt"
9+
10+
func ExampleThisShouldNotHaveAParameter(thisShouldntExist int) {
11+
fmt.Println("X")
12+
// Output:
13+
}

src/go/doc/example.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ func Examples(testFiles ...*ast.File) []*Example {
6262
if !ok || f.Recv != nil {
6363
continue
6464
}
65+
if params := f.Type.Params; params.List != nil {
66+
continue // function has params; not a valid example
67+
}
6568
numDecl++
6669
name := f.Name.Name
6770
if isTest(name, "Test") || isTest(name, "Benchmark") {

0 commit comments

Comments
 (0)