|
| 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