gitee.com/wgliang/goreporter@v0.0.0-20180902115603-df1b20f7c5d0/linters/errorcheck/test/test.go (about)

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