github.com/getkalido/errcheck@v1.7.0-alpha/errcheck/testdata/src/a/main.go (about)

     1  // ensure that the package keyword is not equal to file beginning
     2  // to test correct position calculations.
     3  package a
     4  
     5  import (
     6  	"bytes"
     7  	"crypto/sha256"
     8  	"fmt"
     9  	"math/rand"
    10  	mrand "math/rand"
    11  	"os"
    12  )
    13  
    14  func a() error {
    15  	fmt.Println("this function returns an error") // ok, excluded
    16  	return nil
    17  }
    18  
    19  func b() (int, error) {
    20  	fmt.Println("this function returns an int and an error") // ok, excluded
    21  	return 0, nil
    22  }
    23  
    24  func c() int {
    25  	fmt.Println("this function returns an int") // ok, excluded
    26  	return 7
    27  }
    28  
    29  func rec() {
    30  	defer func() {
    31  		recover()     // want "unchecked error"
    32  		_ = recover() // ok, assigned to blank
    33  	}()
    34  	defer recover() // want "unchecked error"
    35  }
    36  
    37  type MyError string
    38  
    39  func (e MyError) Error() string {
    40  	return string(e)
    41  }
    42  
    43  func customError() error {
    44  	return MyError("an error occurred")
    45  }
    46  
    47  func customConcreteError() MyError {
    48  	return MyError("an error occurred")
    49  }
    50  
    51  func customConcreteErrorTuple() (int, MyError) {
    52  	return 0, MyError("an error occurred")
    53  }
    54  
    55  type MyPointerError string
    56  
    57  func (e *MyPointerError) Error() string {
    58  	return string(*e)
    59  }
    60  
    61  func customPointerError() *MyPointerError {
    62  	e := MyPointerError("an error occurred")
    63  	return &e
    64  }
    65  
    66  func customPointerErrorTuple() (int, *MyPointerError) {
    67  	e := MyPointerError("an error occurred")
    68  	return 0, &e
    69  }
    70  
    71  type ErrorMakerInterface interface {
    72  	MakeNilError() error
    73  }
    74  type ErrorMakerInterfaceWrapper interface {
    75  	ErrorMakerInterface
    76  }
    77  
    78  func main() {
    79  	// Single error return
    80  	_ = a() // ok, assigned to blank
    81  	a()     // want "unchecked error"
    82  
    83  	// Return another value and an error
    84  	_, _ = b() // ok, assigned to blank
    85  	b()        // want "unchecked error"
    86  
    87  	// Return a custom error type
    88  	_ = customError() // ok, assigned to blank
    89  	customError()     // want "unchecked error"
    90  
    91  	// Return a custom concrete error type
    92  	_ = customConcreteError()         // ok, assigned to blank
    93  	customConcreteError()             // want "unchecked error"
    94  	_, _ = customConcreteErrorTuple() // ok, assigned to blank
    95  	customConcreteErrorTuple()        // want "unchecked error"
    96  
    97  	// Return a custom pointer error type
    98  	_ = customPointerError()         // ok, assigned to blank
    99  	customPointerError()             // want "unchecked error"
   100  	_, _ = customPointerErrorTuple() // ok, assigned to blank
   101  	customPointerErrorTuple()        // want "unchecked error"
   102  
   103  	// Method with a single error return
   104  	x := t{}
   105  	_ = x.a() // ok, assigned to blank
   106  	x.a()     // want "unchecked error"
   107  
   108  	// Method call on a struct member
   109  	y := u{x}
   110  	_ = y.t.a() // ok, assigned to blank
   111  	y.t.a()     // want "unchecked error"
   112  
   113  	m1 := map[string]func() error{"a": a}
   114  	_ = m1["a"]() // ok, assigned to blank
   115  	m1["a"]()     // want "unchecked error"
   116  
   117  	// Additional cases for assigning errors to blank identifier
   118  	z, _ := b()    // ok, assigned to blank
   119  	_, w := a(), 5 // ok, assigned to blank
   120  
   121  	// Assign non error to blank identifier
   122  	_ = c()
   123  
   124  	_ = z + w // Avoid complaints about unused variables
   125  
   126  	// Type assertions
   127  	var i interface{}
   128  	s1 := i.(string)    // ok, would fail with -assert
   129  	s1 = i.(string)     // ok, would fail with -assert
   130  	s2, _ := i.(string) // ok, would fail with -blank
   131  	s2, _ = i.(string)  // ok, would fail with -blank
   132  	s3, ok := i.(string)
   133  	s3, ok = i.(string)
   134  	switch s4 := i.(type) {
   135  	case string:
   136  		_ = s4
   137  	}
   138  	_, _, _, _ = s1, s2, s3, ok
   139  
   140  	// Goroutine
   141  	go a()    // want "unchecked error"
   142  	defer a() // want "unchecked error"
   143  
   144  	// IO errors excluded by default
   145  	b1 := bytes.Buffer{}
   146  	b2 := &bytes.Buffer{}
   147  	b1.Write(nil)
   148  	b2.Write(nil)
   149  	rand.Read(nil)
   150  	mrand.Read(nil)
   151  	sha256.New().Write([]byte{})
   152  
   153  	os.ReadFile("main.go") // want "unchecked error"
   154  
   155  	var emiw ErrorMakerInterfaceWrapper
   156  	emiw.MakeNilError() // want "unchecked error"
   157  }