Skip to content

Commit 4100dac

Browse files
committed
go/analysis/passes/deepequalerrors: add typeparams test
This CL adds a test for the assign pass that involves use of generics. Update golang/go#48704 Change-Id: I1aa51aed24d63d42b6b0150d64925b97dfd59a92 Reviewed-on: https://go-review.googlesource.com/c/tools/+/357412 Run-TryBot: Zvonimir Pavlinovic <[email protected]> gopls-CI: kokoro <[email protected]> Reviewed-by: Robert Findley <[email protected]> Trust: Cherry Mui <[email protected]>
1 parent 9b675d0 commit 4100dac

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

go/analysis/passes/deepequalerrors/deepequalerrors_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ import (
99

1010
"golang.org/x/tools/go/analysis/analysistest"
1111
"golang.org/x/tools/go/analysis/passes/deepequalerrors"
12+
"golang.org/x/tools/internal/typeparams"
1213
)
1314

1415
func Test(t *testing.T) {
1516
testdata := analysistest.TestData()
16-
analysistest.Run(t, testdata, deepequalerrors.Analyzer, "a")
17+
tests := []string{"a"}
18+
if typeparams.Enabled {
19+
tests = append(tests, "typeparams")
20+
}
21+
analysistest.Run(t, testdata, deepequalerrors.Analyzer, tests...)
1722
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2021 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+
// This file contains tests for the deepequalerrors checker.
6+
7+
package a
8+
9+
import (
10+
"io"
11+
"os"
12+
"reflect"
13+
)
14+
15+
type myError int
16+
17+
func (myError) Error() string { return "" }
18+
19+
func bad[T any]() T {
20+
var t T
21+
return t
22+
}
23+
24+
type s1 struct {
25+
s2 *s2[myError2]
26+
i int
27+
}
28+
29+
type myError2 error
30+
31+
type s2[T any] struct {
32+
s1 *s1
33+
errs []*T
34+
}
35+
36+
func hasError() {
37+
var e error
38+
var m myError2
39+
reflect.DeepEqual(bad[error](), e) // want `avoid using reflect.DeepEqual with errors`
40+
reflect.DeepEqual(io.EOF, io.EOF) // want `avoid using reflect.DeepEqual with errors`
41+
reflect.DeepEqual(e, &e) // want `avoid using reflect.DeepEqual with errors`
42+
reflect.DeepEqual(e, m) // want `avoid using reflect.DeepEqual with errors`
43+
reflect.DeepEqual(e, s1{}) // want `avoid using reflect.DeepEqual with errors`
44+
reflect.DeepEqual(e, [1]error{}) // want `avoid using reflect.DeepEqual with errors`
45+
reflect.DeepEqual(e, map[error]int{}) // want `avoid using reflect.DeepEqual with errors`
46+
reflect.DeepEqual(e, map[int]error{}) // want `avoid using reflect.DeepEqual with errors`
47+
// We catch the next not because *os.PathError implements error, but because it contains
48+
// a field Err of type error.
49+
reflect.DeepEqual(&os.PathError{}, io.EOF) // want `avoid using reflect.DeepEqual with errors`
50+
51+
}
52+
53+
func notHasError() {
54+
reflect.ValueOf(4) // not reflect.DeepEqual
55+
reflect.DeepEqual(3, 4) // not errors
56+
reflect.DeepEqual(5, io.EOF) // only one error
57+
reflect.DeepEqual(myError(1), io.EOF) // not types that implement error
58+
}

0 commit comments

Comments
 (0)