github.com/cockroachdb/tools@v0.0.0-20230222021103-a6d27438930d/go/analysis/passes/deepequalerrors/testdata/src/a/a.go (about)

     1  // Copyright 2019 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() error { return nil }
    20  
    21  type s1 struct {
    22  	s2 *s2
    23  	i  int
    24  }
    25  
    26  type myError2 error
    27  
    28  type s2 struct {
    29  	s1   *s1
    30  	errs []*myError2
    31  }
    32  
    33  func hasError() {
    34  	var e error
    35  	var m myError2
    36  	reflect.DeepEqual(bad(), e)           // want `avoid using reflect.DeepEqual with errors`
    37  	reflect.DeepEqual(io.EOF, io.EOF)     // want `avoid using reflect.DeepEqual with errors`
    38  	reflect.DeepEqual(e, &e)              // want `avoid using reflect.DeepEqual with errors`
    39  	reflect.DeepEqual(e, m)               // want `avoid using reflect.DeepEqual with errors`
    40  	reflect.DeepEqual(e, s1{})            // want `avoid using reflect.DeepEqual with errors`
    41  	reflect.DeepEqual(e, [1]error{})      // want `avoid using reflect.DeepEqual with errors`
    42  	reflect.DeepEqual(e, map[error]int{}) // want `avoid using reflect.DeepEqual with errors`
    43  	reflect.DeepEqual(e, map[int]error{}) // want `avoid using reflect.DeepEqual with errors`
    44  	// We catch the next not because *os.PathError implements error, but because it contains
    45  	// a field Err of type error.
    46  	reflect.DeepEqual(&os.PathError{}, io.EOF) // want `avoid using reflect.DeepEqual with errors`
    47  
    48  }
    49  
    50  func notHasError() {
    51  	reflect.ValueOf(4)                    // not reflect.DeepEqual
    52  	reflect.DeepEqual(3, 4)               // not errors
    53  	reflect.DeepEqual(5, io.EOF)          // only one error
    54  	reflect.DeepEqual(myError(1), io.EOF) // not types that implement error
    55  }