go/analysis: test errorsas analysis with type params
Add tests for the errorsas analyzer pass with type
parameters.
Change-Id: I96d62c9ef9f194c42bfbfadcae730bf6caa7040f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/352989
Trust: Suzy Mueller <suzmue@golang.org>
Run-TryBot: Suzy Mueller <suzmue@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
diff --git a/go/analysis/passes/errorsas/errorsas_test.go b/go/analysis/passes/errorsas/errorsas_test.go
index 5ef8668..7908e89 100644
--- a/go/analysis/passes/errorsas/errorsas_test.go
+++ b/go/analysis/passes/errorsas/errorsas_test.go
@@ -12,9 +12,14 @@
"golang.org/x/tools/go/analysis/analysistest"
"golang.org/x/tools/go/analysis/passes/errorsas"
+ "golang.org/x/tools/internal/typeparams"
)
func Test(t *testing.T) {
testdata := analysistest.TestData()
- analysistest.Run(t, testdata, errorsas.Analyzer, "a")
+ tests := []string{"a"}
+ if typeparams.Enabled {
+ tests = append(tests, "typeparams")
+ }
+ analysistest.Run(t, testdata, errorsas.Analyzer, tests...)
}
diff --git a/go/analysis/passes/errorsas/testdata/src/typeparams/typeparams.go b/go/analysis/passes/errorsas/testdata/src/typeparams/typeparams.go
new file mode 100644
index 0000000..8b020ca
--- /dev/null
+++ b/go/analysis/passes/errorsas/testdata/src/typeparams/typeparams.go
@@ -0,0 +1,37 @@
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// This file contains tests for the errorsas checker.
+
+package a
+
+import "errors"
+
+type myError[T any] T
+
+func (myError[T]) Error() string { return "" }
+
+type twice[T any] struct {
+ t T
+}
+
+func perr[T any]() *T { return nil }
+
+func two[T any]() (error, *T) { return nil, nil }
+
+func _[E error](e E) {
+ var (
+ m myError[int]
+ tw twice[myError[int]]
+ )
+ errors.As(nil, &e)
+ errors.As(nil, &m) // *T where T implemements error
+ errors.As(nil, &tw.t) // *T where T implements error
+ errors.As(nil, perr[error]()) // *error, via a call
+
+ errors.As(nil, e) // want `second argument to errors.As must be a non-nil pointer to either a type that implements error, or to any interface type`
+ errors.As(nil, m) // want `second argument to errors.As must be a non-nil pointer to either a type that implements error, or to any interface type`
+ errors.As(nil, tw.t) // want `second argument to errors.As must be a non-nil pointer to either a type that implements error, or to any interface type`
+ errors.As(two[error]())
+}